Questions on number constraints and conversions

# liorean (18 years ago)

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)?

  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?

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

# Yuh-Ruey Chen (18 years ago)

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.

# Brendan Eich (18 years ago)

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.

  1. 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.

# Lars T Hansen (18 years ago)

On 6/5/07, liorean <liorean at gmail.com> 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. 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?

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

Nothing's been discussed, really. Generally the number types are interconvertible and truncation etc can be hard to control. You could imagine a type constructor, eg "strict", s.t.

var x : strict int

means x can only convert from the value set implied by int. For example. But almost certainly not in ES4.