www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Is there a way to

reply Kyle Ingraham <kyle kyleingraham.com> writes:
Is there a way to write a delegate type as being specific for 
some parameters and tolerant of anything for others? For example, 
I can specify this type:

```d
alias MyDelegateType = void delegate(string name, int age);
```

It would be compatible with a pointer to this function converted 
to a delegate:

```d
void someFunc(string name, int age) { // }
```

That type would not work for this function though:

```d
void anotherFunc(string name, int age, string location) { // }
```

How can I write a type that is strict for the first two 
parameters but tolerant of any other parameter following those?
Sep 10 2022
parent reply Adam D Ruppe <destructionator gmail.com> writes:
On Saturday, 10 September 2022 at 23:37:30 UTC, Kyle Ingraham 
wrote:
 How can I write a type that is strict for the first two 
 parameters but tolerant of any other parameter following those?
That's not a type per se, but you can assign wrapper at the assignment thing. Like your_delegate = (a,b) => func_with_three_args(a, b, "some default"); that kind of thing. if you have a delegate in an object, you can use a property setter to accept more things and wrap it like that.
Sep 10 2022
parent reply Kyle Ingraham <kyle kyleingraham.com> writes:
On Sunday, 11 September 2022 at 00:04:55 UTC, Adam D Ruppe wrote:
 On Saturday, 10 September 2022 at 23:37:30 UTC, Kyle Ingraham 
 wrote:
 How can I write a type that is strict for the first two 
 parameters but tolerant of any other parameter following those?
That's not a type per se, but you can assign wrapper at the assignment thing. Like your_delegate = (a,b) => func_with_three_args(a, b, "some default"); that kind of thing. if you have a delegate in an object, you can use a property setter to accept more things and wrap it like that.
My problem is that I would like to store a reference to that delegate in a struct for later use: ```d struct MyStruct { MyDelegate myDelegate; } ``` I can't use default parameters because I want to be able to call the delegate with arguments extracted from a URL path at runtime. I'm mucking about with a custom router for vibe-d and trying to setup the ability for handlers to have different parameter signatures.
Sep 10 2022
parent reply Adam D Ruppe <destructionator gmail.com> writes:
On Sunday, 11 September 2022 at 00:32:18 UTC, Kyle Ingraham wrote:
 I can't use default parameters because I want to be able to 
 call the delegate with arguments extracted from a URL path at 
 runtime
Some kind of wrapper might not only be your best option, it might be your only option. Your delegate would be like string callWith(string[][string] webArgs) and you can generate a wrapper to convert arguments and call the original function.
 I'm mucking about with a custom router for vibe-d and trying to 
 setup the ability for handlers to have different parameter 
 signatures.
I'd be surprised if vibe doesn't already have this built in, my web frameworks have since before vibe was born. (my implementation btw does the wrapper thing like described above)
Sep 10 2022
parent Kyle Ingraham <kyle kyleingraham.com> writes:
On Sunday, 11 September 2022 at 00:56:39 UTC, Adam D Ruppe wrote:
 On Sunday, 11 September 2022 at 00:32:18 UTC, Kyle Ingraham 
 wrote:
 I can't use default parameters because I want to be able to 
 call the delegate with arguments extracted from a URL path at 
 runtime
Some kind of wrapper might not only be your best option, it might be your only option. Your delegate would be like string callWith(string[][string] webArgs) and you can generate a wrapper to convert arguments and call the original function.
 I'm mucking about with a custom router for vibe-d and trying 
 to setup the ability for handlers to have different parameter 
 signatures.
I'd be surprised if vibe doesn't already have this built in, my web frameworks have since before vibe was born. (my implementation btw does the wrapper thing like described above)
Thanks for the wrapper suggestion Adam. I tried that out and got the functionality that I wanted. I wrap the handler in a delegate that has the signature I need. vibe-d does have a way to achieve something similar but I'm working on a router that does a bit more path validation before calling a handler.
Sep 12 2022