constructoreiffelanonymous-objects

Passing anonymous object of derived class to a routine in Eiffel


Assuming that we have:

class A ...
class B inherit A ...

f (a_a: A) ...

Then, can you write something like the following pseudocode, without using a temporary variable?

-- `y` is an argument to B's constructor.
f (create B.make(y))

Solution

  • You are looking for a creation expression. Unlike creation instruction, it omits target entity, but always specifies a creation type:

    create {B}.make (y)
    

    Note. As a rule of thumb, if a type can be confused with an entity, it is enclosed in curly braces. For example, in the phrase create B.make (y) the terminal B is interpreted as an entity name. Enclosing it in the braces promotes it to a type.