constantseiffel

What is the best way to declare a constant into a class that can be redefined


What is in eiffel the best way to have a constant which can be redefined?

while having a function which only returns "green" or "blue" needs the string to be created again, performance problem or doesnt matter?

As far as I understood, the onces cannot be redeclared...


Solution

  • Once features can be redefined as any other features. The example would be

    class A feature
        color: STRING once Result := "green" end
    end
    
    class B inherit A redefine color end feature
        color: STRING once Result := "blue" end
    end
    

    Also, manifest strings themselves can be defined as once:

    class A feature
        color: STRING do Result := once "green" end
    end
    
    class B inherit A redefine color end feature
        color: STRING do Result := once "blue" end
    end
    

    The behavior in both cases is identical, so you can even mix both variants. The performance might be different but only marginally. In both cases new strings are not created on every call.