A programming language in which you can refer to an array element. (Point)

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

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

I'm thinking about referencing an element named element_name using a dot instead of pillars:

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

Do you know any language that has a similar syntax to the example above?

What do you think of this example of an array element reference? Is this good or is it as bad as my writing in English?

respectfully

+1


source to share


6 answers


You can almost certainly do this in any dynamic language by reversing property / variable access as an indexer if the specified property / variable does not actually exist. I suspect that many dynamic languages ​​already provide this functionality in some areas.

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

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



One area where I could well do something like this would be for the XML API, where the property says "take the first element of the given name":

XElement author = doc.Root.Posts.Author;

      

This is pretty neat - for specific cases where this is what you want. Just don't try to apply it too broadly ...

+3


source


JavaScript does exactly what you describe. In JavaScript, each object is simply a map of property names to values. The parenthesis operator returns a property by name (or by value in the case of an integer index). You can refer to named properties simply by using a dot, although you cannot do so using integer metrics. This page details the parenthesis operator and dot notation.



+7


source


What would be the advantage of this syntax for you?

If you have the names of the fixes, why not create a class with properties?

+1


source


REXX has the concept of stems where you can say x.1, x.2 x.bob and they refer to array elements x [1], x [2], and x ['bob'] respectively.

In addition, LotusScript (in Notes) allows you to handle Notes databases in such a way that db.get ("fieldname") is the same as db.fieldname.

I used REXX bit one (as there is no choice), but when I am coded for Notes databases I preferred it to do things.

+1


source


Lua tables have ax as syntactic sugar for ["x"]. Lua tables are associative arrays that can be used to represent arrays, hashes, and records from other languages. Sugar helps make the code more readable by illustrating intent (Record? Array? Hashtable?), Although it has nothing to do with the language.

+1


source


You may be looking for a class or struct if you want to use the element name as a field / property.

0


source







All Articles