Ross Judson | 21 Dec 2004 16:38
Favicon

Default values

Philippe Altherr <Philippe.Altherr <at> epfl.ch> writes:

> The notation "= _" should not be understood as an assignment of a
> default value but rather as a way of declaring uninitialized mutable
> fields and variables. That's why "x = _" and "val x: K = _" are not
> allowed. As "= _" declares initialized fields and variables, you
> should never read them before assigning them with some
> value. Otherwise, you might get some unexpected results.
> 

At times I feel somewhat "boxed in" by Scala's type parameter system.  It is
difficult to create code that works on all numeric types, for example.  If I
want to create time-series code that operates across doubles, ints, and floats,
it's pretty hard to get there.  I can get the "default" values that I want with
_ and initialized variables, but there's no common set of operations across
numeric types that I can count on (there's no superclass for numeric types, and
no way to insert one in).  Scala's type parameters can't be "ORed", currently,
so no:

class [A >: Int | A >: Double] {
...
}

and there's no 

class [A >: NumVal] {
}

Scala does a great job of handling type parameters for regular classes.  I'd
like to see it somewhat more flexible at handling primitives, though, especially
numbers.  

There's obviously a big difference between text substitution (which is what C++
more or less does) and type substitution (which Scala does).  Scalac _knows_ the
types it is using to generate classes.  C++ just mushes things together and sees
if the result makes sense.  At times, that can be convenient ;)

I do a lot of work that involves time series of numbers.  I'd like to be able to
create classes that use longs, doubles, or objects as time representations, and
use various numbers as values.  I'd like to be able to sum the values in a time
series.  Short of extensive use of Any, lots of "matches", and the resulting
loss of rigor and type safety, I'm not sure how to get there in the current
language definition.

type TimeValue = Long | Double | java.util.Date;

class TimeSeries[T >:  TimeValue, V >: NumValue] {
  ...
}


Gmane