Re:Destroy All Ifs – A Perspective from Functional Programming
by TeMPOraL on Saturday July 16, 2016 @06:43PM
(#12108162)
Multiple return values are a great idea IMO, and I learned to really appreciate them in Common Lisp. They're best used as additional bits of information that the programmer may or may not find useful. Like that string comparison example of yours. Or #'gethash[0], that will return the value from hash table you're looking for or a default value (which is optional and by default NIL) if there is no entry with a given key, but it'll also return a second, boolean value that tells you whether your return value was actually found or not - which cleanly solves the problem of storing NILs in hash tables, in the places you care about it.
Multiple return values feel elegant also because the compiler can optimize them away when you're not using them, which is the most common case.
Re:Destroy All Ifs – A Perspective from Functional Programming
by chongli on Saturday July 16, 2016 @08:04PM
(#12108373)
That still has the problem of boolean blindness. The boolean you get back doesn't tell you what it means, you have to go looking for that information.
Re:Destroy All Ifs – A Perspective from Functional Programming
by eru on Sunday July 17, 2016 @02:32AM
(#12109253)
What's the difference between multiple return values and returning a tuple?
(Apart from that languages with multiple return values tend to have some special syntax for binding only the first few members of the returned tuple?)
Re:Destroy All Ifs – A Perspective from Functional Programming
by dllthomas on Saturday July 23, 2016 @12:11AM
(#12148297)
Apart from what you mention, it's often not possible to pass along all of the multiple return values as a single value.
Re:Destroy All Ifs – A Perspective from Functional Programming
by harveywi on Sunday July 17, 2016 @09:32AM
(#12110109)
The difference, in terms of type theory, is that a tuple is a product type [1] but a type representing multiple possible return values (to represent different outcomes) would be encoded using a sum type [2].
Re:Destroy All Ifs – A Perspective from Functional Programming
by eru on Monday July 18, 2016 @06:45AM
(#12114265)
I don't think eg Go uses a sum type:
https://gobyexample.com/multiple-return-values
Typically they use product types to simulate sum types.
Re:Destroy All Ifs – A Perspective from Functional Programming
by tamana on Saturday July 16, 2016 @07:17PM
(#12108254)
The Lua solution is clutter. Why return a boolean just to switch on it and then discard it?
Re:Destroy All Ifs – A Perspective from Functional Programming
by Dylan16807 on Sunday July 17, 2016 @12:15AM
(#12109005)
Why create a lambda just to execute it and then discard it?
Re:Destroy All Ifs – A Perspective from Functional Programming
by eru on Sunday July 17, 2016 @02:34AM
(#12109256)
The problem with the Boolean is rather that people mix up the two values all the time.
The lambda also has more type safety. A Boolean is always a Boolean, but the compiler (and in a dynamic language the runtime) can tell you when you are calling your passed functions with the wrong arguments, because you mixed them up.
Re:Destroy All Ifs – A Perspective from Functional Programming
by titzer on Sunday July 17, 2016 @12:08PM
(#12110582)
Use an enum.
Re:Destroy All Ifs – A Perspective from Functional Programming
by eru on Monday July 18, 2016 @06:43AM
(#12114264)
No need to stop there. Not only use an enum, but also make it bear different types of values for different cases, and you arrive at Algebraic Datatypes. Eg, for trees you can have:
data Tree = Empty
| Leaf Int
| Node Tree Tree
The destroy all IF reminds me of GOTO considered harmful of the 70's. There are other ways to fix the problem.