Type inference for constructors
data pulverizer
data.pulverizer at gmail.com
Fri Sep 18 05:43:56 UTC 2020
I’d like to know if constructors of classes and structs can have
type inference. So far, I am using a separate function for this
purpose, for example:
```
import std.stdio: writeln;
struct Pair(T, U)
{
T first;
U second;
this(T first, U second)
{
this.first = first;
this.second = second;
}
}
Pair!(T, U) pair(T, U)(T first, U second)
{
return Pair!(T, U)(first, second);
}
void main()
{
auto mp = pair("Michael", 32);//standard function inference
works
//auto m0 = Pair("Michael", 32); //I’d like to be able to do
this
writeln("pair: ", mp);
}
```
On a slightly different note, I was doing some reading in
std.typecons
(https://github.com/dlang/phobos/blob/3754e92341a393331bd6b0bc9e70335d34e42016/std/typecons.d#L6838) on Github and wondered what `this` in the code below does:
```
auto ref opIndex(this X, D...)(auto ref D i) {
return a[i]; }
```
More information about the Digitalmars-d-learn
mailing list