I’m really curious to know why initializing objects, primarily Hashes (maps, dictionaries, key-value lookups, whatever you want to call them) are such a pain to initialize.
In your typical Java scenario this is how you initialize your Hash:
HashMaphashyMcHash = new HashMap (); hashyMcHash.put("foo", "bar"); hashyMcHash.put("car", "jar"); hashyMcHash.put("arr", "rawr"); // And... so on
Meanwhile, most sane languages allow you to do crazy things like this:
hashyMcHash = {foo: "bar", car: "jar", arr: "rawr"};
Now, in languages like Python, Ruby, JavaScript those are just parts of the language and you kinda get them for free, so I can understand something like that not being available in Java. Now, lets look at another statically typed language that I think took a fair compromise between how to initialize a Hash.
NSDictionary hashyMcHash = [[NSDictionary alloc] initWithObjectsAndKeys: @"foo", @"bar", @"car", @"jar", @"arr", @"rawr", nil];
To me this seems like a pretty fair compromise for initializing our Hashes since it allows me to state what some default values in it will be in code that is fairly readable. I have an idea of how one could do it in Java, though don’t know if it exists or not.
// Constructor would probably look like this: // HashMap(K[] keys, V[] values) HashMap hashyMcHash = new HashMap ( ["foo", "car", "arr"], ["bar", "jar", "rawr"], );
If this does exist, please let me know because I’d really love something like this. If it doesn’t… why?