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

Answers


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

LuaSQLite - reading a single entry

sqlite lua

I would like to read a single entry from sqlite-database with LuaSQLite3 and not all rows.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.