www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - o!(const(T)) parameter.

reply "sclytrack" <sclytrack fake.com> writes:
I want a function with parameter o!(const(Form)) to accept both 
o!(Form) and o!(immutable(Form))

Is there a way to do it?




import std.stdio;
import std.traits;


class Form
{
         int number = 10;
}

struct o(T)
{
         T data;

         this(T data)
         {
                 this.data = data;
         }

         o!(const(Unqual!(T))) constify() const
         {
                 return o!(const(Unqual!(T)))(data);
         }

         alias constify this;
}


void hello(o!(const(Form)) frm)
{
   writeln(frm.data.number);
   writeln(typeof(frm.data).stringof);
}

void main()
{
         writeln("This application works nicely");
         auto frm = o!(Form) (new Form());
//      auto ifrm = o!(immutable(Form)) (new immutable Form());

         hello(frm);
//      hello(ifrm);
}
Apr 25 2015
parent reply "anonymous" <anonymous example.com> writes:
On Saturday, 25 April 2015 at 14:52:45 UTC, sclytrack wrote:
 I want a function with parameter o!(const(Form)) to accept both 
 o!(Form) and o!(immutable(Form))

 Is there a way to do it?





 import std.stdio;
 import std.traits;


 class Form
 {
         int number = 10;
 }

 struct o(T)
 {
         T data;

         this(T data)
         {
                 this.data = data;
         }

         o!(const(Unqual!(T))) constify() const
         {
                 return o!(const(Unqual!(T)))(data);
         }

         alias constify this;
 }


 void hello(o!(const(Form)) frm)
 {
   writeln(frm.data.number);
   writeln(typeof(frm.data).stringof);
 }

 void main()
 {
         writeln("This application works nicely");
         auto frm = o!(Form) (new Form());
 //      auto ifrm = o!(immutable(Form)) (new immutable Form());

         hello(frm);
 //      hello(ifrm);
 }
It works when you exclude the recursive alias this: static if(!is(T == const)) alias constify this; Your original program crashes the compiler, which is always a bug. I filed an issue: https://issues.dlang.org/show_bug.cgi?id=14499
Apr 25 2015
parent "sclytrack" <sclytrack fake.com> writes:
 It works when you exclude the recursive alias this:
     static if(!is(T == const)) alias constify this;

 Your original program crashes the compiler, which is always a 
 bug. I filed an issue:
 https://issues.dlang.org/show_bug.cgi?id=14499
Thank you, very much.
Apr 25 2015