www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Odd construct idea. Splitting arguments inside a parameter list.

reply Chris Katko <ckatko gmail.com> writes:
````D
struct pair
{
float x,y;
}

myFunction(float taco, float x, float y, float burrito)
  {
  // stuff
  }

myfunction(_taco, _x, _y, _burrito);  // call function

// But can we do this?
pair p;
myfunction(_taco, p; _burrito);  // p becomes (x,y) and satisfies 
the two floats in the signature
````
I don't know if I need this but I'm curious if it's a template 
possibility. Though under-the-hood it could violate some 
necessary assumption about function signature matching.

I'm curious if you can pass a struct of values (a 'tuple'?) with 
the right subfields, as if those fields occupied a function 
signature. (As I write this and try to explain it, it probably 
sounds impossible.)

I have an existing API that uses X/Y coordinates, but I like 
using packed/named tuples (right term?) for related arguments. 
pos(x,y) vs velocity(x,y) for example make it super easy to tell 
which x belongs to which construct. Worst case I could just write 
wrapper functions for like 60+ functions. But it's still an 
interesting "can D do this" idea that popped into my head 
tonight. I'm always curious about what's possible.

  - Note that it doesn't necessarily have the struct fields match 
the called function argument names. I'm talking about calling 
with a struct with two floats (of any name), fulfilling two float 
requirement of a function signature. Is there a way for a 
tuple/array/some-sort-of-combined object to fulfill two separate 
function arguments?

Of course we could always just do:
````D
pair p;
myFunction(taco, p.x, p.y, burrito);
````
May 23 2022
next sibling parent Mike Parker <aldacron gmail.com> writes:
On Monday, 23 May 2022 at 08:34:21 UTC, Chris Katko wrote:
 ````D
 I'm curious if you can pass a struct of values (a 'tuple'?) 
 with the right subfields, as if those fields occupied a 
 function signature. (As I write this and try to explain it, it 
 probably sounds impossible.)
Right now you can use `.tupleof`: ```d myFunction(taco, p.tupleof, burrito); ```
May 23 2022
prev sibling parent reply vit <vit vit.vit> writes:
On Monday, 23 May 2022 at 08:34:21 UTC, Chris Katko wrote:
 ````D
 struct pair
 {
 float x,y;
 }

 [...]
This work too: ```d myFunction(taco, p.tupleof, burrito); ```
May 23 2022
parent reply user1234 <user1234 12.de> writes:
On Monday, 23 May 2022 at 08:52:12 UTC, vit wrote:
 On Monday, 23 May 2022 at 08:34:21 UTC, Chris Katko wrote:
 ````D
 struct pair
 {
 float x,y;
 }

 [...]
This work too: ```d myFunction(taco, p.tupleof, burrito); ```
and you can pass a std.typecons.Tuple as well, it will expand x y
May 23 2022
parent user1234 <user1234 12.de> writes:
On Monday, 23 May 2022 at 08:53:27 UTC, user1234 wrote:
 On Monday, 23 May 2022 at 08:52:12 UTC, vit wrote:
 On Monday, 23 May 2022 at 08:34:21 UTC, Chris Katko wrote:
 ````D
 struct pair
 {
 float x,y;
 }

 [...]
This work too: ```d myFunction(taco, p.tupleof, burrito); ```
and you can pass a std.typecons.Tuple as well, it will expand x y
well actually std Tuple is a struct but that works without excplicit `.tupleof`.
May 23 2022