The Ceylon metamodel open type and declaration package.
As described in the ceylon.language.meta documentation, this package contains all the types that represent Ceylon declarations and open types.
The following code will list all the classes in the ceylon.language package and print their
extended type:
for(decl in `package ceylon.language`.members<ClassDeclaration>()){
if(exists extendedType = decl.extendedType){
print("Class ``decl.name`` extends: ``extendedType``");
}else{
print("Class ``decl.name`` does not extend anything");
}
}
The following code will iterate all the class declarations in the ceylon.language package that
are not abstract, anonymous or annotations, and that have no type parameters nor initialiser
parameters. For each matching class, we will apply it to get a class model which we can then
use to instantiate the class and display its instance:
for(decl in `package ceylon.language`.members<ClassDeclaration>()){
if(!decl.abstract
&& !decl.anonymous
&& !decl.annotation
&& decl.parameterDeclarations.empty
&& decl.typeParameterDeclarations.empty){
Class<Object,[]> classModel = decl.classApply<Object,[]>();
Object instance = classModel();
print("Instance of ``decl.name`` is: ``instance``");
}
}
| Aliases | |
OpenTypeArgument | Source Codeshared OpenTypeArgument=> [OpenType, Variance]A tuple representing an open type argument and its use-site variance. Since 1.2.0 |
| Values | |
contravariant | Source Codeshared contravariant contravariantContravariant means that supertypes of the given type may be accepted. |
covariant | Source Codeshared covariant covariantCovariant means that subtypes of the given type may be returned. |
invariant | Source Codeshared invariant invariantInvariant means that neither subtype nor supertype can be accepted, the type has to be exactly that which is declared. |
nothingType | Source Codeshared nothingType nothingTypeThe singleton open type for Nothing. |
| Interfaces | |
AliasDeclaration | Source Codeshared AliasDeclarationType alias declaration. While type aliases are erased (substituted for what they alias is a better term) from every declaration that uses them during compile-time, the declaration of the type alias is still visible at run-time. |
AnnotatedDeclaration | Source Codeshared AnnotatedDeclarationDeclaration which can be annotated, such as: You can query annotations that are placed on a given annotated declaration with: |
CallableConstructorDeclaration | Source Codeshared CallableConstructorDeclarationDeclaration model for callable constructors, for example class WithConstructors {
shared new () {}
shared new clone(WithConstructors other) {}
// ...
CallableConstructorDeclaration default = `new WithConstructors`;
CallableConstructorDeclaration clone = `new WithConstructors.clone`;
The initializer of a class with a parameter list can also be
represented
as a |
ClassDeclaration | Source Codeshared ClassDeclarationClass declaration. Callable classesSince Ceylon 1.2 classes are not always directly invokable
(if the class has constructors, but not a default constructor). Thus
members of Usage sample for toplevel classesBecause some classes have type parameters, getting a model requires applying type arguments to the
class declaration with class Foo<T>(){
string => "Hello, our T is: ``typeLiteral<T>()``";
}
void test(){
// We need to apply the Integer closed type to the Foo declaration in order to get the Foo<Integer> closed type
Class<Foo<Integer>,[]> classModel = `class Foo`.classApply<Foo<Integer>,[]>(`Integer`);
// This will print: Hello, our T is: ceylon.language::Integer
print(classModel());
}
Usage sample for member classesFor member classes it is a bit longer, because member classes need to be applied not only their type arguments but also
the containing type, so you should use class Outer(){
shared class Inner(){
string => "Hello";
}
}
void test(){
// apply the containing closed type `Outer` to the member class declaration `Outer.Inner`
MemberClass<Outer,Outer.Inner,[]> memberClassModel = `class Outer.Inner`.memberClassApply<Outer,Outer.Inner,[]>(`Outer`);
// We now have a MemberClass, which needs to be applied to a containing instance in order to become an
// invokable class model:
Class<Outer.Inner,[]> boundMemberClassModel = memberClassModel(Outer());
// This will print: Hello
print(boundMemberClassModel());
}
|
ClassOrInterfaceDeclaration | Source Codeshared ClassOrInterfaceDeclarationA class or interface declaration. Usage sample for toplevel classesBecause some classes have type parameters, getting a model requires applying type arguments to the
class declaration with class Foo<T>() {
string => "Hello, our T is: ``typeLiteral<T>()``";
}
void test(){
// We need to apply the Integer closed type to the Foo declaration in order to get the Foo<Integer> closed type
ClassOrInterface<Foo<Integer>> classOrInterfaceModel = `class Foo`.apply<Foo<Integer>>(`Integer`);
assert(is Class<Foo<Integer>,[]> classOrInterfaceModel);
// This will print: Hello, our T is: ceylon.language::Integer
print(classOrInterfaceModel());
}
Note that there are more specialised versions of Usage sample for member classesFor member classes or interfaces it is a bit longer, because member types need to be applied not only their type arguments but also
the containing type, so you should use class Outer(){
shared class Inner(){
string => "Hello";
}
}
void test(){
// apply the containing closed type `Outer` to the member class declaration `Outer.Inner`
value memberClassModel = `class Outer.Inner`.memberApply<Outer,Outer.Inner>(`Outer`);
assert(is MemberClass<Outer,Outer.Inner,[]> memberClassModel);
// We now have a MemberClass, which needs to be applied to a containing instance in order to become an
// invokable class model:
Class<Outer.Inner,[]> boundMemberClassModel = memberClassModel(Outer());
// This will print: Hello
print(boundMemberClassModel());
}
Note that there are more specialised versions of |
ClassWithConstructorsDeclaration | Source Codeshared ClassWithConstructorsDeclarationThe declaration model of a class that has constructors. For example: class Point {
shared new(Float x, Float y) {
// ...
}
shared new polar(Float r, Float theta) {
// ...
}
shared new origin {
// ...
}
}
Such classes may not have a default (unnamed) constructor, so defaultConstructor has optional type. |
ClassWithInitializerDeclaration | Source Codeshared ClassWithInitializerDeclarationThe declaration model of a class that has a parameter list rather than explicit constructors. For example: class Color(Integer rgba) {
}
Such classes have a meaningful parameter list and for abstraction purposes
have a single |
ConstructorDeclaration | Source Codeshared ConstructorDeclarationAbstraction over callable constructors and value constructors |
Declaration | Source Codeshared DeclarationA declaration. There are only two types of declarations:
|
FunctionDeclaration | Source Codeshared FunctionDeclarationAbstraction over declarations which can be invoked, namely functions, methods and constructors |
FunctionOrValueDeclaration | Source Codeshared FunctionOrValueDeclarationA function or value declaration. |
FunctionalDeclaration | Source Codeshared FunctionalDeclarationA function declaration. Usage sample for toplevel functionBecause some functions have type parameters, getting a model requires applying type arguments to the
function declaration with String foo<T>(){
return "Hello, our T is: ``typeLiteral<T>()``";
}
void test(){
// We need to apply the Integer closed type to the foo declaration in order to get the foo<Integer> function model
Function<String,[]> functionModel = `function foo`.apply<String,[]>(`Integer`);
// This will print: Hello, our T is: ceylon.language::Integer
print(functionModel());
}
Usage sample for methodsFor methods it is a bit longer, because methods need to be applied not only their type arguments but also
the containing type, so you should use class Outer(){
shared String hello() => "Hello";
}
void test(){
// apply the containing closed type `Outer` to the method declaration `Outer.hello`
Method<Outer,String,[]> methodModel = `function Outer.hello`.memberApply<Outer,String,[]>(`Outer`);
// We now have a Method, which needs to be applied to a containing instance in order to become an
// invokable function:
Function<String,[]> boundMethodModel = methodModel(Outer());
// This will print: Hello
print(boundMethodModel());
}
|
GenericDeclaration | Source Codeshared GenericDeclarationA declaration that can have type parameters. |
GettableDeclaration | Source Codeshared GettableDeclarationAbstraction over declarations from which a value can be obtained, namely
|
Import | Source Codeshared ImportModel of an |
InterfaceDeclaration | Source Codeshared InterfaceDeclarationAn interface declaration. Usage sample for toplevel interfacesBecause some interfaces have type parameters, getting a model requires applying type arguments to the
interface declaration with interface Foo<T> satisfies List<T> {
}
void test(){
// We need to apply the Integer closed type to the Foo declaration in order to get the Foo<Integer> closed type
Interface<Foo<Integer>> interfaceModel = `interface Foo`.interfaceApply<Foo<Integer>>(`Integer`);
// This will print: ceylon.language::List<ceylon.language::Integer>
for(satisfiedType in interfaceModel.satisfiedTypes){
print(satisfiedType);
}
}
Usage sample for member interfacesFor member interfaces it is a bit longer, because member interfaces need to be applied not only their type arguments but also
the containing type, so you should use class Outer(){
shared interface Inner<T> satisfies List<T> {
}
}
void test(){
// apply the containing closed type `Outer` to the member class declaration `Outer.Inner`
MemberInterface<Outer,Outer.Inner<Integer>> memberInterfaceModel = `interface Outer.Inner`.memberInterfaceApply<Outer,Outer.Inner<Integer>>(`Outer`, `Integer`);
// This will print: ceylon.language::List<ceylon.language::Integer>
for(satisfiedType in memberInterfaceModel.satisfiedTypes){
print(satisfiedType);
}
}
|
Module | Source Codeshared ModuleA |
NestableDeclaration | Source Codeshared NestableDeclarationA declaration which can be contained in a Functions, values, classes, interfaces and aliases are such declarations. |
OpenClassOrInterfaceType | Source Codeshared OpenClassOrInterfaceTypeAn open class or interface, with open type arguments. For example, |
OpenClassType | Source Codeshared OpenClassTypeAn open class type. |
OpenInterfaceType | Source Codeshared OpenInterfaceTypeAn open interface type. |
OpenIntersection | Source Codeshared OpenIntersectionAn open intersection type. |
OpenType | Source Codeshared OpenTypeAn open type. An open type is a type which may contain unbound type variables, such as |
OpenTypeVariable | Source Codeshared OpenTypeVariableAn open type variable. |
OpenUnion | Source Codeshared OpenUnionAn open union type. |
Package | Source Codeshared PackageModel of a |
SetterDeclaration | Source Codeshared SetterDeclarationA setter declaration represents the assign block of a |
TypeParameter | Source Codeshared TypeParameterA type parameter declaration. |
TypedDeclaration | Source Codeshared TypedDeclarationDeclaration which has an open type. |
ValueConstructorDeclaration | Source Codeshared ValueConstructorDeclarationDeclaration model for value constructors, for example class Currency {
"The US Dollar"
shared new usd {}
// ...
}
ValueConstructorDeclaration dollars = `new Currency.usd`;
|
ValueDeclaration | Source Codeshared ValueDeclarationA value declaration. Usage sample for toplevel valueGetting a model requires applying type arguments to the
value declaration with String foo = "Hello";
void test(){
// We need to apply the the foo declaration in order to get the foo value model
Value<String> valueModel = `value foo`.apply<String>();
// This will print: Hello
print(valueModel.get());
}
Usage sample for attributesFor attributes it is a bit longer, because attributes need to be applied the containing type, so you should
use class Outer(){
shared String foo => "Hello";
}
void test(){
// Apply the containing closed type `Outer` to the attribute declaration `Outer.foo`
Attribute<Outer,String> valueModel = `value Outer.foo`.memberApply<Outer,String>(`Outer`);
// We now have an Attribute, which needs to be applied to a containing instance in order to become a
// readable value:
Value<String> boundValueModel = valueModel(Outer());
// This will print: Hello
print(boundValueModel.get());
}
|
Variance | Source Codeshared VarianceVariance information. |
| Classes | |
contravariant | Source Codeshared contravariantContravariant means that supertypes of the given type may be accepted. |
covariant | Source Codeshared covariantCovariant means that subtypes of the given type may be returned. |
invariant | Source Codeshared invariantInvariant means that neither subtype nor supertype can be accepted, the type has to be exactly that which is declared. |
nothingType | Source Codeshared nothingTypeThe singleton open type for Nothing. |