New Language Addition: Dictionary Literals

6 views
Skip to first unread message

Francisco Tolmasky

unread,
Dec 29, 2009, 4:35:32 PM12/29/09
to Cappuccino & Objective-J Development List
This new language addition is pretty straight forward and I believe
will be a great relief to everyone: the addition of a literal syntax
for dictionaries. Currently to make a dictionary you have to do
something like this:

[CPDictionary dictionaryWithObjects:___ forKeys:___];
or
x = [CPDictionary dictionary]; [x setObject:___ forKey:___];
or
[CPDictionary dictionaryWithJSObject:____];

The idea is to provide a new alternative that looks very similar to
JSON:

theDictionary = @{ key:object, key2:object2 };

This would be 100% equivalent to [CPDictionary dictionaryWithJSObject:
{ key:object, key2:object2 }]; In fact, the compiler would just turn
it into this. To have inner dictionaries just continue using the
syntax:

@{ key:object, key2:object2, key3:@{ key:object } };

The dictionary created will still be accessible with objectForKey: and
allow mutation with setObject:forKey:, but I think this will make a
big difference psychologically since dictionaries feel "heavy" right
now, just due to the verbosity of creating them.

Feel free to chime in.

Danny Greg

unread,
Dec 29, 2009, 5:04:17 PM12/29/09
to Cappuccino & Objective-J Development List
I think this is a great idea, have you thought much about adding some
syntax for quick value accessing?

Also how about extending this to arrays where it just becomes @
{object, object, object}. Once the precedent has been set it seems
sensible to extend it to arrays.

Ross Boucher

unread,
Dec 29, 2009, 5:14:59 PM12/29/09
to objecti...@googlegroups.com
Well, Arrays and CPArrays are already the same thing. There's no need
for a special syntax.

> --
>
> You received this message because you are subscribed to the Google
> Groups "Cappuccino & Objective-J Development List" group.
> To post to this group, send email to objecti...@googlegroups.com.
> To unsubscribe from this group, send email to objectivej-de...@googlegroups.com
> .
> For more options, visit this group at http://groups.google.com/group/objectivej-dev?hl=en
> .
>
>

Tom Robinson

unread,
Dec 29, 2009, 6:11:35 PM12/29/09
to objecti...@googlegroups.com
Do we really need syntax? How about just a short function that converts a JS object to a CPDictionary:

function CPDict(object)
{
var keys = Object.keys(object);
var values = keys.map(function(k){ return o[k]; }); // or whatever
return [CPDictionary dictionaryWithObjects:values forKeys:keys];
}

CPDict({ "foo" : "bar" })

etc.

Francisco Tolmasky

unread,
Dec 29, 2009, 6:31:45 PM12/29/09
to Cappuccino & Objective-J Development List
Well, we do already have dictionaryWithJSObject: (which you wrote), so
if we were to just add a top level function like CPDict, we would
probably use that instead of functions that aren't implemented yet
like keys and map:

function CPDict(JSObject) { return [CPDictionary
dictionaryWithJSObject:JSObject]; }

But I suppose that is an implementation detail.

The idea behind the syntax is that I think people "feel" like they're
using something heavier than they really are. By adding a syntax
feature it feels more like a base class object on par with Array. For
example, I feel people don't use Sets and OrderedSets, despite being
the appropriate data structure in many cases, simply because its so
easy to create arrays and arrays "feel" so cheap to create.

The other nice thing about syntax is that we could extend JSON in the
same way. People can pass around JSON that includes all the
fundamental data structures of Objective-J. Today this is ambiguous,
your choices without doing significant work yourself are:

1. Turn all {}'s in JSON into dictionaries
2. Turn only the top most {} in JSON into a dictionary
3. Don't turn any {}'s in JSON into dictionaries.

@{} would allow you explicitly differentiate dictionaries from js
objects.

Tom Robinson

unread,
Dec 29, 2009, 6:46:37 PM12/29/09
to objecti...@googlegroups.com

On Dec 29, 2009, at 3:31 PM, Francisco Tolmasky wrote:

> The other nice thing about syntax is that we could extend JSON in the
> same way. People can pass around JSON that includes all the
> fundamental data structures of Objective-J. Today this is ambiguous,
> your choices without doing significant work yourself are:
>
> 1. Turn all {}'s in JSON into dictionaries
> 2. Turn only the top most {} in JSON into a dictionary
> 3. Don't turn any {}'s in JSON into dictionaries.
>
> @{} would allow you explicitly differentiate dictionaries from js
> objects.

I think it would be a mistake to start using a custom version of JSON. "{}" is already the dictionary type in JSON, and while JSON happens to be a subset of JS, that doesn't mean {} *has* to map directly to JS objects. We could easily implement a Plist encoder/decoder which uses CPDictionary instead of plain JS objects. In most cases I think you'd want option #1 or #3.

Francisco Tolmasky

unread,
Dec 29, 2009, 7:01:44 PM12/29/09
to Cappuccino & Objective-J Development List
The main problem with mapping {} to dictionaries like we do today is
that there is a huge complexity cliff when you *don't* want options
#1, #2, or #3. You go from it just magically working to probably
having to do it all manually yourself (iterating over the js objects
and turning them into dictionaries when some condition is met). The
truth is that {} isn't really the dictionary type in JS, its the
everything type in JS, and its trivial to come up with cases where
you'd want both dictionaries and js objects alongside eachother, like @
{ key:@{ point: { x:1, y:2 } } }. @ really just serves as a simple
indicator to the JSON to the parser that you want it to be "promoted"
to a dictionary, and its very easy to take it and turn it into 'real'
JSON by just replacing instances of @{} with {} if that is needed.
However, it is impossible to to take said JSON and glean from it
whether something was meant to be a dictionary or just an object.
Reply all
Reply to author
Forward
0 new messages