Programming language where you can refer to a array element by . (dot)

In C# language when you refer to an array element you can write:

myclass.my_array['element_name'] = new Point(1,1);

I think about refering to a element with name element_name by using dot in place of backets:

myclass.my_array.element_name = new Point(1,1);

Do you know any language where exists similar syntax to the example above?

What do you think about this example of refering to a array element? Is this good or is it as bad as my writing in english skills?

Kind regards


You could almost certainly do this in any dynamic language, by handling property/variable access as an indexer if the specified property/variable didn't actually exist. I suspect that many dynamic languages already provide this functionality in some areas.

It's possible that in C# 4 you'll be able to make your objects behave like this if you really want to.

However, I would agree with the implicit judgement in Mohit's question - I see no reason to consider this more generally readable than using the more common indexer syntax, and it will confuse people who are used to indexers looking like indexers.

One area where I would quite possibly do something like this would be for an XML API, where a property would indicate "take the first element of the given name":

XElement author = doc.Root.Posts.Author;

That's quite neat - for the specific cases where it's what you want. Just don't try to apply it too generally...

JavaScript does exactly what you describe. In JavaScript, every object is just a map of property names to values. The bracket operator just returns a property by name (or by value in the case of an integer index). You can refer to named properties by just using a dot, though you can't do that with integer indicies. This page describes the bracket operator and dot notation in detail.

Need Your Help

check if UIButton is selected

iphone objective-c ios ipad

Is there any way to keep track the state of a UIButton whether it is selected or not? I tried accessing the selected property but it doesn't seem to work, seems to work only for UISwitch

WordPress: Get ID of custom post type within nested shortcodes

php wordpress nested shortcode

I'm currently having trouble getting the ID of a custom post type from within a nested shortcode. I'm not sure if this is possible.