4 ms·
I don't understand why modern languages still use the "mutli-value context" in place of tuples. When I first saw it in Matlab, I thought it was a mistake, and s
by tomp 13y ago
I don't understand why modern languages still use the "mutli-value context" in place of tuples. When I first saw it in Matlab, I thought it was a mistake, and seeing it in Go and Julia reinforced this belief. IMO, tuples are superior in almost every way, as they are first-class, i.e. they act like any other values. They also avoid such sad patterns:
record MyInt (value:Int);
// delegate any function called with a MyInt to be called on its Int value
[F] overload F(x:MyInt) = ..F(x.value);
where `..` must be used to extract multiple values in a single-value context. This could easily avoided using tuples.
The only advantage of Matlab-style multiple values is that they allow a kind of return-value type overloading, i.e. some Matlab functions perform less work if you don't only consume 1 return value. But I believe that this could be easily solved with a first-class code pattern.
- codebaobab 13y agoMaybe modern languages don't have tuples because modern programming language designers don't have any|much|enough experience with tuples? If I wanted to play with tuples, what language(s) would I have to use?
- oblique63 13y agowhat about python?
- tomp 13y agoPython, or any of the functional programming languages: OCaml, Haskell, probably Scala. Tuples are really just syntactic shortcut for immutable lists, so Clojure would be appropriate as well, even though it makes no distinction between a tuple and a list (e.g. "rest" parameters to a variadic function are a list).
- lobster_johnson 13y agoWhat exactly do you mean by "multi-value context" and indeed "tuple" in this case? A tuple is traditionally an ordered list where the positions may or may not be assigned a name. When we assign names to the positions, we tend to call them "records". In other words the tuple [30, "Bob"] and the record {age: 30, name: "Bob"} are equivalent. One language that prominently relies on tuples with unnamed fields, in the absence of a built-in record syntax, is Erlang. The downside to not having names is that to find a value you must know its position, which hides information in the implementation (refactoring the code in a way that changes the position, for example, might not result in a compile-time error), which is one reason that Haskell provides syntactic sugar for expressing tuple types using a record syntax. One thing that Go does right is to sometimes allow you to address a record as a tuple: var x SomeRecord = {1, "Bob"} However, when reading, I believe (I haven't actually checked) you can't access a record variable as a tuple.
- tomp 13y agoBy "tuple", I mean a first-class object that has multiple elements; an immutable list of sorts (and by "first-class" I mean an object that can be stored into a variable). E.g. in python: key, value = {'a': 1}.iteritems().next() I could also do: item = {'a': 1}.iteritems.next() key, value = item which shows that tuples are first-class. By "multi-value context" I mean treating expressions differently depending on their context; some contexts are then "multi-value". E.g. in go: v, ok = a[x] Here, if `x` is present in `a`, `v` is assigned the value at `x`, and `ok` is `true`; otherwise, `ok` is `false`, and `v` is assigned the zero value of its type. Note that I cannot do this: pair = a[x] v, ok = pair as `pair = a[x]` has a different meaning in this context: it only returns the value at `x` (or the zero value), and gives no indication of whether `x` is present or not. So, the meaning of the expression `a[x]` changes depending on the context, which can be either "single-value" or "multi-value".
- KenoFischer 13y agoNote that this does work just fine in Julia however since tuples are first class objects ;)