Restrict type of function parameter to a defined list of types?
Martin B
martin.brzenska at googlemail.com
Sun Dec 12 13:11:58 UTC 2021
Hi everyone,
lets say that required is a setter method:
```
Nullable!string str(Nullable!string setter) {
return this._str = setter;
}
```
The user should be able to:
```
auto a = new A();
a.str = "abc";
```
As the setters parameter is defined to be of type
`Nullable!string`, the compiler complains. So the user need to
do `a.str = nullable("abc");`
I guess a solution could be to define the setter as:
```
Nullable!string str(T)(T setter) {
return this._str = setter;
}
```
^stupid question: Is this the correct way to restrict the
parameter type to be `string` or `Nullable!string` or is there a
more precise way to restrict?
BTW: here is a more complete simple example of what i am talking
about: https://run.dlang.io/is/zP4vkb
More information about the Digitalmars-d-learn
mailing list