472,951 Members | 1,830 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,951 software developers and data experts.

multiple return values (new syntax proposal)

Hello all,

I have been thinking about a possible extension to C/C++ syntax.

The current syntax allows declaring a function that returns a value:

int foo();

however, if I were to return more than one value, for example three
int-s, I would have to change my "logic" and pass the references to my
return values (i.e. declare them in the argument list) or to declare an
extra-struct representing the return type of three ints (extra work and
extra types). The function would look something like:

int foo(int&,int&);
or
void foo(int &, int&, int&);
or
struct rettype
{
int a, b, c;
};

rettype foo();

Would it not be nice if I could keep the thinking "return values are on
the left-hand side" and do something like this:

----
declaration:

{int, int, int} foo();
----
definition:

{int, int, int} foo()
{
...
return {1, 42, 0};
}
----
code:

int a, b, c;
{a, b, c} = foo();
----
I picked the curly braces, but it could be something else, it doesn't
matter - I am talking about the principle.

What do you think?

Sep 8 '06 #1
8 5114
al******************@gmail.com wrote:
I have been thinking about a possible extension to C/C++ syntax.

[..]
int a, b, c;
{a, b, c} = foo();
----
I picked the curly braces, but it could be something else, it doesn't
matter - I am talking about the principle.

What do you think?
What problem does it solve that cannot be solved by returning a struct?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 8 '06 #2
It is not solving any particular problem that can not be solved with
current syntax. It just, in my opinion, makes the way of thinking about
returned values more consistent: they are on the left-hand side of the
function decl.

I would, of course, not replace passing by ref or other methods, just
add this one. It would make already beautiful language even more
beautiful :-)

Sep 8 '06 #3
al******************@gmail.com schrieb:
I have been thinking about a possible extension to C/C++ syntax.

The current syntax allows declaring a function that returns a value:

int foo();

however, if I were to return more than one value, for example three
int-s, I would have to change my "logic" and pass the references to my
return values (i.e. declare them in the argument list) or to declare an
extra-struct representing the return type of three ints (extra work and
extra types).
The extra struct could be a nice template, like std::pair or boost::tuple:

http://www.boost.org/libs/tuple/doc/...ers_guide.html

With boost::tie, you can even unpack the pair or tuple into single variables.

--
Thomas
http://www.netmeister.org/news/learn2quote.html
Sep 8 '06 #4

al******************@gmail.com wrote:
Hello all,

I have been thinking about a possible extension to C/C++ syntax.

The current syntax allows declaring a function that returns a value:

int foo();

however, if I were to return more than one value, for example three
int-s, I would have to change my "logic" and pass the references to my
return values (i.e. declare them in the argument list) or to declare an
extra-struct representing the return type of three ints (extra work and
extra types). The function would look something like:

int foo(int&,int&);
or
void foo(int &, int&, int&);
or
struct rettype
{
int a, b, c;
};

rettype foo();

Would it not be nice if I could keep the thinking "return values are on
the left-hand side" and do something like this:

----
declaration:

{int, int, int} foo();
----
definition:

{int, int, int} foo()
{
...
return {1, 42, 0};
}
----
code:

int a, b, c;
{a, b, c} = foo();
----
I picked the curly braces, but it could be something else, it doesn't
matter - I am talking about the principle.

What do you think?
I think that you should also have the possibility that the function
need not supply all of the return values. In that case, the return
values have to have default initializers;

{int, int = 42} foo() { return 3; } // returns 3, 42

The callers should not be required to capture all of the return values.
Presently, you can call a function and ignore the result. Multiple
return values would preserve this freedom:

foo(); // 3, 42 thrown away

int x = foo(); // x = 3

{ x } = foo(); // x = 3

It might be worthwhile to look at Common Lisp multiple values.

Here is a possible alternative syntax:

// group together destinations for multiple values
// mnemonic: "into"
>{ lvalue ... }
// capture multiple values emanating from expression
// mnemonic: "extract"
<{ expr }

