mercredi 14 septembre 2016

Is there a name for the following reflection language feature and standard way to implement?

Let's say you have an object and you want to get a string of the name of the property you are referencing. Is there a name for that? I mean it's some sort of reflection but is there a more specific name for it and do languages implement it in standard way?

For example, this feature exists for class based objects using toString() in JavaScript or ActionScript.

var person:Person = new Person();
var nameOfClass:String = person.toString();
trace(nameOfClass); // traces "Person" or "com.mydomain.model.Person"

But since properties can be null or undefined you can't use toString() as it will throw an error or if it's not null it will get the value:

var person:Person = new Person();
var nameOfProperty:String = person.name.toString(); // error is thrown when null
person.name = "John";
person.name.toString(); // traces the value "John" not the type

So I'm guessing some languages would have a global function like so.

var person:Person = new Person();
var nameOfProperty:String = propertyName(person.name);

trace(nameOfProperty); // traces "name"

nameOfProperty = propertyName(person.zipCode);
trace(person.zipCode); // traces "zipCode" 

Is there a specific name for this feature and if so how is implemented?

Aucun commentaire:

Enregistrer un commentaire