Skip to content
This repository has been archived by the owner on Feb 19, 2018. It is now read-only.

CS2 Discussion: Features: Literal-notation for Map-Types #43

Closed
Asc2011 opened this issue Sep 23, 2016 · 5 comments
Closed

CS2 Discussion: Features: Literal-notation for Map-Types #43

Asc2011 opened this issue Sep 23, 2016 · 5 comments

Comments

@Asc2011
Copy link

Asc2011 commented Sep 23, 2016

I truly welcome the arrival of Set- and Map-types. The more i use esp. maps i'd like to use a literal for map-types. One can initialize a Map using a nested-array

m = new Map [
  [ 1, 'one' ],
  [[2]], 'two'
 ]

but thats really ugly. In an ideal world it could be written like a object-literal

m = new Map ( 1: 'one', [2]: 'two' ) or simply

m = 
   1: 'one'
   [2]: 'two'

as this might seem to be ambiguous, maybe with a type-hint

m = 1:'one', [2]:'two' as Map
m<Map> = 1:'one', [2]:'two' 

Such a type-hint might serve as a solution to the for-loop-problem on iterables as well. If no hint was given, CS would assume a object in case of for-of-loop. If a hint was given then translate into a for (var [k, v] of myMap)-style-loop for a map-type or for (let elem of mySet)

for k,v of m<Map>
  ...
for k,v of m as Map
  ...

s = new Set [1,2,3,4]
for member in s<Set>
  ...

I'd love to see member-access via bracket-notation naturally like so

m = 1:'one',  [2]:'two', 'three':3 as Map
console.log m[1]
# => 'one'
i='three'
console.log m[i]
# => 3

I was surprised to learn that Gorillascript has already introduced a literal-notation for 'first-class-members' set- and map-types. It uses the ampersand to distinguish objects from maps

let map = %{
  [obj]: 1
  [other]: "hello"
}

In an ideal world one could do destructuring-assignments on map-types, the same way it can be done with objects.
Since the profiling-results on map- and set-types that float around on the web are not convincing, no literal-notation is avail, no bracket-notation and no destructuring at the moment there is not much reason to prefer maps instead of objects, unless one wants to use objects as keys of the map. A literal-notation for map-types might change this a bit.
This is not a proposal for types in CS thru the backdoor - but maybe we could stick to the for-loops as they are by using such a tiny annotation ?

@mitar
Copy link

mitar commented Dec 11, 2016

Could this be combined with YAML support #59? Does YAML have a concept of a map?

@edemaine
Copy link

The trouble is that there are two notions of key-value "maps" in ES6, objects and Map objects.

@mitar YAML has notation for key-value maps, but it's exactly what CS uses for objects, namely

key1: value1
key2: value2

YAML does offer explicit typing via !; a Map might be represented as

!Map
key1: value1
key2: value2

I've never found this particularly pretty notation, but it's an option...

To me, a natural syntax for Map literals would be the following.

x = new Map
  stringKey: value1
  [object]: value2

This would get translated to

x = new Map([['stringKey', value1], [object, value2]])

(or perhaps something more efficient)

By contrast,

x = new Map
  stringKey: value1
  [object]: value2

is getting translated to

x = {'stringKey': value1, [object]: value2}

which stringifies object. But maybe that's OK?

@connec
Copy link

connec commented Dec 12, 2016

It would be pretty interesting to 'adopt' YAML's tagging syntax for structures and sequences, then a Map (or even an immutable.Map, etc.) could be written as:

!Map
  stringKey: value1
  ? object # using canonical mapping syntax
  : value2

@GeoffreyBooth
Copy link
Collaborator

Closing due to lack of interest.

@coffeescriptbot coffeescriptbot changed the title Literal-notation for Map-Types CS2 Discussion: Features: Literal-notation for Map-Types Feb 19, 2018
@coffeescriptbot
Copy link
Collaborator

Migrated to jashkenas/coffeescript#4939

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

No branches or pull requests

6 participants