re 1) To my mind the point of tuples is preserving the exact Types and all the benefits that flow from that
2) Literal Syntax
Yeeeees - following python works and is familiar but against their choice is the possibility of (visual/reading) confusion with braced expressions or comma separated lists ( as in method invocations) ,
IIRC In python theres some unexpected syntactic weirdness to distinguish between a list of expressions vs a tuple ( something really obvious like use of a trailing comma from memory )...
Seems to me that a Tuple is a most like a List (wrt cobra and existing literals) - except it is immutable and its items are strongly individually typed , that seems to suggest some permutation of List literal ([])
In my notes I've been using something like *[] ( which sorta has a relationship to streams as well) or (analogous to the string variants) a single char prefix to delineate from Lists
e.g. t[] (or T[]) - (t/T for
tuple)
l = [a,b,c]
# l is a List of gcd type of a,b,c
tupl = *[a,b,c]
# OR
tupl = t[a,b,c]
# tupl is a tuple of 3 items Typed to Types of a,b,c
In above most likely to be constructing using literals anyway
l = [1,"mike",33.2]
# l is a List of (probably Object/dynamic)
t = *[1,"mike",33.2]
t = t[1,"mike",33.2]
# t is a Tuple<int, String, Decimal>
Also theres the idea of a
named tuple (or data Structure) where the individual items have names and the tuple items can be tagged and accessed by name as well as position:
here I'm thinking [:] ( or *[:], t[:] ) where the name precedes the value - analogous to Dict/Maps
namedTuple = [id:1, name:'mike', avTemp:33.2]
assert namedTuple[id] ==1
assert namedTuple[name] =='mike'
assert namedTuple[avTemp] == 33.2
(alternatively the names could be explicitly strings like a Dict/Map<String:??>
nt = ['id':1, 'name':'mike', 'avTemp':33.2]
assert nt['name'] == 'mike'
Hmm assuming most useful/common use is the named tuple variant (??) then perhaps unnamed should also use ':' explicitly
emptyTuple = [:] # what does this mean ??
namedTuple = [ id:1, name:'mike', avTemp:33.2]
assert namedTuple[2] == namedTuple[name] # or assert namedTuple[2] == namedTuple['name']
anonTuple = [:1, :'mike', :33.2]
assert anonTuple[2] == 'mike'
that would allow us to distinguish between List and Tuple without a prefix char on Tuples...
3) Operations
- indexing by position (and name for named Tuples)
- count/length
- Enumerability
- slicing - slicing a Tuple (by position only) gives a subset tuple
t = t[1, 'mike', 33.2]
t1 = t[:1]
assert t1[0]==1 and t1[1] =='mike'
assert t1[0].typeOf == t[0].typeOf and t1[1].typeOf == t[1].typeOf