14. Instantiation and containment
This chapter defines where new puts the instance it creates. The expression itself (its syntax, the three kinds of
type it can instantiate, the with clause) is in §9.7; the containment of concepts is declared in §5.2.1; the
instances and their container are in §13.2.
14.1 Containment, in two words
- A concept is either nested in a container concept (declared inside it, §5.2.1) or at the root of its model. A concept inherits the container of its parents when it declares none: its applicable container is the declared one, else the most specialized of those of its parents.
- An event and an exception are never nested: they belong to a model instance.
- A virtual model is either a root model, or declares a container model (it is declared in the directory of another model, §5.1.4).
- At run time, an instance of a nested concept is contained in an instance of its applicable container concept (or of a
specialization of it); an instance of a root concept, an event and an exception belong to a model instance; a model
instance may be contained in another model instance.
containerdesignates that container (§13.2).
14.2 The container of a new
new X(args) has no container of its own. The instance is created in a host, found from a starting
object:
| Form | Starting object |
|---|---|
new X(args) | this, the instance on which the behaviour runs |
c.new X(args) | the object designated by the path c |
From the starting object, the containment tree is climbed: the starting object itself, then its container, then the
container of that one, up to the model instance, then to the model instance that contains it, if any. The host is
the nearest object that can hold an X:
X is | A host is |
|---|---|
a concept nested in a container concept K | an instance of K or of a specialization of K |
| a root concept | a model instance of the model that declares X, or of a model that extends it |
| an event or an exception | none: it does not climb; it belongs to the model instance of the starting object, which is its source (the listeners are looked up from it) |
a virtual model that declares a container model M | a model instance of M or of a specialization of M |
| a virtual model with no container model | none: it is not hosted by an instance, and does not climb (§9.7, §12.5) |
The rules that follow hold for every form.
- The nearest wins. There is no ambiguity error. When a concept can contain a specialization of itself, and an instance of that specialization is the starting object, it is the host, and not the instance around it.
- The starting object is a candidate. A
new X()that was valid becausethiscould hold anXkeeps its meaning: the instance is created inthis. - A sibling is a climb. From an instance of
B, nested inA,new B(…)finds noBable to hold aB, climbs to the instance ofA, and creates aBbeside the current one: itscontaineris thatA. - The explicit form climbs from its path.
c.new X(…)climbs from the object designated byc, which need not be the container ofX. - A behaviour of an ancestor is tolerated. A behaviour declared on a model, or on a concept, may run on an instance of a
specialization: when the model climbed is a parent of the model a root concept or a contained model is declared in, or the
concept of the starting object is a parent of the container concept of
X, thenewis accepted when validated, and it succeeds when the climb meets a host at run time. - A model nested in another one has no other fallback. A virtual model that declares a container model needs a
hosting model instance on the way up: when there is none, the
newfails with anFMLResolutionError. Therc,pathandfolderparameters apply to a model that declares no container model only. - A nested concept is not created in the model. From a behaviour declared on a model, a
newof a concept nested in another one is an invalid binding: there is no instance of the container to climb to. - No syntax is added. A bare
newis still printed bare:thisis never written, andthis.new X()is not rewritten.
public concept Alpha {
String name;
create(String name) {
this.name = name;
}
public concept Beta {
String name;
create(String name) {
this.name = name;
}
// The Alpha that holds this Beta
public Beta sibling(String name) {
return new Beta(name);
}
// The model instance, two steps up
public Alpha another(String name) {
return new Alpha(name);
}
// The same, from another Beta
public Alpha anotherFrom(Beta other, String name) {
return other.new Alpha(name);
}
}
}
14.3 Where it is checked
The same rule is applied twice.
-
When the unit is validated, on the concepts: starting from the concept of the behaviour (or, for
c.new X, the concept of the type ofc), the applicable containers are climbed, then the models. When no host can be reached, thenewis an invalid binding, reported with the concepts that were looked at:cannot instantiate Beta in Containment: no container (an instance of Alpha) can be reached from Containment (looked at: Containment)The
unmatched: new X(…)of amatchis not checked: it is evaluated like anynew, fromthis, and not from thefromcontainer of thematch(§10.9). -
When it runs, on the instances: starting from the object, the containers are climbed with the concepts of the instances, which may be specializations of those that were validated. A
newwhose host cannot be found raises anFMLResolutionError(§13.6); an invalid binding is an execution error as any other (§9.8).
FMLContainerResolver implements both sides; CreationSchemePathElement calls it.
The checks of the instance factory, which require a container compatible with the concept, are kept: for a concept, they
see a host chosen by the climb. An event and an exception are created in a model instance, and a virtual model with no
container model has no host. TestContainment.fmlscript and TestContainmentValidation (fml-cli-test) run the
examples of this chapter on FML/Containment.fml.