Re: Types
- Posted by Robert Craig <rds at ATTCANADA.NET> Mar 26, 2000
- 574 views
Martin Nilson writes: > Question: Will the above examples always turn out the same way? > (i e: when type-checking is turned off, variables declared as > integers or sequences are always type-checked, while atoms and > user-defined types are never checked) The manual is deliberately vague about the type-checking that will continue to happen even when you say "without type_check". It's vague because this is meant to be left up to the implementer of Euphoria, and the details could change in future releases of the interpreter, or might be different in a Euphoria compiler (if ever). With the *current* implementation, Euphoria will continue to enforce the integer and sequence type checks, for example, atom a sequence s integer i s = 5 -- this will be caught i = {} -- this will be caught a = {1,2,3} -- this will *not* be caught and if you have a user-defined type: type foo(integer x) return x >= 0 and x < 10 end type foo f f = 99 -- this will not be caught f = 1.5 -- this will be caught f = {} -- this will be caught In other words, it will enforce integer and sequence when they are in the parameter declaration of the type, but it will not execute any code inside the type. There are generally more, often many more, references to the value of a variable, than there are assignments to the variable. A type-check wastes a bit of time when you assign, but makes up for it when you read the value of the variable. In Euphoria this is particularly true of integers and sequences, but not so much in the case of atoms (long story omitted). > "Types can also be called just like other functions." > Question: Are these the ONLY differences? (logically as > well as performanceically) Yes. Irv Mullins writes: > If type() is called as a function, then it takes a > whopping 2.5x longer! I can't think of any reason why declaring a function as a type would affect its performance. A quick test I just tried showed equal performance. function foo(integer i) -- same time when this is "type foo" return i end function integer x atom t t = time() for i = 1 to 10000000 do x = foo(0) end for ?time()-t -- 3.4 seconds on Pentium-350 Regards, Rob Craig Rapid Deployment Software http://www.RapidEuphoria.com