labeled tuple types
See original GitHub issue[@gavinking] Record types are a construct popular in ML-like languages, that are a bit like C-style structs. From our point of view, they are tuples with named elements. An elegant way to add support for record types to Ceylon would be via shared
parameters of class aliases, for example:
class Person(shared String first, shared String last, shared Integer age) => [first, last, age];
Then you could write:
Person person = Person("Joe", "Bloggs", 32);
String firstName = person.first;
String lastName = person.last;
Integer age = person.age;
Where Person
is just an alias for the type [String,String,Integer]
, and person.last
is just sugar for person[1]
.
But would this feature be useful to us? I dunno. It might be useful for working with APIs that query relational databases. Apart from that, I can’t think of much use, since for us classes are just much more powerful.
One use we do have for record types is to encode parameter names to function types. But I don’t think the above approach helps us much there. For that problem we would need to be able to write down types like:
Callable<Anything, [String first, String last, Integer age]>
without needing to define an alias first.
[Migrated from ceylon/ceylon-spec#745]
Issue Analytics
- State:
- Created 10 years ago
- Comments:34 (15 by maintainers)
AFAICT You don’t name tuples with classes. You simply have classes that have named attributes.
This to me departs from the general Ceylon direction of having more (or even “complete”) metadata available at runtime (than e.g. Java)…