Pipo
Pipo

Reputation: 5083

Eiffel: setting a constant with reference to other class constant

How to set a constant refering to another class constant in Eiffel?

Something like that doesn't compile unfortunately

Default_log_level: like {MY_LOGGER}.log_level = {MY_LOGGER}.Log_level_info

Upvotes: 0

Views: 52

Answers (2)

Eric Bezault
Eric Bezault

Reputation: 141

Constant attributes can only be made of a manifest constant, but a possible workaround could be to use frozen once functions:

frozen Default_log_level: INTEGER
    once
       Result := {MY_LOGGER}.Log_level_info
    ensure
        definition: Result = {MY_LOGGER}.Log_level_info
    end

frozen means that it cannot be redefined in descendant classes (like constant attributes).

Unfortunately, the type of once functions cannot rely on anchored types, hence the use of INTEGER instead of like {MY_LOGGER}.log_level.

And finally, the drawback with this solution is that it cannot be used where constant attributes are expected (e.g. in inspect instructions).

Upvotes: 2

Alexander Kogtenkov
Alexander Kogtenkov

Reputation: 5790

Constant attributes cannot be defined using other constant attributes in the current version of Eiffel.

Upvotes: 2

Related Questions