Questions on number constraints and conversions

Brendan Eich brendan at mozilla.org
Mon Jun 11 11:45:17 PDT 2007


On Jun 11, 2007, at 9:38 AM, Yuh-Ruey Chen wrote:

> I'm not well-versed with ES4's type system and I come from a JS
> background rather than an AS one, but I think you can define your own
> number class and override "function to T(v)" for each numeric type (or
> maybe just "function to Number(v)" would work) and define the casting
> behavior there, throwing TypeErrors when necessary.

FYI, the way to customize conversion for a class C is to write a meta  
static function convert(v:T):C -- the :C return type annotation is  
implicit, and :T is optional (T defaults to *) as usual.

> liorean wrote:
>> Hello!
>>
>> After discussion in a thread on ECMAScript binding of the DOM I came
>> to wonder a bit of what type of type contraints ES4 actually will be
>> able to have:
>>
>> 1. ES3 array.length is constrained so that if setting the value is
>> converted ToUInt32(rhs) and a TypeError is thrown if that value is  
>> not
>> equal to ToNumber(rhs). Is this type of type constraint possible in
>> ES4 short of using a setter (ignoring the fact that this type
>> constraint is not all that setter would have to deal with)?

See the reference implementation, builtins/Array.es (http:// 
ecmascript-lang.org/) -- length needs a setter. It's a hard case, we  
don't propose type annotation syntax to handle it.

>> 2. Also, conversions between the types. Is is possible to have a type
>> constraint of uint that does not wrap around negatives but instead
>> throws if the value is not part of the value set of uint for example?

We have not defined such a variant of uint, or overflow mode.

>> Similarly a type constraint of int and a value larger than 0x7fffffff
>> throwing instead of of wrapping around into the negatives?

Ditto. It could be done several ways. It's not going to fit ES4 at  
this point, IMO. But suggestions for the best way to support overflow  
exceptions, whenever it might be done, are welcome.

/be




More information about the Es4-discuss mailing list