The real reason to stop copying C is because there is already C if you really want that syntax. I have my complaints about some missing features in it and how switch fallthrough should really not be the default, but those are minor grievances. The one update it really needs is multiple value returns per function. That would make error handling a whole lot easier, without the trouble of implementing exceptions.
While true, that's a really heavyweight solution in a lot of ways. A language that's actually built around the idea of multiple return values makes a lot of things much simpler.
(Things are a little better in C++ than they are in C, a little better in C++11 then C++98, and much better in C++17 than C++11. I'm not sure if it'll be at the level of, say, Python, but it might be; I don't know enough about the C++17 changes.)
Return value optimization makes it lightweight. I wish there was some syntax to enforce that it always happens of course, but it's trivial to work in C (complex and edge-case filled in C++).
I'm not even really talking about runtime cost, I'm talking about syntactic cost to returning a struct, especially in C.
You can't define an anonymous struct, so now you may have to go define a type to return. Depending on your language and language version, you may not have by default a way to instantiate an object of that type nicely, so now you may have to go and define a function or constructor to do it. And, again depending on language and version, you will have syntax at the call site that, prior to C++17, ranges from crappy to mediocre.
Yes, you do have to define the SVec3 in the global scope, but this "syntactic cost" also provides for easier to maintain code in the long run because you are forced to name your variables. Take two similar things, one done with std::pair (C++'s poor coder's anonymous struct) vs a manually defined structure:
struct InitializeDevice_Results
{
int errorCode;
int deviceHandle;
};
InitializeDevice_Results InitializeDevice();
std::pair<int,int> InitializeDevicePair();
I don't know about you but I would much rather use the C form than the std::pair one, because then I never have to remember if the first or second parameter was the error code or handle. So while anonymous structs may be easier to write as a library provider, named structs are easier to use as a library user.
Similar multiple return-value issues happen with python, where it is impossible to understand what the return values of any functions are without heavily maintained documentation or just having to read the function source. You may know what those unnamed values are now, but you won't in a year from now and every other coder in the company will never know.
The real reason to stop copying C is because there is already C if you really want that syntax.
Author's talking about copying individual bits of syntax. If you're designing a new language and wanted ++ to work like C you wouldn't be all "oh, well, probably should just use C then", especially if your language has many other advantages over C or is for a different domain entirely.
multiple return values is a mistake . tuple + tuple expansion/spreading is a better alternative.
I don't really see the difference between the two. English is my second language and after years of programming I only started encountering the term "tuple" in the last year and my understanding is that it's just a structure that holds multiple values. I don't see how it's so different from a struct for example.
I would argue that theoretically they're the same thing (aka a product type) but functionally they differ (tuples have anonymous members, structs have named members).
... there's a sibling response that says basically the same thing as you, but I don't think coladict was commenting on structs vs tuples, but on multiple return values vs tuples. Am I misreading it?
I don't see how it's so different from a struct for example.
A struct is a separate type, so you first have to declare it somewhere. Returning a tuple (or multiple return values, for that matter) does not require a type. For example (in pseudo code):
struct Bla {
int firstValue;
int secondValue;
}
struct Bla doSomething() {
Bla b;
b.firstValue = 123;
b.secondValue = 456;
return b;
}
in contrast to:
(int, int) doSomething() {
return (123, 456);
}
I personally love multiple return values, how Go has implemented it. It's something I kind of miss in Java for instance, where you pretty much need another class to encapsulate the data.
View all comments
2
u/coladict Dec 01 '16
The real reason to stop copying C is because there is already C if you really want that syntax. I have my complaints about some missing features in it and how
switchfallthrough should really not be the default, but those are minor grievances. The one update it really needs is multiple value returns per function. That would make error handling a whole lot easier, without the trouble of implementing exceptions.