Thread overview
enhancement type system
Jul 31, 2008
baleog
Jul 31, 2008
downs
Jul 31, 2008
downs
Jul 31, 2008
bearophile
Aug 01, 2008
JAnderson
July 31, 2008
Can i dream that someday D would has an enhancement type system? To do something like this:

type T = { int[2..17] | string("default") }
type U = { T | None };

or it's a totally bad idea?
July 31, 2008
baleog wrote:
> Can i dream that someday D would has an enhancement type system? To do something like this:
> 
> type T = { int[2..17] | string("default") }

Yay Haskell!

Seriously, the closest AFAIK is 2.0's opDot plus implicit cast .. you could create a type that enforces being between 2 and 17 for as long as possible (as far as type deduction will let it), then falls back to int when an int is needed.

> type U = { T | None };
> 
> or it's a totally bad idea?

2.0's opDot goes towards this direction.

If I understand it correctly, it will let us code like this:

struct NonNull(T) {
  T obj;
  T opDot() {
    debug if (!obj) throw new Exception("Object has somehow become null!");
    return obj;
  }
  T opAssign(T t) {
    if (!t) throw new Exception("Cannot set NonNull to null!");
    obj = t;
  }
}
July 31, 2008
downs wrote:
>   T opAssign(T t) {
>     if (!t) throw new Exception("Cannot set NonNull to null!");
>     obj = t;
Er, of course, return obj = t;
>   }
> }
July 31, 2008
baleog:
> Can i dream that someday D would has an enhancement type system? To do something like this:

Are you talking algebraic data types, like ones in Scala/Haskell?
(I suggest people around here to take a look at Scala: optional semicolon, actor-based concurrency, algebraic data types, and more 'modern' things).

If you are talking about algebraic data types, then this may indeed require a more powerful type system... it's not a small change (the syntactic part of such changes are very little compared to the changes in the type system), I don't know what type system Scala has.

Bye,
bearophile
August 01, 2008
baleog wrote:
> Can i dream that someday D would has an enhancement type system? To do something like this:
> 
> type T = { int[2..17] | string("default") }
> type U = { T | None };
> 
> or it's a totally bad idea?

Is this kinda like a union?

-Joel