user3464741
user3464741

Reputation:

Type-safe metaprogramming? Using strings to query ceylon.language for meta information?

There seems to be a mismatch between the return value of type.getAttributes() and the argument value of type.getAttribute() concerning the (qulified vs. unqualified) naming of the attributes:

If the name of the attribute returned by getAttributes is e.g. "ceylon.language::String.reversed" this cannot be used to zip over attributes of two models:

value stringType1 = `String`;
value stringType2 = `String`;
for (a in stringType1.getAttributes<String,Anything,Nothing>(`SharedAnnotation`)) {
    if(exists p = stringType2.getAttribute<String,Anything,Nothing>(a.string)) {
      print(" >   `` p  ``     ");}
}

This code doesn't produce the expected result, because getAttribute() expects e.g. "reversed" as argument string instead of "ceylon.language::String.reversed" which is returned by getAttributes().

In general, I don't favor this approach to use strings for such a task. But it seems to be the only way to retrieve directly one single attribute by querying for it using a string.

The only alternative solution that I see is to read all attributes into a e.g. a TreeSet, write a Comparator that compares models representation of the attributes instead of their string representation and so let's one retrieve the required attributes in the right order for zipping them. Or something like: zip(...getAttributes<...>(...).sort(), ...getAttributes<...>(...).sort())?

Sometimes this might be unefficient, because it requires processing of all attributes when sometimes not all attributes are needed.

It would be desirable to have an standard api to expose meta information in a way similar to the collections api for tasks like to recursively apply a compare function to object hierarchies to match or test for different types of equality (by exact type match, by subtype, by value, unifiability, ...)

It might require a tree based collection of meta information, a selection function to retrieve only the required information and a compare function. In the end it will be sort of LINQ for ceylon meta information.

I'm wondering if such an api is already there or is it planned or is it left to the user to make one's own custom implementation?

What I really don't understand is that strings are used to query ceylon.language for meta information, where type-safety was an explicit design goal for ceylon metaprogramming.

Upvotes: 0

Views: 126

Answers (1)

Gavin King
Gavin King

Reputation: 4273

You should use a.declaration.name to get the unqualified name of an attribute a. (Or a.declaration.qualifiedName to get the qualified name.)

Don't use a.string, since, as documented by Object.string, it's just

A developer-friendly string representing the instance.

It's not intended for use as a key.

Upvotes: 0

Related Questions