The >{ and <{ could be recognized as tokens, which gets rid of
ambiguities against compound statements.

This would allow for these uses:

// foo takes three arguments
// bar yields three values
// here, bar's three values become arguments for foo:
// analogous to Common Lisp multiple-value-call.
foo(<{bar()});

// here quux is a one argument function
// it takes only the first return value of bar
quux(bar());

// assignment of four values to four lvalues
>{x, y, z.field, *p} = <{returns_four_values()};
The >{ } could be permitted to enclose a declaration, allowing for the
binding of a group of variables to multiple values:
>{ unsigned int x, y; double z = 42 } = <{ func(arg); }
Here, z gets 42 if the function returns only three values, otherwise it
takes the fourth value.

Sep 8 '06 #5

al******************@gmail.com wrote:
declaration:

{int, int, int} foo();
How about:

std::tr1::tuple<int, int, intfoo();

Sep 8 '06 #6
I was not trying to find a way to return more than one value (my
"struct" example is functionally equivalent to using tuple template).
My idea was to increase the clarity of the written code.

Additionally, the "extension to the syntax" I talked about would be
applicable to C as well.

Noah Roberts wrote:
al******************@gmail.com wrote:
declaration:

{int, int, int} foo();

How about:

std::tr1::tuple<int, int, intfoo();
Sep 9 '06 #7
al******************@gmail.com wrote:
Hello all,

I have been thinking about a possible extension to C/C++ syntax.

The current syntax allows declaring a function that returns a value:

int foo();

however, if I were to return more than one value, for example three
int-s, I would have to change my "logic" and pass the references to my
return values (i.e. declare them in the argument list) or to declare an
extra-struct representing the return type of three ints (extra work and
extra types). The function would look something like:

int foo(int&,int&);
or
void foo(int &, int&, int&);
or
struct rettype
{
int a, b, c;
};

rettype foo();

Would it not be nice if I could keep the thinking "return values are on
the left-hand side" and do something like this:

----
declaration:

{int, int, int} foo();
----
definition:

{int, int, int} foo()
{
...
return {1, 42, 0};
}
----
code:

int a, b, c;
{a, b, c} = foo();
What would be the meaning of

{ ignored, b, ignored } = foo();

Is that supposed to yield undefined behavior? If so, I would need to have
n-1 dummy variables if I want to ignore all but one returned value.

Also, where in the type system do you put these triples? I.e., if I have

template < typename T >
void bar ( T const & t ) {
...
}

and do

bar ( foo() );

what is the type T that will be chosen? If it is some tuple-type, would the
proposed meaning of

{ a, b, c } = foo();

really be consistent with our expectations? I would read that as:

create a tripple temporary from the values a, b, c
assign foo() to that temporary overwriting the old values
throw the temporary away

The meaning of {1,42,0} in your return statement seems to be exactly that:
form a temporary triple from 1, 42, and 0; then return the temporary.
>

----
I picked the curly braces, but it could be something else, it doesn't
matter - I am talking about the principle.

What do you think?
I think your proposal does not interact nicely with the type system of C++.
Best

Kai-Uwe Bux
Sep 10 '06 #8
al******************@gmail.com wrote:
I was not trying to find a way to return more than one value (my
"struct" example is functionally equivalent to using tuple template).
My idea was to increase the clarity of the written code.
This type of constructions will be very difficult to implement in C++
without a big redesign of the language. And in order to have any hope to be
evaluated by the standard commitee, you must do a lot of work to
demonstrate that can be done without interfering with any existing syntax
or semantic (preferably by providing a working implementation). I suggest
you to read "The design and implementation of C++", It has a good
explanation of the process of proposing and accepting extensions.

--
Salu2
Sep 10 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Chris Barker | last post by:
Hi all, We've been having a discussion over on the wxPython-users mailing list about how to deal with multiple versions of wxPython. During the discussion it came up that this isn't a problem...
28
by: Fábio Mendes | last post by:
I'm sorry if it's an replicate. Either my e-mail program is messing with things or the python-list sent my msg to /dev/null. I couldn't find anything related in previous PEP's, so here it goes a...
66
by: Darren Dale | last post by:
Hello, def test(data): i = ? This is the line I have trouble with if i==1: return data else: return data a,b,c,d = test()
3
by: jason | last post by:
How does one loop through the contents of a form complicated by dynamic construction of checkboxes which are assigned a 'model' and 'listingID' to the NAME field on the fly in this syntax:...
4
by: Leslaw Bieniasz | last post by:
Cracow, 20.09.2004 Hello, I need to implement a library containing a hierarchy of classes together with some binary operations on objects. To fix attention, let me assume that it is a...
9
by: Code4u | last post by:
My colleagues and I have been discussing techniques for implementing interfaces in C++. We're looking for a mechanism similar to COM's QueryInterface, in which a certain types of objects can be...
12
by: Dennis D. | last post by:
Hello: I want a function to return three variables to the calling procedure: Private Function CalcTimes(ByVal iAddDays as Integer, ByVal iAddHours as Integer, ByVal iAddMins as Integer) As...
5
by: Jason | last post by:
Is there a mechanism in VB.NET that allows something like: If myVar In ("A","B","C") Then... The way I'm doing it now is: Select Case myVar Case "A","B","C" Or like this:
34
by: glomde | last post by:
i I would like to extend python so that you could create hiercical tree structures (XML, HTML etc) easier and that resulting code would be more readable than how you write today with packages like...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.