www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Restrict type of function parameter to a defined list of types?

reply Martin B <martin.brzenska googlemail.com> writes:
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
Dec 12 2021
parent reply Adam D Ruppe <destructionator gmail.com> writes:
On Sunday, 12 December 2021 at 13:11:58 UTC, Martin B wrote:
 Hi everyone,

 lets say that required is a setter method:
 ```
 Nullable!string str(Nullable!string setter) {
     return this._str = setter;
 }
 ```
Just add a forwarding overload: auto str(string s) { return this.str(nullable(s)); } Then the user can pass either Nullable or string but not other things.
Dec 12 2021
parent reply Martin B <martin.brzenska googlemail.com> writes:
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; } ```
Dec 12 2021
parent reply Paul Backus <snarwin gmail.com> writes:
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
Dec 12 2021
parent reply Martin B <martin.brzenska googlemail.com> writes:
On Sunday, 12 December 2021 at 14:11:48 UTC, Paul Backus wrote:
 You can use a [template constraint][1]:
Hi Paul, yes! thats it, Thanks. I am facepalming me right now because have been on that webpage and missed that point.
Dec 12 2021
parent Imperatorn <johan_forsberg_86 hotmail.com> writes:
On Sunday, 12 December 2021 at 14:26:54 UTC, Martin B wrote:
 On Sunday, 12 December 2021 at 14:11:48 UTC, Paul Backus wrote:
 You can use a [template constraint][1]:
Hi Paul, yes! thats it, Thanks. I am facepalming me right now because have been on that webpage and missed that point.
Happens to everyone at some time 🌅
Dec 12 2021