Tuples as pairs #1474
andfenastari
started this conversation in
Ideas & suggestions
Tuples as pairs
#1474
Replies: 1 comment
-
@andfenastari Hello! Thanks for your suggestion. One big restriction we have is FFI, we want to remain compatible with Erlang data structures. What Erlang code would we generated for these expresssions? pair(1, 2)
pair(1, pair(2, 3))
pair(1, pair(2, pair(3, 4))) If |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Motivation
A problem I often see on languages with rust-like type systems is the proliferation of "numbered functions", that is, variants of functions operating on heterogeneous collections based on their size, e.g. gleam/dynamics
tuple2
,tuple3
,tuple4
, etc.Functions like these are such a common pattern (I wouldn't be surprised if the iterator module received higher arity versions of the
zip
function at some point in the future) as always involve such amounts of hackery that I think the language ought to support some solution for it, if not the one hereby presented.Proposal
Introduce a new primitive type
pair(a, b)
and make tuple types syntactic sugar for it, so that#(a, b)
meanspair(a, c)
,#(a, b, c)
meanspair(pair(a, b), c)
, etc. This way, thetuple{N}
functions can be replaced by a singlefn tuple(first: Decoder(a), second: Decoder(b)) -> Decoder(pair(a, b))
, which then could be chained with the|>
operator.Another example using iterators:
Further thoughts
Another feature that would combine nicely with this one, but might be too complicated for the language, is the ability to "spread" tuples as arguments to functions, allowing the
decode{N}
functions to be replaced by a combination offn decode(constructor: fn(..args) -> t, decoder: Decoder(args)) -> Decoder(t)
andtuple
, as in:Beta Was this translation helpful? Give feedback.
All reactions