object oriented value type

BCS ao at pathlink.com
Mon Jun 25 14:51:08 PDT 2007


Reply to Henning,

> BCS <ao at pathlink.com> schrieb (Mon, 25 Jun 2007 19:10:54 +0000 (UTC)):
> 
>> Reply to Robert,
>> 
>>> I do think having a syntax to add properties to primitive types is a
>>> good idea, though, in the same way as the funky array syntax works
>>> now.
>>> 
> Wouldnt it be more straigtforward if there were no basic types at all?
> Ok, you then run into the OO-value-type question again, but somithing
> like (to follow the example below)
> 
> // No special syntax needed to declare that this is a value type as it
> // inherits from one (int)
> class MyInt : int {
> static addCount = 0;
> MyInt opAdd(MyInt that) {
> addCount++;
> return this.value + that.value;
> }
> }

that would be vary hard to make work well in a systems language. 

the point of the typedef I J {...} would be to let the user change (at compile 
time) the semantics of the the built in types.

>> how about
>> 
>> |typedef int myInt
>> |{
>> |  static addCount=0; // static members? why not?
>> |
>> |  myInt opAdd(myInt that)
>> |  {
>> |     addCount++;
>> |     return this+that; // this is int
>
> That would be a bit confusing as it looks like recursion.
> 

okay make it:

return cast(int)this+cast(int)that;

>> |  }
>> |  private opMod(); // forbid mod on myInt
>> |}
>> added in template typedefs:
>> 
>> |typedef real SIuint(int dist, int mass, int time)
>> |{
>> |   SIuint!(dist+T.dist, mass+T.mass, time+T.time) opMull(T)(T p)
>> |      // in-lining reduces to same as normal mul
>> |   {
>> |      return this*p;
>> |   }
>> |}
>
> I must confess I dont think that I understand your example. Would that
> allow multiplying integers with units?
> 

Oh please don't nit pick <g> I just slaped that togehter.

SIuint!(dist+Tdist, mass+Tmass, time+Ttime) opMull(int Tdist, int Tmass, 
int Ttime)(SIunit!(Tdist, Tmass, Ttime) p)

some sort of int == SIUnit!(0,0,0) would be needed to.





More information about the Digitalmars-d mailing list