www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Multi typed value return.

reply Matthew Ong <ongbp yahoo.com> writes:
Hi D developer,

Any plan to support this really cool feature of Multiple type
return value?
http://golang.org/doc/effective_go.html#multiple-returns

In Java and C++ we need to use the HashMap to do this and does not have
compiler checking and need runtime casting checking?

func (file *File) Write(b []byte) (n int, err Error) // different type.
func nextInt(b []byte, i int) (int, int) // same type
func nextInt(b []byte, pos int) (value, nextPos int) // same type.

These ability in D will be useful to reduced the amount of extra runtime
casting and checking a function.

I am aware of this in/out parameter like in the MS SQL/PL/SQL.
However that would meant that I would need to create all those variables
in stack before I call updateRow.
//void updateRow(int out myNum, string out myStr);

Where else if D allows :
// function updating the collection table and returns the number of rows
updated and also all the keys updated.
(int changeCount, string[] autoId ) updateRowA(int myNum, string myStr);


That would allow the caller to only create the variable needed in the stack
post calling the updateRowA.
Hope this is a good idea?

Please share what u think.
Matthew Ong.
May 12 2011
next sibling parent reply Adam Ruppe <destructionator gmail.com> writes:
Look at http://dpldocs.info/std.typecons.Tuple

Note though that D isn't a kitchen sink - features aren't added to
the language just because. More often than not though, the existing
features can do what you ask for if you combine them well.

Spend some time working with the language and you'll probably find
it's more than adequate for real work.
May 12 2011
parent Matthew Ong <ongbp yahoo.com> writes:
Hi Adam,

Hey. Thanks for that sample from an experience developer. That save me lot of
time
for figuring them out.

That is a useful work around that does not need me to create a class/struct for
a
simple purpose to define and call a function to return this manner.
May 12 2011
prev sibling next sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On 2011-05-12 04:55, Matthew Ong wrote:
 Hi D developer,
 
 Any plan to support this really cool feature of Multiple type
 return value?
 http://golang.org/doc/effective_go.html#multiple-returns
 
 In Java and C++ we need to use the HashMap to do this and does not have
 compiler checking and need runtime casting checking?
 
 func (file *File) Write(b []byte) (n int, err Error) // different type.
 func nextInt(b []byte, i int) (int, int) // same type
 func nextInt(b []byte, pos int) (value, nextPos int) // same type.
 
 These ability in D will be useful to reduced the amount of extra runtime
 casting and checking a function.
 
 I am aware of this in/out parameter like in the MS SQL/PL/SQL.
 However that would meant that I would need to create all those variables
 in stack before I call updateRow.
 //void updateRow(int out myNum, string out myStr);
 
 Where else if D allows :
 // function updating the collection table and returns the number of rows
 updated and also all the keys updated.
 (int changeCount, string[] autoId ) updateRowA(int myNum, string myStr);
 
 
 That would allow the caller to only create the variable needed in the stack
 post calling the updateRowA.
 Hope this is a good idea?
D is not at a stage where we're looking to implement new features. We're looking to stabilize the language as it is and fully implement the few features which have been introduced but not fully implemented. So, don't expect new features to be added to the language any time soon. The current version of the language is D2. Once D2 has stabilized and is completely production-ready, we may begin on a new version of the language which introduces new features, but that's a ways down the road. For now, if you want multiple return values, the closest that you're going to get is either using out or ref function parameters to return values via the arguments to a function or to return a std.typeconse.Tuple of values. - Jonathan M Davis
May 12 2011
prev sibling parent reply bearophile <bearophileHUGS lycos.com> writes:
Matthew Ong:

 Any plan to support this really cool feature of Multiple type
 return value?
Multiple return values is a very handy feature, I use it quite often in Python. It makes the code more natural and readable, and avoids some troubles given by "out" arguments. In the D standard library there is a struct-based Tuple. So what's desired is just syntax sugar for the unpacking of a Phobos Tuple, useful for tuples in general, tuples returned by functions, and unpacking of tuples in the foreach, like in foreach((x,y); foo()). We have discussed about tuple unpacking this some time ago and I think Walter & Andrei don't hate the idea of this syntax sugar, but I presume it's not for D2. Bye, bearophile
May 12 2011
parent Matthew Ong <ongbp yahoo.com> writes:
On 5/13/2011 12:47 AM, bearophile wrote:
 Matthew Ong:

 Any plan to support this really cool feature of Multiple type
 return value?

 Multiple return values is a very handy feature, I use it quite often in
Python. It makes the code more natural and readable, and avoids some troubles
given by "out" arguments. In the D standard library there is a struct-based
Tuple. So what's desired is just syntax sugar for the unpacking of a Phobos
Tuple, useful for tuples in general, tuples returned by functions, and
unpacking of tuples in the foreach, like in foreach((x,y); foo()). We have
discussed about tuple unpacking this some time ago and I think Walter&  Andrei
don't hate the idea of this syntax sugar, but I presume it's not for D2.

 Bye,
 bearophile
Yes. Thanks for the Tuple and yes. That will do the job for the time being. It does beat manually creating a struct and change that later also. -- Matthew Ong email: ongbp yahoo.com
May 16 2011