News for hackers. Stuff that matters.
Re:Returning multiple values from functions in C++ by qwertyuiop924 on Monday March 07, 2016 @08:46AM (#11238791)
For a moment, I thought they had added true multiple value return to C++. I am sooo glad they didn't. The lisp folks added it way back, and it has been an ugly scar on the language ever since. Why do real multi-value returns suck? because they don't compose well, they're awkward as all heck to use, and require wrapping certain function calls in really weird constructs. The PROPER way to do multiple values is to return a data structure and pattern match against it. Of course, lisp has support for this, multi-value return was just an optimization hack. WHICH YOU SHOULD NO LONGER USE!
Re:Returning multiple values from functions in C++ by junke on Monday March 07, 2016 @09:40AM (#11239091)
Multiple values are not "just" an optimization hack, they are designed to be convenient to use. I don't know which construct you find "weird". Multiple-value-bind is a descriptive name which is long to type but this does not matter (type m-v-b + auto-complete).

Typical example: you read a line in a file. The line you get is terminated either by a newline character or by the end-of file. The implementation of read-line[1] must know if you reached end of file or not, but most of the time, you don't care about it. Sometimes, you want to know, and then you can take the secondary value (missing-newline-p). I like the fact that I can focus on the primary value without caring about other ones when I want.

[1] http://clhs.lisp.se/Body/f_rd_lin.htm

Re:Returning multiple values from functions in C++ by JoeAltmaier on Monday March 07, 2016 @09:43AM (#11239120)
Sounds like methods could also add special-purpose multiple values later, without impacting existing callers. Which is nice too.
Re:Returning multiple values from functions in C++ by qwertyuiop924 on Monday March 07, 2016 @01:09PM (#11240383)
Maybe this is different in CL, but in scheme, if you're using a procedure that returns multiple values, there is no default return, or wrapping the multiple values into a list: You have to use call-with-values, any number of srfis providing sugar, most commonly srfi-8, although 11 and 71 also exist, and without the sugar, god knows how many lambdas. EVERY. SINGLE. TIME. You want to call a function that returns multiple values.
Re:Returning multiple values from functions in C++ by lispm on Monday March 07, 2016 @02:09PM (#11240813)
You might want to learn Common Lisp BEFORE you make statements about how a feature of the language is obsolete or not.
Re:Returning multiple values from functions in C++ by qwertyuiop924 on Monday March 07, 2016 @02:57PM (#11241166)
I was talking about the general idea of MVR, AND how I thought is was implemented in lisp. I was wrong. This happens sometimes.

And even the new explanation, I STILL don't like MVR. I think that returning a list or other data structure, or just writing two functions, is much clearer, and lends itself better to function chaining. Sure, in CL there's a default, which makes things a little better, but what if you want the other one? I don't want to write

    (multiple-value-bind (a b) (foo bar baz) b)
all over my codebase, and the scheme equivalent is just as bad, if not worse.
Re:Returning multiple values from functions in C++ by junke on Monday March 07, 2016 @03:15PM (#11241279)
You don't have to write it all over the codebase. If you have a specific need, defmacro yourself out of your problem.

Generally speaking, taking a secondary value alone feels like a bad use of MVR (it happens rarely with standard functions and most libraries).

MVR are not intended to be substitute to other data-structures. As said elsewhere, you can and should use structs, classes, lists, hash-tables,...

After all, why do we use multiple values instead of a single struct for functions arguments? Because in CL and other languages, unlike in Haskell, you don't want to define an ADT for all your functions or rely on currying. But if you start putting too many coupled parameters in your functions, it starts to smell fishy.

Extracting a single value is easy, by the way:

  (nth-value 1 (foo bar baz))
Re:Returning multiple values from functions in C++ by lispm on Monday March 07, 2016 @04:12PM (#11241724)
Why don't you take a bit of time to read about multiple values in Common Lisp?

    CL-USER 76 > (nth-value 1 (values 'a 'b 'c 'd))
    B
While you are at it, check out the feature of 'Macros' in Lisp, which allows everyone to write code to shorten this stuff. See my code in this thread for a macro which then allows you to write:

    (multiple-value-bind-some (NIL b)
       (foo bar baz)
      b)
One then can name ignored variables as NIL in the source code...
Re:Returning multiple values from functions in C++ by qwertyuiop924 on Tuesday March 08, 2016 @09:13AM (#11245289)
Good point. And very true. Still looks a bit ugly to me, but it's better than nothing, I suppose. And yes, I am aware of macros. For some reason using them in this situation didn't occur to me, so I guess I am an idiot after all.

This is actually one of the cases where syntax-rules would allow you to write something cleaner, I would guess. I like syntax rules, but everybody always seems to complain about it...

Re:Returning multiple values from functions in C++ by masklinn on Monday March 07, 2016 @09:18AM (#11238949)
It's funny, your comment uses "true MRV" for language-level special-casing by opposition to a matched (or even just python-style unpacked) data-structure, whereas I've always thought the latter was "true MRV".

Either way, we do agree on MRV being a language-level special case being a hack. Even PHP isn't that bad — though the unpacking is macro-ish bleh.

Re:Returning multiple values from functions in C++ by junke on Monday March 07, 2016 @09:43AM (#11239119)
If you wrap your data in a list or a tuple, it is still a single value. It makes sense to talk about true MRV in languages that support it, like Ada, Forth and others... (https://rosettacode.org/wiki/Return_multiple_values)
Re:Returning multiple values from functions in C++ by qwertyuiop924 on Monday March 07, 2016 @01:41PM (#11240610)
Exactly.
Re:Returning multiple values from functions in C++ by lispm on Monday March 07, 2016 @04:24PM (#11241815)
Which is clearly nonsense. Multiple values have been extremely useful and there are several code bases in Common Lisp which make extensive use of it.
Re:Returning multiple values from functions in C++ by qwertyuiop924 on Tuesday March 08, 2016 @09:16AM (#11245304)
Yes. They are useful. So is goto, so are continuations. MVR isn't the unforgivable sin that those things are, but imo, it makes your codebase uglier.
Re:Returning multiple values from functions in C++ by lispm on Tuesday March 08, 2016 @11:21AM (#11246187)
Many Lisp programmers think different.
Re:Returning multiple values from functions in C++ by qwertyuiop924 on Tuesday March 08, 2016 @01:57PM (#11247564)
Well, I'm not many lisp programmers. I'm me. I've got an opinion that I think is reasonable, and I'm willing to hear you out. In fact, I already have, and I disagree. This isn't a problem, it's good to hear differing opinions, although you always sound a bit smug about it. But I could just be reading that in.

Point is, I disagree, and your opinion is fine. 'Kay?