View mode: basic / threaded / horizontal-split · Log in · Help
February 06, 2009
Tuples
I've been thinking a little about the idea of returning tuples from functions, w.r.t. Bugzilla 2628
(http://d.puremagic.com/issues/show_bug.cgi?id=2628).   Would it be feasible to solve this by making
struct[index] for any struct w/o an opIndex overload equivalent to struct.tupleof[index]?  This would be
trivial syntactic sugar, but would allow user-defined tuples in Phobos to look like a builtin tuple, be
returned from functions, etc., while changing very little under the hood.   Furthermore, for functions that
take a tuple, we could allow structs to be implicitly cast to tuples, e.g.:

struct Foo {
    uint u;
    float f;
}

void doStuff(uint myInt, float myFloat) {}

Foo foo;
doStuff(foo);  // Implicitly casts to foo.tupleof, works.
February 06, 2009
Re: Tuples
Reply to dsimcha,

> I've been thinking a little about the idea of returning tuples from
> functions, w.r.t. Bugzilla 2628
> (http://d.puremagic.com/issues/show_bug.cgi?id=2628).   Would it be
> feasible to solve this by making struct[index] for any struct w/o an
> opIndex overload equivalent to struct.tupleof[index]?  This would be
> trivial syntactic sugar, but would allow user-defined tuples in Phobos
> to look like a builtin tuple, be returned from functions, etc., while
> changing very little under the hood.   Furthermore, for functions that
> take a tuple, we could allow structs to be implicitly cast to tuples,
> e.g.:
> 
> struct Foo {
> uint u;
> float f;
> }
> void doStuff(uint myInt, float myFloat) {}
> 
> Foo foo;
> doStuff(foo);  // Implicitly casts to foo.tupleof, works.

I like the first part some (but not without reservations). As for the second 
bit... I don't like it. It would make overload resolution to tricky, if not 
at the compiler level than at the eyeball level.
February 06, 2009
Re: Tuples
== Quote from BCS (ao@pathlink.com)'s article
> Reply to dsimcha,
> > I've been thinking a little about the idea of returning tuples from
> > functions, w.r.t. Bugzilla 2628
> > (http://d.puremagic.com/issues/show_bug.cgi?id=2628).   Would it be
> > feasible to solve this by making struct[index] for any struct w/o an
> > opIndex overload equivalent to struct.tupleof[index]?  This would be
> > trivial syntactic sugar, but would allow user-defined tuples in Phobos
> > to look like a builtin tuple, be returned from functions, etc., while
> > changing very little under the hood.   Furthermore, for functions that
> > take a tuple, we could allow structs to be implicitly cast to tuples,
> > e.g.:
> >
> > struct Foo {
> > uint u;
> > float f;
> > }
> > void doStuff(uint myInt, float myFloat) {}
> >
> > Foo foo;
> > doStuff(foo);  // Implicitly casts to foo.tupleof, works.
> I like the first part some (but not without reservations). As for the second
> bit... I don't like it. It would make overload resolution to tricky, if not
> at the compiler level than at the eyeball level.

Yeah, scratch the second part.  In addition, I thought of this problem after I posted:

struct Foo {
   uint u;
   float f;
}

void doStuff(T...)(T args) {}
Foo foo;
doStuff(foo);  // Ambiguous.  Does the struct get passed, or is it //unpacked to a
tuple?
February 07, 2009
Re: Tuples
BCS wrote:
> Reply to dsimcha,
> 
>> I've been thinking a little about the idea of returning tuples from
>> functions, w.r.t. Bugzilla 2628
>> (http://d.puremagic.com/issues/show_bug.cgi?id=2628).   Would it be
>> feasible to solve this by making struct[index] for any struct w/o an
>> opIndex overload equivalent to struct.tupleof[index]?  This would be
>> trivial syntactic sugar, but would allow user-defined tuples in Phobos
>> to look like a builtin tuple, be returned from functions, etc., while
>> changing very little under the hood.   Furthermore, for functions that
>> take a tuple, we could allow structs to be implicitly cast to tuples,
>> e.g.:
>>
>> struct Foo {
>> uint u;
>> float f;
>> }
>> void doStuff(uint myInt, float myFloat) {}
>>
>> Foo foo;
>> doStuff(foo);  // Implicitly casts to foo.tupleof, works.
> 
> I like the first part some (but not without reservations). As for the
> second bit... I don't like it. It would make overload resolution to
> tricky, if not at the compiler level than at the eyeball level.

I personally believe this is all getting a bit too silly; it's all this
work to get around that you can't return tuples from a function.  I
think it'd be a better idea to just make it so we can return value
tuples from functions, period.

Of course, I also want automatic unpacking and to re-purpose that silly
comma operator, but that can wait.  :)

As for the second, I just use this:

Foo foo;
doStuff(foo.tupleof);

Note: it's been a while, and I might have used a special .tuple member;
can't remember.

 -- Daniel
Top | Discussion index | About this forum | D home