Supertypes, subtypes, and more

Joseph Rushton Wakeling via Digitalmars-d digitalmars-d at puremagic.com
Wed Jun 25 15:22:33 PDT 2014


On 25/06/14 23:10, Tobias Pankrath via Digitalmars-d wrote:
> Did you try using a method?
>
> module1
> ---
>
> struct Subtype {
> private int x;
> public int getX() { return x; }
> alias getX this;
> }
> ---
>
> module2
> ---
> import module1;
> void main()
> {
> Subtype st = Subtype(2);
> int x = st;
> }
> ---
>
> Works for me.

Yes, someone suggested this to me quite recently in fact.  But I think it 
doesn't solve the real issue -- you're still exposing implementation details, as 
a user can always call getX directly.


More information about the Digitalmars-d mailing list