Reputation:
I get unexpected results from the following code snippet using the eclipse ide:
class Example(String s = "init") {
shared String a() => "Func 1";
shared String b = "Attr 1";
shared String c(Integer i) { return "Func 2"; }
}
shared
void run() {
// 1.
print("getAttributes: " + `Example`.getAttributes().string);
print("getMethods: " + `Example`.getMethods().string);
// prints: []
// i.e. doesnt print any attribute or method
// 2.
value e = Example()
type(e); // error
e.type(); // error, should be replaced by the ide with the above version.
}
ad 1.) I get as a result:
getAttributes: []
getMethods: []
where I expected lists containing attributes or methods.
ad 2.) The doc says:
"The type() function will return the closed type of the given instance, which can only be a ClassModel since only classes can be instantiated. ..."
But I cant find the type() function and others related to meta programming, i.e. I dont get a tooltip, but instead I get a runtime (!) error:
Exception in thread "main" com.redhat.ceylon.compiler.java.language.UnresolvedCompilationError: method or attribute does not exist: 'type' in type 'Example'
So, where is the equivalent to the backticks `Example`.... as a function ?
Upvotes: 1
Views: 102
Reputation: 2742
`Example`.getAttributes()
returns an empty list because getAttributes
takes three type arguments: Container
, Get
, Set
. When called as getAttributes()
, the typechecker tries to infer them, but since there’s no information (no arguments with the appropriate type), the inferred type argument is Nothing
. Since Nothing
is not a container of any of the class’ members, the resulting list is empty. Use getAttributes<>()
instead to use the default type arguments, or specify them explicitly (e. g. getAttributes<Example>()
). Same thing with getMethods()
. Try online
The type
function is in ceylon.language.meta
and needs to be imported: import ceylon.language.meta { type }
. Once you do that (and remove the e.type()
line), the compilation error will go away.
If you directly want a meta object of the function, you can write `Example.a`
.
Upvotes: 0