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? 8 5148 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
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 :-) 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 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.
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();
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 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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()
|
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:...
|
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...
|
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...
|
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...
|
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:
|
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...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |