There are two things here: multiple return and named return. You can have multiple return and de-structuring assignment without named return, e.g. foo() { return (a,b); } x,y = foo(); Many languages implement this. Some go further with and allow de-structuring assignments, like this swap operation: [a, b] = [b,a]. One thing that concerns me about this named return value stuff is that it...
more...
- Ray Cromwell
I love multiple return values, but the named return values seems a bit dodgy. In the example on your page, it looks nice, but I agree with Ray about the implications in a longer function.
- Joey Gibson
The problem with unnamed return values is that you don't know what order to put them in. How do you know if the GetNames function is supposed to be "return firstname, lastname" or "return lastname, firstname"? The solution is to name them so you can have "return {firstname=f, lastname=l}" and not have to worry that you got the order wrong.
- Gabe
That's a reasonable point, but then I would say that you shouldn't just have a 'naked return', but rather something like the syntax you wrote. Of course, it works both ways -- you could get the method parameters in the wrong order as well, so named parameters would help fix that. :)
- Ray Cromwell
Gabe - do you mean that if say "firstname, lastname = GetNames()" it will return "Robert, Felty", and if I say "lastname, firstname = GetNames()" it will return "Felty, Robert"?
- Robert Felty
Rob: Maybe something like "with GetNames() { fn, ln = .firstname, .lastname }"
- Gabe
That makes sense Gabe. What languages currently have a "with"?
- Robert Felty
The "with" statement goes back many decades. Pascal and similar languages (like Modula) have one, but it's more like Javascript's let statement. JS has a with statement, but it's almost too pointless to use. VB's with statement was the one I was approximating, and is probably the best syntax for one. Ada has a with statement, but it's for importing packages, so it's nothing like in the other languages I described.
- Gabe
Hm, Pascal's "with" works exacly like JS's one, AFAIR.
- Alex Kapranoff
Alex, it seems you are right. It's been decades since I've used Pascal, and misremembered.
- Gabe
Also, Python's with statement (new for 2.6) is actually more like C#'s using. Actually, C# has a few different ways of using "using". One way is to import a package into your namespace, like Ada's with statement. Another way is to declare a variable to be initialized at the beginning of a block and make sure it is destroyed at the end of the block, like Python's with statement.
- Gabe
3 cupcakes is all that's left from last night's party