How does alias exactly work

Ali Çehreli acehreli at yahoo.com
Tue Sep 29 02:17:28 UTC 2020


On 9/28/20 6:46 PM, Ruby The Roobster wrote:
> I thought alias could work like this with classes:

That would work with template parameters:

alias A = Foo!(3, "hello");

> 
> alias test = MyClass(3,"H",9.1); //Assume the constructor parameters for 
> MyClass are (int,string,double).
> 
> Can anybody fix this code?

You have to write a function (or a lambda):

class MyClass {
   this(int, string, double) {
   }
}

auto test1() {
   return new MyClass(3,"H",9.1);
}

auto test2 = () => new MyClass(4, "I", 10.1);

void main() {
   test1();
   test2();
}

However, I assumed each invocation would create a new object. If not, 
something like this:

MyClass test3;
static this() {
   test3 = new MyClass(5, "J", 11.1);
}

test3 is a single instance for each thread. A single instance for the 
whole program would be a different exercise. :)

Ali


More information about the Digitalmars-d-learn mailing list