www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 12100] New: __GENTYPE to generate ever different types

https://d.puremagic.com/issues/show_bug.cgi?id=12100

           Summary: __GENTYPE to generate ever different types
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: bearophile_hugs eml.cc


--- Comment #0 from bearophile_hugs eml.cc 2014-02-07 10:35:50 PST ---
Currently in std.typecons there is Typedef, that the docs describe with:

template Typedef(T)
struct Typedef(T, T init, string cookie = null); 


The cookie is needed when you want to create different types based on the same
type:

alias MyInt1 = Typedef!(int, 0, "A");
alias MyInt2 = Typedef!(int, 0, "B");


To remove the need of the cookie I suggest to add to D something that could be
named __GENTYPE, that every time creates a new type:

struct Typedef(T, T init=T.init, NT=__GENTYPE); 


So every instantiation of Typedef is different:

alias MyInt1 = Typedef!int;
alias MyInt2 = Typedef!int;
static assert (!is(MyInt1 == MyInt2));


A __GENTYPE is also a very simple way to implement the "Path Dependent Types"
of the Scala language. Here in Scala b1 and b2 have different type:

http://stackoverflow.com/questions/2693067/what-is-meant-by-scalas-path-dependent-types

class A {
    class B {
    }
}

var a1 = new A;
var a2 = new A;
var b1 = a1.new B;
var b2 = a2.new B;


Parametrizing the class A with an unused type, and using __GENTYPE inside a
helper function that instantiates A, you can have the same feature in D:

class A(Ghost) {
    class B {
    }
}


The disadvantage is that you have template bloat, so here it becomes useful an
idea I suggested to reduce template bloat, templated():

http://www.digitalmars.com/d/archives/digitalmars/D/templated_126655.html


The name __GENTYPE comes from the Lisp gensym that's used to generate new names
to be used in macros.

http://en.wikipedia.org/wiki/Hygienic_macro#Strategies_used_in_languages_that_lack_hygienic_macros

http://www.lispworks.com/documentation/HyperSpec/Body/f_gensym.htm#gensym

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 07 2014