Restrict type of function parameter to a defined list of types?
Paul Backus
snarwin at gmail.com
Sun Dec 12 14:11:48 UTC 2021
On Sunday, 12 December 2021 at 13:42:08 UTC, Martin B wrote:
> On Sunday, 12 December 2021 at 13:21:06 UTC, Adam D Ruppe wrote:
>> On Sunday, 12 December 2021 at 13:11:58 UTC, Martin B wrote:
>> Just add a forwarding overload:
> Hi Adam,
>
> i am wondering if there is another possibility without having
> to create overloads for each parameter type - something like
> this pseudocode:
> ```
> Nullable!string str(T : {string, Nullable!string}) (T setter) {
> return this._str = setter;
> }
> ```
You can use a [template constraint][1]:
```d
Nullable!string str(T)(T setter)
if (is(T == string) || is(T == Nullable!string))
{
return this._str = setter;
}
```
[1]: https://dlang.org/spec/template.html#template_constraints
More information about the Digitalmars-d-learn
mailing list