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

46 views Asked by At

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

  • class A => color: STRING = "green"
  • color B inherit A => cannot redefine

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...

1

There are 1 answers

4
Alexander Kogtenkov On BEST ANSWER

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.