www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Ada-Style Sub-Typing

reply "Per =?UTF-8?B?Tm9yZGzDtnci?= <per.nordlow gmail.com> writes:
Is there some elegant way of creating "isolated" types in D 
similar to the semantics of `subtype` in Ada.

Something like this

struct A
{
     this(int value) { this._value = value; }
     private int _value;
     alias _value this;
}

struct B
{
     this(int value) { this._value = value; }
     private int _value;
     alias _value this;
}

void main(string[] args)
{
     A a = 11;
     B b = 12;
     a = b;
}

except that

the final statement should error.
Jun 02 2015
next sibling parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 06/02/2015 01:16 PM, "Per =?UTF-8?B?Tm9yZGzDtnci?= 
<per.nordlow gmail.com>" wrote:
 Is there some elegant way of creating "isolated" types in D similar to
 the semantics of `subtype` in Ada.

 Something like this

 struct A
 {
      this(int value) { this._value = value; }
      private int _value;
      alias _value this;
 }

 struct B
 {
      this(int value) { this._value = value; }
      private int _value;
      alias _value this;
 }

 void main(string[] args)
 {
      A a = 11;
      B b = 12;
      a = b;
 }

 except that

 the final statement should error.
Typedef: http://dlang.org/phobos/std_typecons.html#.Typedef Apparently, one needs to use its cookie feature to get what you need: import std.typecons; alias A = Typedef!(int, int.init, "A"); alias B = Typedef!(int, int.init, "B"); void main(string[] args) { A a = 11; B b = 12; a = b; // Fails to compile; good } Ali
Jun 02 2015
parent "Per =?UTF-8?B?Tm9yZGzDtnci?= <per.nordlow gmail.com> writes:
On Tuesday, 2 June 2015 at 21:44:33 UTC, Ali Çehreli wrote:
 On 06/02/2015 01:16 PM, "Per =?UTF-8?B?Tm9yZGzDtnci?= 
 <per.nordlow gmail.com>" wrote:
 Is there some elegant way of creating "isolated" types in D 
 similar to
 the semantics of `subtype` in Ada.
Correction, this is not subtype but a *derived* type, as type A is new Integer type B is new Integer in Ada. See also: http://en.wikibooks.org/wiki/Ada_Programming/Type_System#Derived_types
Jun 03 2015
prev sibling parent Charles Hixson via Digitalmars-d-learn writes:
It's not totally convenient, but what I'm currently using is a slightly 
less elaborate struct:
struct    A
{    int    v;    }

Since all I'm creating is an isolated type, the private stuff isn't 
needed.  If I wanted to get fancy I could start implementing 
operations.  Comparison is free, as sturcts of the same type are, by 
default, compared via a bit match.  This, however, doesn't give you less 
than, greater than, etc. I think you need to implement those, almost 
certainly if you want to use anything but unsigned comparison.

OTOH, if you do an alias this, then I'm not sure you maintain type 
isolation.  I think that will auto-convert to the type of the variable.  
(I haven't checked this...so perhaps I'm just being pessimistic/optimistic.)

Consider what purpose declaring the internal variable to be private 
serves.  Remember that structs are copied rather than passed by 
reference, and I think you'll decide it doesn't really serve any purpose.

On 06/02/2015 01:16 PM, via Digitalmars-d-learn wrote:
 Is there some elegant way of creating "isolated" types in D similar to 
 the semantics of `subtype` in Ada.

 Something like this

 struct A
 {
     this(int value) { this._value = value; }
     private int _value;
     alias _value this;
 }

 struct B
 {
     this(int value) { this._value = value; }
     private int _value;
     alias _value this;
 }

 void main(string[] args)
 {
     A a = 11;
     B b = 12;
     a = b;
 }

 except that

 the final statement should error.
Jun 06 2015