Passing anonymous object of derived class to a routine in Eiffel

58 Views Asked by At

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))
1

There are 1 best solutions below

0
Alexander Kogtenkov On BEST ANSWER

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.