tags:

views:

352

answers:

6

Hello,

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

+1  A: 

Hi,

what would be the advantage of such a syntax for you?

If you have fix names why not create a class with properties?

Regards, divo

0xA3
+3  A: 

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...

Jon Skeet
Your XML example looks almost like the E4X extension of Javascript. http://en.wikipedia.org/wiki/E4X
Adrian
@Adrian: Sure - it's a natural thing to do with a dynamic language. I'd be surprised if it were radically different :)
Jon Skeet
+1  A: 

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

In addition LotusScript (in Notes) allows you to process the Notes databases in this fashion, where db.get("fieldname") is the same as db.fieldname.

I've used the REXX one a bit (as there's no choice) but when I've coded for Notes databases, I've preferred the get/put way of doing things.

paxdiablo
+1  A: 

Lua tables have a.x as syntactic sugar for a["x"]. Lua tables are associative arrays that could be used to represent arrays, hashes, and records of other languages. The sugar helps making code more readable by illustrating the intention (Record? Array? Hashtable?), though it makes no difference to the language.

Firas Assaad
A: 

Maybe you are looking for a class or struct if you you want to use the element name as a field/property.

leppie
+7  A: 

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.

rmeador