Syntax suggestion for tuples and pattern matching.
HI everyone! I recently started thinking about adding tuples and pattern matching in my language. The base syntax is C-like (understand brackets and semicolons). I already have a few special primitives:
- complex
#(xxx)(internally 2 floats on the stack) - polar
$(xxx)(idem) - Vec
@(xxx)(internally 3 or 4 floats on the stack)
What would you use for tuples?
{{xxx}} @{xxx} or anything else?
The language supports (scopable) switches, but I'd like to turn that into pattern matching, maybe
match (exp, other_exp, ...) {
case 1, 2 -> some_func();
default -> other_func();
}
What would you suggest?
Posted Jul 23, 2019, 3:24 PM UTC · edited
The overwhelmingly most common notation for tuples is a comma-separated sequence of values enclosed in parentheses:
(x, y, z). You need a very good reason to deviate from that, and even in that case, your reason is probably not very good. It's generally worth designing the grammar of a language explicitly so that common notation can be supported, unless the point of a language is specifically to gain an advantage by using specialised or uncommon notation (APL and concatenative languages are good examples of such cases).In fact, offhand the only language I can think of that has tuples and does not use this notation is Erlang, where tuples are enclosed in curly braces:
{x, y, z}. This has the advantage of straightforwardly supporting single-element tuples, but it is much less common than using parentheses.Thanks for your answer. I'd rather avoid parentheses, as it is a (parenthesized) list of expressions in gwion. As for curly braces, they are already used for code blocks. I understand that conforming to standards is good, but I think those would lead to too many conflicts in the grammar.
Can code blocks appear in the same context as tuples? If not you can easily tell them apart even if you use curlies.
Nice catch! Lambdas could appear in the same context, but their syntax is quite different
[deleted]
Can't you change that?
TypeScript uses square brackets:
[x, y, z]. I'm not familiar with the reasoning behind that choice.I think it's because they're basically arrays is js/ts
TypeScript is designed to be a superset of JS, and in JS you use square brackets.
Sure, but JavaScript doesn't actually have tuples. You just abuse destructuring to make it look like it does. But I suppose you are still right in that they wanted a syntactically-valid JavaScript program which uses that "feature" to be a tuple in TypeScript.
You definitely have to think a bit differently and make different trade-offs when designing a language intended to have the relationship TS has with JS.
JS uses array literals for the kind of things you would use tuples for in other languages. For example, Map (dict) initialization takes an iteratable of length 2 arrays.
Personally I prefer that syntax, as tuples are basically just heterogenous arrays, so having them share square bracket syntax and using type inference to choose between them is IMO pretty ideal.
I think the fact that you can't infer the type on its own is the biggest down side. For example in
let x = [y, z], what should be the type ofx? Typescript chooses it to be an array which I find inconvenient.Haskell takes a much more elegant approach to this problem via typeclasses and bidirectional type inference. You can see it in action with number literals, and with
OverloadedLiteralsandOverloadedStrings(although they need a bump in power to be able to handle HLists/HArrays).If the above is defined
zwill have a polymorphic type based on theIsListtypeclass, andaandbwill have type[Foo]andBoolrespectively.Now if you convolute things a bit to something like:
Haskell will simply reject it without an explicit higher rank type signature, but the following will work as expected:
By bumping the power of the
IsListtypeclass to something that is parameterized by a list of types rather than a single element type, you can get it to support types likeTuple [Int, Bool]as well.[deleted]
No, not really, unless the cost is very low.
I would say it depends on how expressive your language is, specifically when it comes to tuples.
If you have a lot of expressivity and tuples are more like heterogenous arrays that support things like mapping a polymorphic function over all elements and concatenating tuples and so on, then I would say you should definitely support single element tuples.
If your language is simple and relatively inexpressive then they are much less needed.
In Erlang specifically, most functions that have multiple return options return tagged tuples, where the first element is the tag. Because Erlang is not statically typed, you would pattern match on the number of elements in the tuple. So you could distinguish between pattern matching an arbitrary tuple and an arbitrary 1-ary tuple.
ex. example(N) when N < 0 -> { error_neg }; example(0) -> { error_zero }; example(N) -> { ok, N * 2 }.
In a statically typed language it’s not worth it but it can make some code more idiomatic.