questions on nullability

Peter Hall peterjoel at gmail.com
Fri Jun 16 13:33:37 PDT 2006


At the risk of being tangential, is there any value in generalising the
nullability concept into a pseudo type, which may be defined as the union of
some set of types, with a further set of types removed?

Re-using the + and - operators, it could be something like:

function add(a:(Number+Complex-Null), b:(Number+Complex-Null)):Complex{
  if(a is Number){
     a = new Complex(a, 0);
  }
  return a.add(b);
}

or

const ComplexOrReal = (Number+Complex-Null);
function add(a:ComplexOrReal, b:ComplexOrReal):Complex{
  if(a is Number){
     a = new Complex(a, 0);
  }
  return a.add(b);
}

Most examples that I could think of could also be accomplished with method
overloading. But it seems like a feature that might be simpler to implement
than overloading. Method return types would probably have to be disallowed
from using one of these types though.

Peter


On 6/15/06, Peter Hall <peterjoel at gmail.com> wrote:
>
> (redacted) writes:
> > > lth at opera.com writes:
> > > > The entire reason that the class definition can be annotated is to
> > > > make it easier for the programmer not to have to worry about null
> > when
> > > > using classes for which a null value does not make sense.  The
> > > > canonical example is "Complex": this is usually viewed as a "value
> > > > type", ie, it does not normally make sense to have Complex values
> > that
> > > > can be null.
> > >
> > > Isn't there then an argument for adding a syntax to provide a default
> > value
> > > in place of null, much as int or Number are defaulted to 0?
> >
> > That would not prevent you from later assigning null to a variable of
> > that type, though, or pass null to a function taking that type.
>
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.mozilla.org/pipermail/es-discuss/attachments/20060616/6e87e8c8/attachment.html 


More information about the Es4-discuss mailing list