Search code examples
eiffel

eiffel: semantic of ANY default


Surprised that Default in class ANY is frozen and without implementation???, what is the semantic for this function??

Class ANY

frozen default: detachable like Current
        -- Default value of object's type
    do
    end

My intention was to define a default: like Current or maybe detachable which returns the default value for current Class, so redefine it...


Solution

  • This is a default value of a detachable version of a type. For reference types, this is Void. For expanded types, this is the corresponding default value, i.e. the one initialized with default_create. E.g., for BOOLEAN, it is False.

    If an expanded class provides a specific implementation of default_create, it is used to initialize Result even without a body in default. For example, consider a class

    expanded class X inherit ANY redefine default_create end feature
        item: INTEGER_32
        default_create do item := 42 end
    end
    

    For a variable x of type X, an expression x.default.item would give 42. When default is called, the value of Result is initialized by calling X.default_create that sets item. So, no instructions in the body of default are required.

    To summarize, default returns

    • Void for reference types;
    • a default value for expanded types that do not redefine default_create, including basic types: False, 0, 0.0, etc. If an expanded type has nested attributes, they are initialized recursively using the same rule.
    • a value obtained by calling default_create otherwise.