www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Tuple as Property Parameter

reply Brian Byrne <bdbyrne wisc.edu> writes:
I have a few [newcomer] questions about Tuples that I hope are easy to
answer. What I am trying to create is a property that accepts any tuple:

template Tuple( E... ) {
    alias E Tuple;
}

class Foo {
    int bar( E... )( E e ) {
        ...
    }
}

Foo f = new Foo();
f.bar( 1, 2 );              // Works properly
f.bar( Tuple!( 1, 2 ) ); // Works properly
f.bar = Tuple!( 1, 2 ); // Error: Error: (f).bar(E...) has no value

Is this intended? How can I wrap up the Tuple into a single object so it
will pass off as a property?

Changing 'bar' to 'opAssign' yields a different result for the third example.
Instead of printing out an error I get an alert "abnormal program
termination."

Also, is there, or will there ever be a bracket sequence for initializing
Tuples? Tuple!( a, b, c, ... ) into something like ![ a, b, c, ... ]?

Thanks,
Brian Byrne
Feb 06 2007
parent reply BCS <ao pathlink.com> writes:
Reply to Brian,

 I have a few [newcomer] questions about Tuples that I hope are easy to
 answer. What I am trying to create is a property that accepts any
 tuple:
 
 template Tuple( E... ) {
 alias E Tuple;
 }
 class Foo {
 int bar( E... )( E e ) {
 ...
 }
 }
 Foo f = new Foo();
 f.bar( 1, 2 );              // Works properly
 f.bar( Tuple!( 1, 2 ) ); // Works properly
 f.bar = Tuple!( 1, 2 ); // Error: Error: (f).bar(E...) has no value
 Is this intended? How can I wrap up the Tuple into a single object so
 it will pass off as a property?
 
 Changing 'bar' to 'opAssign' yields a different result for the third
 example. Instead of printing out an error I get an alert "abnormal
 program termination."
 
 Also, is there, or will there ever be a bracket sequence for
 initializing Tuples? Tuple!( a, b, c, ... ) into something like ![ a,
 b, c, ... ]?
 
 Thanks,
 Brian Byrne

For one thing, I'm not sure that any of those should work. Template members are invalid last time I checked.
Feb 06 2007
parent Kirk McDonald <kirklin.mcdonald gmail.com> writes:
BCS wrote:
 Reply to Brian,
 
 I have a few [newcomer] questions about Tuples that I hope are easy to
 answer. What I am trying to create is a property that accepts any
 tuple:

 template Tuple( E... ) {
 alias E Tuple;
 }
 class Foo {
 int bar( E... )( E e ) {
 ...
 }
 }
 Foo f = new Foo();
 f.bar( 1, 2 );              // Works properly
 f.bar( Tuple!( 1, 2 ) ); // Works properly
 f.bar = Tuple!( 1, 2 ); // Error: Error: (f).bar(E...) has no value
 Is this intended? How can I wrap up the Tuple into a single object so
 it will pass off as a property?

 Changing 'bar' to 'opAssign' yields a different result for the third
 example. Instead of printing out an error I get an alert "abnormal
 program termination."

 Also, is there, or will there ever be a bracket sequence for
 initializing Tuples? Tuple!( a, b, c, ... ) into something like ![ a,
 b, c, ... ]?

 Thanks,
 Brian Byrne

For one thing, I'm not sure that any of those should work. Template members are invalid last time I checked.

Template member functions work (and have for some time), they're just implicitly final. -- Kirk McDonald Pyd: Wrapping Python with D http://pyd.dsource.org
Feb 06 2007