#54 Map syntax

brian Fri 17 Mar 2006

I can probably get maps working in a night (probably tonight).

Although we need to nail down a syntax for the type declaration - it can't be just colon (it has be a "wrapper" syntax like | for Methods). I'm thinking:

Foo[Str] for a map of Foos keyed by Str

The Map literal syntax will be:

["a":Foo.make("a"), "b":Foo.make("b")]

The empty Map will be:

[:]
Foo[Str][:]

Can you guys come up with anything better than that? We might consider using -> instead of : like Ruby and using : for dynamic invoke. Dynamic invoke will probably be used more (maybe).

andy Fri 17 Mar 2006

That's not too bad - not sure about the quotes in the literal, but it makes it more consistent. I don't like the way Ruby does it, too verbose. Besides the arrow operator is more natural for dynamic invoke.

brian Fri 17 Mar 2006

The "quotes" in the example are just a string literal - the key and value are any expression production. If the key was an Int:

[0:"zero", 1:"one"]

Tthe key or value can be any expression:

[user0.username:user0, user1.username:user1]

brian Sat 18 Mar 2006

I checked in support for maps using the syntax from above. Note sure what I think about the map type literal (especially when used with a map constructor):

m := Str[Int][:]
m := Str[Int][0:"zero", 1:"one"]

I have the core methods working and in the test suite:

class Map
{    
  Bool empty()
  Int size()
  V get(K key)
  M set(K key, V val)
  V remove(K key)
  Void clear()
  Str toStr()
  Void each(|V value, K key| c) 
}

andy Sun 19 Mar 2006

Actually, I thought we said maps were going to defined with key:value?

m := Str:Str
m := Int:Str[:]
m := Int:Str[0:"foo", 1:"bar"]

brian Mon 20 Mar 2006

We can't do Key:Val because it doesn't compose - we had talked about that before. For example Foo:Bar[] is ambiguous.

andy Mon 20 Mar 2006

I like Key:Val alot better - but I remember that issue now.

brian Thu 13 Apr 2006

Ok - I changed it back to our original design Key:Val since the current format of Val[Key] sucks. However the formal signature for a parameterized Map is [Key:Val], although I let you leave off the [...] if not needed:

// type signatures
Int:Str         // map of Str keyed by Int
Int:Str[]       // map of Str[] keyed by Int
[Int:Str]       // same as Int:Str
[Int:Str][]     // list of Int:Str
Str:Str:Str     // map of [Str:Str] keyed by Str
Str:[Str:Str]   // same as above
[Str:[Str:Str]] // same as above

// map literals
m := Int:Str[:]
m := [Int:Str][:]

Login or Signup to reply.