www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Tuple types/Anonymous structs

reply "John C" <johnch_atms hotmail.com> writes:
Python has the concept of a tuple to pack a set of values into a single, 
unnamed type. These would be useful in situations when, for example, a 
function needs to return more than one variable, say to indicate whether an 
operation was successful, but you don't want to define a specific type.

Currently we'd do something like this:

    bool parseCommandLine(char[] cmdline, out bool showHelp) { ... }

Which is fine, but I think tuples might be a more elegant way of doing this.

Python's syntax for declaring a tuple is

    t = 12.3, 456, 'wow'

To unpack the values, you call into it as you would a list

    t[1]

which would be 456.

I've been looking at implementing a tuple library for D, studying similar 
concepts from other languages and library implementations (C++ boost, etc). 
A rough stab might look like this:

    class Tuple(First, Second) {

        this(First first, Second second) {
            first_ = first;
            second_ = second;
        }

        First first() { return first_; }
        Second second() { return second_; }

        private First first_;
        private Second second_;
    }

And you could use it like so:

    Tuple!(bool, bool) parseCommandLine(char[] cmdline) {
        ...
        return new Tuple!(bool, bool)(success, showHelp);
    }

    Tuple!(bool, bool) result = parseCommandLine(getCommandLine());
    bool showHelp = result.second;

An alternative would be to have a built-in tuple construct in D. They're 
essentially anonymous structs, so the syntax might resemble the definition 
of a struct:

    struct { bool; bool } parseCommandLine(char[] cmdline) {
        return { success, showHelp };
    }

    struct { bool; bool } result = parseCommandLine(getCommandLine());
    bool showHelp = result[1];

The advantages of a built-in version over a library are not clear at this 
stage, but it does look a little cleaner and slightly more succinct. What do 
people think of these ideas? Where else might tuples be useful? Would people 
like to see a tuple library for D, or have support for them built into the 
language? 
Apr 01 2005
next sibling parent reply pragma <pragma_member pathlink.com> writes:
In article <d2jmqn$smi$1 digitaldaemon.com>, John C says...
An alternative would be to have a built-in tuple construct in D. They're 
essentially anonymous structs, so the syntax might resemble the definition 
of a struct:

    struct { bool; bool } parseCommandLine(char[] cmdline) {
        return { success, showHelp };
    }

    struct { bool; bool } result = parseCommandLine(getCommandLine());
    bool showHelp = result[1];

The advantages of a built-in version over a library are not clear at this 
stage, but it does look a little cleaner and slightly more succinct. What do 
people think of these ideas? Where else might tuples be useful? Would people 
like to see a tuple library for D, or have support for them built into the 
language? 
I don't mean to completely deconstruct your idea, but I think what you're looking for isn't a tuple system any more than runtime struct initalization. You can still do things kind of like a tuple if the struct initalizer would work at run-time; it's broken in the same fashion as arrays.
 struct MyTuple{
     bool a, bool b;
 }

 MyTuple myfunc(){ return( {a:true,b:false} ); } // look closely :)
If you need something with N elements returned, then you're looking at using an array. I'll add that this is what scripted languages do anyhow, and they can "cheat" since their type system is usually soft; generic arrays flourish in such environments. So dynamic array and struct initalziation, when implemented, will get you most of the way there, IMO. - EricAnderton at yahoo
Apr 01 2005
parent reply "John C" <johnch_atms hotmail.com> writes:
"pragma" <pragma_member pathlink.com> wrote in message 
news:d2jssi$12up$1 digitaldaemon.com...
 In article <d2jmqn$smi$1 digitaldaemon.com>, John C says...
An alternative would be to have a built-in tuple construct in D. They're
essentially anonymous structs, so the syntax might resemble the definition
of a struct:

    struct { bool; bool } parseCommandLine(char[] cmdline) {
        return { success, showHelp };
    }

    struct { bool; bool } result = parseCommandLine(getCommandLine());
    bool showHelp = result[1];

The advantages of a built-in version over a library are not clear at this
stage, but it does look a little cleaner and slightly more succinct. What 
do
people think of these ideas? Where else might tuples be useful? Would 
people
like to see a tuple library for D, or have support for them built into the
language?
I don't mean to completely deconstruct your idea, but I think what you're looking for isn't a tuple system any more than runtime struct initalization.
The struct-like syntax was an attempt to avoid introducing a new keyword, because they're similar to structs. But perhaps you're right -- maybe what I'm after actually *is* runtime struct initialisation coupled with unnamed types.
 You can still do things kind of like a tuple if the struct initalizer 
 would work
 at run-time; it's broken in the same fashion as arrays.

 struct MyTuple{
     bool a, bool b;
 }

 MyTuple myfunc(){ return( {a:true,b:false} ); } // look closely :)
I didn't realise this was on the cards. Not quite there, as the struct will still need to be named, I assume.
 If you need something with N elements returned, then you're looking at 
 using an
 array.  I'll add that this is what scripted languages do anyhow, and they 
 can
 "cheat" since their type system is usually soft; generic arrays flourish 
 in such
 environments.

 So dynamic array and struct initalziation, when implemented, will get you 
 most
 of the way there, IMO.

 - EricAnderton at yahoo 
