From: "Chris Angell" <[EMAIL PROTECTED]> > I have an idea for the int() function. I think it would be cool if it > returned false/undefined when the argument passed to it is a > whole number.
This is related to something I've been pondering about variables and typing. Question A: In Perl6, how are types enforced? What would be the result of trying to compile and run the following code: my int $foo; $foo = 'Fred'; The rest of this message assumes that the answer to A is "run time error". I'd like to propose the following concept: by default, the result of the above code would still be a runtime error, but with the use of a special pragma/module, the result would be that the code runs as if $foo were an old fashioned dynamically typed scalar. *However*, that scalar would still know it's an integer trapped in a string's body. The same pragma/module that allowed this sort of thing to happen would have a general purpose sub that would indicate if a variable's value matches its typing: unless match_type($fred) { die 'The $foo variable must be an integer'; } Some compile-time checks would be rearranged a little. The compiler would refuse to allow you pass that variable to a declaration that wants some other type of argument. However, if you let the variable get passed unchecked to a sub that wants something else then you would get a run-time error. Here's one advantage to this approach. When I write an application in a language that uses dynamic typing, I have to deal with the reality that sometimes people type weird values into web forms. They put their names into number fields, etc. To handle these types of errors I have to jump through all kinds of hoops to deal with the weird values that come in. A lot of times these hoops involve storing the input into a string variable, testing the string, then assigning it again to the "real" variable. OTOH, with dynamic typing, I still need to run a bunch of regexes against the variable to test if it works. The proposed feature allows a simplified approach to type testing. -Miko