1

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 ?

1 Answers1

0
  1. `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

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

  3. If you directly want a meta object of the function, you can write `Example.a`.

Lucas Werkmeister
  • 2,584
  • 1
  • 17
  • 31
  • So, this should give me a list of attributes: value ga = `Example`.getAttributes; print("getAttributes: " + ga.string); but it prints: getAttributes: Attribute[](Type*), which is nice but not a list of attributes. I think I gave enough information to get a list of attributes ... Why is it necessary to specify additional information and what additional information does this function expect in order to give me a list of attributes; it asks for: Type* ? –  Jul 08 '16 at 11:28
  • 1
    That's a function reference. You're missing the the parentheses to invoke it. Try `getAttributes()`. – Gavin King Jul 08 '16 at 21:08