Apr 01 2005
parent pragma <pragma_member pathlink.com> writes:
In article <d2k0eb$1640$1 digitaldaemon.com>, John C says...
"pragma" <pragma_member pathlink.com> wrote in message 
 struct MyTuple{
     bool a, bool b;
 }

 MyTuple myfunc(){ return( {a:true,b:false} ); } // look closely :)
I didn't realise this was on the cards. Not quite there, as the struct will still need to be named, I assume.
The naming of types in D would be the only "fly in the ointment" to the type of functionality you crave. However, using a formal type declaration is a little less wordy than what you'd have to do with anonymous structs:
 struct{bool a,b} myfunc(){return{a:true,b:false};}  // small as I can make it
For what it's worth there's another proposal that's been floating about for over a year now. Some feel that what is needed is an 'autotype' or 'implicit' keyword that forces the compiler to use type deduction instead of a formal declartion. In which case, the method signature becomes:
 autotype myfunc(){return{a:true,b:false};} // much easier on the eyes
 autotype foo = myfunc(); // foo is now a struct with members a and b
The concept is borrowed directly from the C++0x language draft/proposal. However, I gather that this won't make it into D anytime soon. :( - EricAnderton at yahoo
Apr 02 2005
prev sibling next sibling parent "Charlie" <charles jwavro.com> writes:
Looks good.  I like it as a library addition, Im not sure it would work well
as a language feature might cause some troubles with name resolution, but I
would use the lib :).

Charlie

"John C" <johnch_atms hotmail.com> wrote in message
news:d2jmqn$smi$1 digitaldaemon.com...
 Python has the concept of a tuple to pack a set of values into a single,
 unnamed type. These would be useful in situations when, for example, a
 function needs to return more than one variable, say to indicate whether
an
 operation was successful, but you don't want to define a specific type.

 Currently we'd do something like this:

     bool parseCommandLine(char[] cmdline, out bool showHelp) { ... }

 Which is fine, but I think tuples might be a more elegant way of doing
this.
 Python's syntax for declaring a tuple is

     t = 12.3, 456, 'wow'

 To unpack the values, you call into it as you would a list

     t[1]

 which would be 456.

 I've been looking at implementing a tuple library for D, studying similar
 concepts from other languages and library implementations (C++ boost,
etc).
 A rough stab might look like this:

     class Tuple(First, Second) {

         this(First first, Second second) {
             first_ = first;
             second_ = second;
         }

         First first() { return first_; }
         Second second() { return second_; }

         private First first_;
         private Second second_;
     }

 And you could use it like so:

     Tuple!(bool, bool) parseCommandLine(char[] cmdline) {
         ...
         return new Tuple!(bool, bool)(success, showHelp);
     }

     Tuple!(bool, bool) result = parseCommandLine(getCommandLine());
     bool showHelp = result.second;

 An alternative would be to have a built-in tuple construct in D. They're
 essentially anonymous structs, so the syntax might resemble the definition
 of a struct:

     struct { bool; bool } parseCommandLine(char[] cmdline) {
         return { success, showHelp };
     }

     struct { bool; bool } result = parseCommandLine(getCommandLine());
     bool showHelp = result[1];

 The advantages of a built-in version over a library are not clear at this
 stage, but it does look a little cleaner and slightly more succinct. What
do
 people think of these ideas? Where else might tuples be useful? Would
people
 like to see a tuple library for D, or have support for them built into the
 language?
Apr 01 2005
prev sibling next sibling parent Sean Kelly <sean f4.ca> writes:
In article <d2jmqn$smi$1 digitaldaemon.com>, John C says...
I've been looking at implementing a tuple library for D, studying similar 
concepts from other languages and library implementations (C++ boost, etc). 
This approach works fairly well, and can be extended to to hold as many values as needed. But it's obviously not as elegant as a pure language solution. At this point, however, I think a language solution would be a 2.0 feature.
An alternative would be to have a built-in tuple construct in D. They're 
essentially anonymous structs, so the syntax might resemble the definition 
of a struct:

    struct { bool; bool } parseCommandLine(char[] cmdline) {
        return { success, showHelp };
    }

    struct { bool; bool } result = parseCommandLine(getCommandLine());
    bool showHelp = result[1];
