o!(const(T)) parameter.

anonymous via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Apr 25 08:20:03 PDT 2015


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


More information about the Digitalmars-d-learn mailing list