www.digitalmars.com         C & C++   DMDScript  

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


           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

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:


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():


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



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