I like the general idea, but combining struct and array syntax in that way seems a tad awkward. Either way, this would combine well with auto types: implicit s = parseCommandLine(cmdline); (I used "implicit" because I couldn't remember the keyword suggested for this particular feature) Sean
Apr 01 2005
prev sibling next sibling parent Georg Wrede <georg.wrede nospam.org> writes:
John C wrote:
 Python has the concept of a tuple to pack a set of values into a single, 
 unnamed type. These would be useful in situations when, for example, a 
 function needs to return more than one variable, say to indicate whether an 
 operation was successful, but you don't want to define a specific type.
 
 Currently we'd do something like this:
 
     bool parseCommandLine(char[] cmdline, out bool showHelp) { ... }
 
 Which is fine, but I think tuples might be a more elegant way of doing this.
For this to be useful, the language should be changed so that in principle any function can return tuples. (Examples: APL, Euphoria, etc.) Also, function calls should then implicitly handle the situations where too many or too few "items" are returned. And, if we go that way, then the usefulness would be multiplied by teaching people to make every function accept an (in principle) unlimited number of parameters, and to also return an (in principle) unlimited number of "items". But as an isolated feature, I think the usefulness is questionable. We can return references to arrays or structures, etc., when we really need to "return tuples". And we do already have out parameters. This is a big issue, and goes to the fundamentals of a language. D being "C family", it sits uneasily in D. Also, the current restrictions on what you can return, make the compiler and language more consistent. But, implemented in some library, this could be nice. Hey, we need a CDAN -- The Comprehensive D Archive Network ! :-)
Apr 02 2005
prev sibling parent reply "John C" <johnch_atms hotmail.com> writes:
After trying various ways of implementing a tuple type for multiple return 
values, I'm not terribly happy with the result. The lack of implicit 
template instantiation forced me to dump my preferred approach (which was 
based on Boost's tuple). The code required to access elements would have 
been too unwieldy.

Also, allowing templated classes with the same name to inherit from one 
another would have made the implementation simpler and smaller.

Anyway, I've attached a proof-of-concept version. I'm prepared for any 
criticism, but equally invite ideas for improvement. It's basically a set of 
template classes that work the same as your standard Pair<A, B> class, 
except you can have up to 15 elements.

Here's how to use it:

    Tuple!(float, float) getCoordinates() {
        return new Tuple!(float, float)(x, y);
    }

    Tuple!(float, float) coord = getCoordinates();

John. 


begin 666 tuples.d
M;6]D=6QE('1U<&QE<SL-" T*+RH-"B J(%1U<&QE('1E;7!L871E(&-L87-S

M9F]R(')E='5R;FEN9R!M=6QT:7!L92!V86QU97, 9G)O;2!A(&9U;F-T:6]N

M06-C97!T<R!U<"!T;R Q-2!E;&5M96YT<PT*("H 5F5R8F]S:71Y(&]F(&EN

M<W1A;G1I871I;VX 9F]R('1E;7!L871E<PT*("H ("T 5&5M<&QA=&5S('=I
M=&  <V%M92!N86UE(&-A;B=T(&EN:&5R:70 9G)O;2!O;F4 86YO=&AE< T*
M("H-"B J($ED96%L;'D =&AE(&EN=&5R9F%C92!W;W5L9"!B92!S;VUE=&AI
M;F< ;&EK92!T:&ES. T*("H-"B J("!P=6)L:6, 8VQA<W, 5'5P;&4H5$9I

M:&ES*%1&:7)S="!F:7)S="P 5%-E8V]N9"!S96-O;F0I.PT*("H-"B J("  



M9FQO870L(&9L;V%T*2!T(#T 9V5T0V]O<F1I;F%T97,H*3L-"B J("  9FQO
M870 >" ]('0N9V5T(2AF;&]A="DH,"D[("\O(&=E="!T:&4 9FER<W0 87)G


M<F4 <W5P<&]R=&5D(&]N(&]P97)A=&]R<RP =V4 8V]U;&0 ;W9E<FQO860 


M;&5M96YT<R!I<R!A8VAI979E9"!B>2!C86QL:6YG('1H92!N86UE(&]F('1H

M=#L-




M92A41FER<W0L(%1396-O;F0I('L-" T*("!T:&ES*%1&:7)S="!F:7)S="P 
M5%-E8V]N9"!S96-O;F0I('L-"B  ("!F:7)S=%\ /2!F:7)S=#L-"B  ("!S
M96-O;F1?(#T




M<W0 9FER<W0L(%1396-O;F0 <V5C;VYD+"!45&AI<F0 =&AI<F0I('L-"B  
M("!F:7)S=%\ /2!F:7)S=#L-"B  ("!S96-O;F1?(#T <V5C;VYD.PT*("  


M<V5C;VYD7SL ?0T*("!45&AI<F0 =&AI<F0H*2![(')E='5R;B!T:&ER9%\[


M" T*<'5B;&EC(&-L87-S(%1U<&QE*%1&:7)S="P 5%-E8V]N9"P 5%1H:7)D


M9FER<W1?(#T
M:&ER9%\ /2!T:&ER9#L-"B  ("!F;W5R=&A?(#T

M;F0 <V5C;VYD*"D >R!R971U<FX <V5C;VYD7SL ?0T*("!45&AI<F0 =&AI
M<F0H*2![(')E='5R;B!T:&ER9%\[('T-"B  5$9O=7)T:"!F;W5R=& H*2![

M.PT*("!P<FEV871E(%1396-O;F0 <V5C;VYD7SL-"B  <')I=F%T92!45&AI
M<F0 =&AI<F1?.PT*("!P<FEV871E(%1&;W5R=&  9F]U<G1H7SL-" T*?0T*


M4V5C;VYD('-E8V]N9"P 5%1H:7)D('1H:7)D+"!41F]U<G1H(&9O=7)T:"P 

M;VYD7R ]('-E8V]N9#L-"B  ("!T:&ER9%\ /2!T:&ER9#L-"B  ("!F;W5R
M=&A?(#T

M<V5C;VYD*"D >R!R971U<FX <V5C;VYD7SL ?0T*("!45&AI<F0 =&AI<F0H
M*2![(')E='5R;B!T:&ER9%\[('T-"B  5$9O=7)T:"!F;W5R=& H*2![(')E






M"B  =&AI<RA41FER<W0 9FER<W0L(%1396-O;F0 <V5C;VYD+"!45&AI<F0 
M=&AI<F0L(%1&;W5R=&  9F]U<G1H+"!41FEF=&  9FEF=& L(%13:7AT:"!S
M:7AT:"D >PT*("  (&9I<G-T7R ](&9I<G-T.PT*("  ('-E8V]N9%\ /2!S

M=7)T:#L-"B  ("!F:69T:%\ /2!F:69T:#L-"B  ("!S:7AT:%\ /2!S:7AT
M:#L-
M?0T*("!44V5C;VYD('-E8V]N9" I('L <F5T=7)N('-E8V]N9%\[('T-"B  

M9F]U<G1H*"D >R!R971U<FX 9F]U<G1H7SL ?0T*("!41FEF=&  9FEF=& H
M*2![(')E='5R;B!F:69T:%\[('T-"B  5%-I>'1H('-I>'1H*"D >R!R971U

M<FEV871E(%1396-O;F0 <V5C;VYD7SL-"B  <')I=F%T92!45&AI<F0 =&AI
M<F1?.PT*("!P<FEV871E(%1&;W5R=&  9F]U<G1H7SL-"B  <')I=F%T92!4

M" T*<'5B;&EC(&-L87-S(%1U<&QE*%1&:7)S="P 5%-E8V]N9"P 5%1H:7)D
M+"!41F]U<G1H+"!41FEF=& L(%13:7AT:"P 5%-E=F5N=& I('L-" T*("!T
M:&ES*%1&:7)S="!F:7)S="P 5%-E8V]N9"!S96-O;F0L(%14:&ER9"!T:&ER
M9"P 5$9O=7)T:"!F;W5R=& L(%1&:69T:"!F:69T:"P 5%-I>'1H('-I>'1H

M("  <V5C;VYD7R ]('-E8V]N9#L-"B  ("!T:&ER9%\ /2!T:&ER9#L-"B  
M("!F;W5R=&A?(#T 9F]U<G1H.PT*("  (&9I9G1H7R ](&9I9G1H.PT*("  
M('-I>'1H7R ]('-I>'1H.PT*("  ('-E=F5N=&A?(#T <V5V96YT:#L-"B  

M4V5C;VYD('-E8V]N9" I('L <F5T=7)N('-E8V]N9%\[('T-"B  5%1H:7)D

M*"D >R!R971U<FX 9F]U<G1H7SL ?0T*("!41FEF=&  9FEF=& H*2![(')E
M='5R;B!F:69T:%\[('T-"B  5%-I>'1H('-I>'1H*"D >R!R971U<FX <VEX




M9G1H7SL-"B  <')I=F%T92!44VEX=&  <VEX=&A?.PT*("!P<FEV871E(%13

M1FER<W0L(%1396-O;F0L(%14:&ER9"P 5$9O=7)T:"P 5$9I9G1H+"!44VEX

M<W0L(%1396-O;F0 <V5C;VYD+"!45&AI<F0 =&AI<F0L(%1&;W5R=&  9F]U
M<G1H+"!41FEF=&  9FEF=& L(%13:7AT:"!S:7AT:"P 5%-E=F5N=&  <V5V
M96YT:"P 5$5I9VAT:"!E:6=H=& I('L-"B  ("!F:7)S=%\ /2!F:7)S=#L-
M"B  ("!S96-O;F1?(#T <V5C;VYD.PT*("  ('1H:7)D7R ]('1H:7)D.PT*

M("  <VEX=&A?(#T

M*2![(')E='5R;B!F:7)S=%\[('T-"B  5%-E8V]N9"!S96-O;F0H*2![(')E

M:7)D7SL ?0T*("!41F]U<G1H(&9O=7)T:" I('L <F5T=7)N(&9O=7)T:%\[

M:7AT:"!S:7AT:" I('L <F5T=7)N('-I>'1H7SL ?0T*("!44V5V96YT:"!S
M979E;G1H*"D >R!R971U<FX <V5V96YT:%\[('T-"B  5$5I9VAT:"!E:6=H

M9FER<W1?.PT*("!P<FEV871E(%1396-O;F0 <V5C;VYD7SL-"B  <')I=F%T
M92!45&AI<F0 =&AI<F1?.PT*("!P<FEV871E(%1&;W5R=&  9F]U<G1H7SL-
M"B  <')I=F%T92!41FEF=&  9FEF=&A?.PT*("!P<FEV871E(%13:7AT:"!S


M92A41FER<W0L(%1396-O;F0L(%14:&ER9"P 5$9O=7)T:"P 5$9I9G1H+"!4
M4VEX=& L(%13979E;G1H+"!416EG:'1H+"!43FEN=& I('L-" T*("!T:&ES
M*%1&:7)S="!F:7)S="P 5%-E8V]N9"!S96-O;F0L(%14:&ER9"!T:&ER9"P 
M5$9O=7)T:"!F;W5R=& L(%1&:69T:"!F:69T:"P 5%-I>'1H('-I>'1H+"!4
M4V5V96YT:"!S979E;G1H+"!416EG:'1H(&5I9VAT:"P 5$YI;G1H(&YI;G1H

M9#L-"B  ("!T:&ER9%\ /2!T:&ER9#L-"B  ("!F;W5R=&A?(#T 9F]U<G1H
M.PT*("  (&9I9G1H7R ](&9I9G1H.PT*("  ('-I>'1H7R ]('-I>'1H.PT*
M("  ('-E=F5N=&A?(#T <V5V96YT:#L-"B  ("!E:6=H=&A?(#T 96EG:'1H


M971U<FX <V5C;VYD7SL ?0T*("!45&AI<F0 =&AI<F0H*2![(')E='5R;B!T
M:&ER9%\[('T-"B  5$9O=7)T:"!F;W5R=& H*2![(')E='5R;B!F;W5R=&A?

M4VEX=&  <VEX=& H*2![(')E='5R;B!S:7AT:%\[('T-"B  5%-E=F5N=&  

M:'1H*"D >R!R971U<FX 96EG:'1H7SL ?0T*("!43FEN=&  ;FEN=& H*2![
M(')E='5R;B!N:6YT:%\[('T-" T*("!P<FEV871E(%1&:7)S="!F:7)S=%\[



M"B  <')I=F%T92!44V5V96YT:"!S979E;G1H7SL-"B  <')I=F%T92!416EG


M(%1&;W5R=& L(%1&:69T:"P 5%-I>'1H+"!44V5V96YT:"P 5$5I9VAT:"P 
M5$YI;G1H+"!45&5N=& I('L-" T*("!T:&ES*%1&:7)S="!F:7)S="P 5%-E
M8V]N9"!S96-O;F0L(%14:&ER9"!T:&ER9"P 5$9O=7)T:"!F;W5R=& L(%1&
M:69T:"!F:69T:"P 5%-I>'1H('-I>'1H+"!44V5V96YT:"!S979E;G1H+"!4
M16EG:'1H(&5I9VAT:"P 5$YI;G1H(&YI;G1H+"!45&5N=&  =&5N=& I('L-
M"B  ("!F:7)S=%\ /2!F:7)S=#L-"B  ("!S96-O;F1?(#T <V5C;VYD.PT*

M("  9FEF=&A?(#T

M("  ;FEN=&A?(#T
M" T*("!41FER<W0 9FER<W0H*2![(')E='5R;B!F:7)S=%\[('T-"B  5%-E

M:&ER9" I('L <F5T=7)N('1H:7)D7SL ?0T*("!41F]U<G1H(&9O=7)T:" I
M('L <F5T=7)N(&9O=7)T:%\[('T-"B  5$9I9G1H(&9I9G1H*"D >R!R971U

M7SL ?0T*("!44V5V96YT:"!S979E;G1H*"D >R!R971U<FX <V5V96YT:%\[

M(%1.:6YT:"!N:6YT:" I('L <F5T=7)N(&YI;G1H7SL ?0T*("!45&5N=&  
M=&5N=& H*2![(')E='5R;B!T96YT:%\[('T-" T*("!P<FEV871E(%1&:7)S



M('-I>'1H7SL-"B  <')I=F%T92!44V5V96YT:"!S979E;G1H7SL-"B  <')I


M;&%S<R!4=7!L92A41FER<W0L(%1396-O;F0L(%14:&ER9"P 5$9O=7)T:"P 
M5$9I9G1H+"!44VEX=& L(%13979E;G1H+"!416EG:'1H+"!43FEN=& L(%14

M96-O;F0 <V5C;VYD+"!45&AI<F0 =&AI<F0L(%1&;W5R=&  9F]U<G1H+"!4
M1FEF=&  9FEF=& L(%13:7AT:"!S:7AT:"P 5%-E=F5N=&  <V5V96YT:"P 
M5$5I9VAT:"!E:6=H=& L(%1.:6YT:"!N:6YT:"P 5%1E;G1H('1E;G1H+"!4
M16QE=F5N=&  96QE=F5N=& I('L-"B  ("!F:7)S=%\ /2!F:7)S=#L-"B  
M("!S96-O;F1?(#T <V5C;VYD.PT*("  ('1H:7)D7R ]('1H:7)D.PT*("  

M<VEX=&A?(#T

M=&5N=&A?(#T
M('T-" T*("!41FER<W0 9FER<W0H*2![(')E='5R;B!F:7)S=%\[('T-"B  

M9"!T:&ER9" I('L <F5T=7)N('1H:7)D7SL ?0T*("!41F]U<G1H(&9O=7)T
M:" I('L <F5T=7)N(&9O=7)T:%\[('T-"B  5$9I9G1H(&9I9G1H*"D >R!R

M>'1H7SL ?0T*("!44V5V96YT:"!S979E;G1H*"D >R!R971U<FX <V5V96YT
M:%\[('T-"B  5$5I9VAT:"!E:6=H=& H*2![(')E='5R;B!E:6=H=&A?.R!]

M=&  =&5N=& H*2![(')E='5R;B!T96YT:%\[('T-"B  5$5L979E;G1H(&5L

M1FER<W0 9FER<W1?.PT*("!P<FEV871E(%1396-O;F0 <V5C;VYD7SL-"B  
M<')I=F%T92!45&AI<F0 =&AI<F1?.PT*("!P<FEV871E(%1&;W5R=&  9F]U
M<G1H7SL-"B  <')I=F%T92!41FEF=&  9FEF=&A?.PT*("!P<FEV871E(%13

M('!R:79A=&4 5$5I9VAT:"!E:6=H=&A?.PT*("!P<FEV871E(%1.:6YT:"!N


M92A41FER<W0L(%1396-O;F0L(%14:&ER9"P 5$9O=7)T:"P 5$9I9G1H+"!4
M4VEX=& L(%13979E;G1H+"!416EG:'1H+"!43FEN=& L(%1496YT:"P 5$5L

M4V5C;VYD('-E8V]N9"P 5%1H:7)D('1H:7)D+"!41F]U<G1H(&9O=7)T:"P 
M5$9I9G1H(&9I9G1H+"!44VEX=&  <VEX=& L(%13979E;G1H('-E=F5N=& L
M(%1%:6=H=&  96EG:'1H+"!43FEN=&  ;FEN=& L(%1496YT:"!T96YT:"P 

M9FER<W1?(#T
M:&ER9%\ /2!T:&ER9#L-"B  ("!F;W5R=&A?(#T 9F]U<G1H.PT*("  (&9I
M9G1H7R ](&9I9G1H.PT*("  ('-I>'1H7R ]('-I>'1H.PT*("  ('-E=F5N
M=&A?(#T <V5V96YT:#L-"B  ("!E:6=H=&A?(#T 96EG:'1H.PT*("  (&YI
M;G1H7R ](&YI;G1H.PT*("  ('1E;G1H7R ]('1E;G1H.PT*("  (&5L979E
M;G1H7R ](&5L979E;G1H.PT*("  ('1W96QF=&A?(#T ='=E;&9T:#L-"B  

M4V5C;VYD('-E8V]N9" I('L <F5T=7)N('-E8V]N9%\[('T-"B  5%1H:7)D

M*"D >R!R971U<FX 9F]U<G1H7SL ?0T*("!41FEF=&  9FEF=& H*2![(')E
M='5R;B!F:69T:%\[('T-"B  5%-I>'1H('-I>'1H*"D >R!R971U<FX <VEX

M7SL ?0T*("!416EG:'1H(&5I9VAT:" I('L <F5T=7)N(&5I9VAT:%\[('T-

M:"!T96YT:" I('L <F5T=7)N('1E;G1H7SL ?0T*("!416QE=F5N=&  96QE
M=F5N=& H*2![(')E='5R;B!E;&5V96YT:%\[('T-"B  5%1W96QF=&  ='=E

M<W0 9FER<W1?.PT*("!P<FEV871E(%1396-O;F0 <V5C;VYD7SL-"B  <')I
M=F%T92!45&AI<F0 =&AI<F1?.PT*("!P<FEV871E(%1&;W5R=&  9F]U<G1H
M7SL-"B  <')I=F%T92!41FEF=&  9FEF=&A?.PT*("!P<FEV871E(%13:7AT

M:79A=&4 5$5I9VAT:"!E:6=H=&A?.PT*("!P<FEV871E(%1.:6YT:"!N:6YT

M=F5N=&  96QE=F5N=&A?.PT*("!P<FEV871E(%14=V5L9G1H('1W96QF=&A?

M(%14:&ER9"P 5$9O=7)T:"P 5$9I9G1H+"!44VEX=& L(%13979E;G1H+"!4
M16EG:'1H+"!43FEN=& L(%1496YT:"P 5$5L979E;G1H+"!45'=E;&9T:"P 
M5%1H:7)T965N=& I('L-" T*("!T:&ES*%1&:7)S="!F:7)S="P 5%-E8V]N
M9"!S96-O;F0L(%14:&ER9"!T:&ER9"P 5$9O=7)T:"!F;W5R=& L(%1&:69T
M:"!F:69T:"P 5%-I>'1H('-I>'1H+"!44V5V96YT:"!S979E;G1H+"!416EG
M:'1H(&5I9VAT:"P 5$YI;G1H(&YI;G1H+"!45&5N=&  =&5N=& L(%1%;&5V
M96YT:"!E;&5V96YT:"P 5%1W96QF=&  ='=E;&9T:"P 5%1H:7)T965N=&  
M=&AI<G1E96YT:"D >PT*("  (&9I<G-T7R ](&9I<G-T.PT*("  ('-E8V]N

M7R ](&9O=7)T:#L-"B  ("!F:69T:%\ /2!F:69T:#L-"B  ("!S:7AT:%\ 
M/2!S:7AT:#L-
M7R ](&5I9VAT:#L-"B  ("!N:6YT:%\ /2!N:6YT:#L-"B  ("!T96YT:%\ 
M/2!T96YT:#L-"B  ("!E;&5V96YT:%\ /2!E;&5V96YT:#L-"B  ("!T=V5L



M5&AI<F0 =&AI<F0H*2![(')E='5R;B!T:&ER9%\[('T-"B  5$9O=7)T:"!F

M('L <F5T=7)N(&9I9G1H7SL ?0T*("!44VEX=&  <VEX=& H*2![(')E='5R
M;B!S:7AT:%\[('T-"B  5%-E=F5N=&  <V5V96YT:" I('L <F5T=7)N('-E

M7SL ?0T*("!43FEN=&  ;FEN=& H*2![(')E='5R;B!N:6YT:%\[('T-"B  

M:"!E;&5V96YT:" I('L <F5T=7)N(&5L979E;G1H7SL ?0T*("!45'=E;&9T
M:"!T=V5L9G1H*"D >R!R971U<FX ='=E;&9T:%\[('T-"B  5%1H:7)T965N

M<')I=F%T92!41FER<W0 9FER<W1?.PT*("!P<FEV871E(%1396-O;F0 <V5C
M;VYD7SL-"B  <')I=F%T92!45&AI<F0 =&AI<F1?.PT*("!P<FEV871E(%1&
M;W5R=&  9F]U<G1H7SL-"B  <')I=F%T92!41FEF=&  9FEF=&A?.PT*("!P



M<')I=F%T92!416QE=F5N=&  96QE=F5N=&A?.PT*("!P<FEV871E(%14=V5L
M9G1H('1W96QF=&A?.PT*("!P<FEV871E(%14:&ER=&5E;G1H('1H:7)T965N

M;F0L(%14:&ER9"P 5$9O=7)T:"P 5$9I9G1H+"!44VEX=& L(%13979E;G1H
M+"!416EG:'1H+"!43FEN=& L(%1496YT:"P 5$5L979E;G1H+"!45'=E;&9T

M<W0 9FER<W0L(%1396-O;F0 <V5C;VYD+"!45&AI<F0 =&AI<F0L(%1&;W5R
M=&  9F]U<G1H+"!41FEF=&  9FEF=& L(%13:7AT:"!S:7AT:"P 5%-E=F5N
M=&  <V5V96YT:"P 5$5I9VAT:"!E:6=H=& L(%1.:6YT:"!N:6YT:"P 5%1E
M;G1H('1E;G1H+"!416QE=F5N=&  96QE=F5N=& L(%14=V5L9G1H('1W96QF
M=& L(%14:&ER=&5E;G1H('1H:7)T965N=& L(%1&;W5R=&5E;G1H(&9O=7)T
M965N=& I('L-"B  ("!F:7)S=%\ /2!F:7)S=#L-"B  ("!S96-O;F1?(#T 
M<V5C;VYD.PT*("  ('1H:7)D7R ]('1H:7)D.PT*("  (&9O=7)T:%\ /2!F




M/2!T=V5L9G1H.PT*("  ('1H:7)T965N=&A?(#T =&AI<G1E96YT:#L-"B  

M9FER<W0H*2![(')E='5R;B!F:7)S=%\[('T-"B  5%-E8V]N9"!S96-O;F0H

M=7)N('1H:7)D7SL ?0T*("!41F]U<G1H(&9O=7)T:" I('L <F5T=7)N(&9O
M=7)T:%\[('T-"B  5$9I9G1H(&9I9G1H*"D >R!R971U<FX 9FEF=&A?.R!]

M96YT:"!S979E;G1H*"D >R!R971U<FX <V5V96YT:%\[('T-"B  5$5I9VAT

M:" I('L <F5T=7)N(&YI;G1H7SL ?0T*("!45&5N=&  =&5N=& H*2![(')E
M='5R;B!T96YT:%\[('T-"B  5$5L979E;G1H(&5L979E;G1H*"D >R!R971U

M;B!T=V5L9G1H7SL ?0T*("!45&AI<G1E96YT:"!T:&ER=&5E;G1H*"D >R!R
M971U<FX =&AI<G1E96YT:%\[('T-"B  5$9O=7)T965N=&  9F]U<G1E96YT

M<W0 9FER<W1?.PT*("!P<FEV871E(%1396-O;F0 <V5C;VYD7SL-"B  <')I
M=F%T92!45&AI<F0 =&AI<F1?.PT*("!P<FEV871E(%1&;W5R=&  9F]U<G1H
M7SL-"B  <')I=F%T92!41FEF=&  9FEF=&A?.PT*("!P<FEV871E(%13:7AT

M:79A=&4 5$5I9VAT:"!E:6=H=&A?.PT*("!P<FEV871E(%1.:6YT:"!N:6YT

M=F5N=&  96QE=F5N=&A?.PT*("!P<FEV871E(%14=V5L9G1H('1W96QF=&A?
M.PT*("!P<FEV871E(%14:&ER=&5E;G1H('1H:7)T965N=&A?.PT*("!P<FEV

M;&%S<R!4=7!L92A41FER<W0L(%1396-O;F0L(%14:&ER9"P 5$9O=7)T:"P 
M5$9I9G1H+"!44VEX=& L(%13979E;G1H+"!416EG:'1H+"!43FEN=& L(%14
M96YT:"P 5$5L979E;G1H+"!45'=E;&9T:"P 5%1H:7)T965N=& L(%1&;W5R

M(%1396-O;F0 <V5C;VYD+"!45&AI<F0 =&AI<F0L(%1&;W5R=&  9F]U<G1H
M+"!41FEF=&  9FEF=& L(%13:7AT:"!S:7AT:"P 5%-E=F5N=&  <V5V96YT
M:"P 5$5I9VAT:"!E:6=H=& L(%1.:6YT:"!N:6YT:"P 5%1E;G1H('1E;G1H
M+"!416QE=F5N=&  96QE=F5N=& L(%14=V5L9G1H('1W96QF=& L(%14:&ER
M=&5E;G1H('1H:7)T965N=& L(%1&;W5R=&5E;G1H(&9O=7)T965N=& L(%1&

M("  <V5C;VYD7R ]('-E8V]N9#L-"B  ("!T:&ER9%\ /2!T:&ER9#L-"B  
M("!F;W5R=&A?(#T 9F]U<G1H.PT*("  (&9I9G1H7R ](&9I9G1H.PT*("  
M('-I>'1H7R ]('-I>'1H.PT*("  ('-E=F5N=&A?(#T <V5V96YT:#L-"B  
M("!E:6=H=&A?(#T 96EG:'1H.PT*("  (&YI;G1H7R ](&YI;G1H.PT*("  
M('1E;G1H7R ]('1E;G1H.PT*("  (&5L979E;G1H7R ](&5L979E;G1H.PT*
M("  ('1W96QF=&A?(#T ='=E;&9T:#L-"B  ("!T:&ER=&5E;G1H7R ]('1H


M<W0H*2![(')E='5R;B!F:7)S=%\[('T-"B  5%-E8V]N9"!S96-O;F0H*2![

M('1H:7)D7SL ?0T*("!41F]U<G1H(&9O=7)T:" I('L <F5T=7)N(&9O=7)T

M(%13:7AT:"!S:7AT:" I('L <F5T=7)N('-I>'1H7SL ?0T*("!44V5V96YT
M:"!S979E;G1H*"D >R!R971U<FX <V5V96YT:%\[('T-"B  5$5I9VAT:"!E

M('L <F5T=7)N(&YI;G1H7SL ?0T*("!45&5N=&  =&5N=& H*2![(')E='5R
M;B!T96YT:%\[('T-"B  5$5L979E;G1H(&5L979E;G1H*"D >R!R971U<FX 

M=V5L9G1H7SL ?0T*("!45&AI<G1E96YT:"!T:&ER=&5E;G1H*"D >R!R971U
M<FX =&AI<G1E96YT:%\[('T-"B  5$9O=7)T965N=&  9F]U<G1E96YT:" I



M:79A=&4 5%1H:7)D('1H:7)D7SL-"B  <')I=F%T92!41F]U<G1H(&9O=7)T

M=&  <VEX=&A?.PT*("!P<FEV871E(%13979E;G1H('-E=F5N=&A?.PT*("!P
M<FEV871E(%1%:6=H=&  96EG:'1H7SL-"B  <')I=F%T92!43FEN=&  ;FEN

M979E;G1H(&5L979E;G1H7SL-"B  <')I=F%T92!45'=E;&9T:"!T=V5L9G1H
M7SL-"B  <')I=F%T92!45&AI<G1E96YT:"!T:&ER=&5E;G1H7SL-"B  <')I
M=F%T92!41F]U<G1E96YT:"!F;W5R=&5E;G1H7SL-"B  <')I=F%T92!41FEF

`
end
Apr 02 2005
parent reply Russ Lewis <spamhole-2001-07-16 deming-os.org> writes:
I like the concept, but have a couple of comments:

1) Perhaps this should be a struct with an opCall rather than a class? 
My impression is that these sorts of Tuples are likely to be used 
by-value, and taht users will want it to be lightweight (otherwise, 
they'll just bite the bullet and use out parameters).

2) I know that it's sort of killing a fly with a sledgehammer, but you 
can simulate implicit template instantiation by overloading a function a 
gazillion times:

Tuple!(float, foat) TupleX(float a,float b)
   { return new Tuple!(float,float)(a,b); }

Obviously, it would only support standard types (plus any number of user 
types that you add), and would get absolutely massive as the tuples get 
to have lots of arguments, but it is technically possible.   Build it 
with a script. :)  Maybe if we have enough pain doing stuff like this, 
it will induce Walter to implement implicit instantiation.
Apr 02 2005
parent "John C" <johnch_atms hotmail.com> writes:
"Russ Lewis" <spamhole-2001-07-16 deming-os.org> wrote in message 
news:d2msva$svr$1 digitaldaemon.com...
I like the concept, but have a couple of comments:

 1) Perhaps this should be a struct with an opCall rather than a class? My 
 impression is that these sorts of Tuples are likely to be used by-value, 
 and taht users will want it to be lightweight (otherwise, they'll just 
 bite the bullet and use out parameters).
Yes, that's more in line with my original idea. I'll do that.
 2) I know that it's sort of killing a fly with a sledgehammer, but you can 
 simulate implicit template instantiation by overloading a function a 
 gazillion times:

 Tuple!(float, foat) TupleX(float a,float b)
   { return new Tuple!(float,float)(a,b); }

 Obviously, it would only support standard types (plus any number of user 
 types that you add), and would get absolutely massive as the tuples get to 
 have lots of arguments, but it is technically possible.   Build it with a 
 script. :)  Maybe if we have enough pain doing stuff like this, it will 
 induce Walter to implement implicit instantiation.
Ah! If I can get my head around how to do that...
Apr 02 2005