Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a new syntactic category: tuples #131

Closed
BurntSushi opened this issue Feb 26, 2013 · 7 comments
Closed

add a new syntactic category: tuples #131

BurntSushi opened this issue Feb 26, 2013 · 7 comments

Comments

@BurntSushi
Copy link
Member

Regardless of whether issue #28 is accepted, arrays can only contain values of a single data type. While data types can still be mixed at the hash level, it may still be desirable to compactly represent an array of mixed data types. We can do this with tuples. So that while this is illegal

usergroups = [[0, "root", "root"], [1, "bin", "root"]]

with tuples, it could be represented as

usergroups = [(0, "root", "root"), (1, "bin", "root")]

In particular, the type of the above is an array of int * string * string if #28 is accepted, or simply array in the current spec.

The semantics of a tuple is that its type is the product of the types of its elements. This not only makes it an ordered type, but it fixes the length as well. So that this would not be allowed:

something = [("a", "b"), ("x", "y", "z")]

since the first tuple has type string * string and the latter has type string * string * string.

I also propose that tuples with less than 2 elements be forbidden. Any attempt to create them should result in an error.

The actual syntax of a tuple is precisely the same as an array, except with () instead of []. Tuples may contain arrays and arrays may contain tuples.

@ibotty
Copy link

ibotty commented Feb 26, 2013

+1

@goto-bus-stop
Copy link

Tuples may contain arrays

So this is okay?

toplevel = [
    (1, 2, ["a", "b"]),
    (3, 4, [3.64, 21.25]),
    (5, 4, [ [ "b" ], [ 1 ], [ 0.0, 0.0, 12.24 ] ])
]

@BurntSushi
Copy link
Member Author

@renekooi That would be OK given the current spec, since all arrays have type array. If the proposal in issue #28 is accepted, then that wouldn't be allowed.

@axelarge
Copy link
Contributor

This would be awesome together with #28. It would simplify the implementation and API for statically typed languages

@mojombo
Copy link
Member

mojombo commented Feb 27, 2013

I rather like the notion of tuples. It solves the mixed type array problem and offers a nice way to specify large amounts of homogenous, structured data in an easy way. Almost every language will be able to express tuples in some kind of reasonable way, allowing us to go back to fully homogenous arrays, which I would prefer.

@tnm I'd love your take on this.

@tnm
Copy link
Contributor

tnm commented Feb 27, 2013

I've been thinking this over since the mixed array type discussion started. I'm in favor of tuples. There's significant practical use — the user example above is perfect — and implementations should be straight-forward.

@mojombo
Copy link
Member

mojombo commented Mar 1, 2013

Spec'd out in #154. Continue discussion there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants