Search code examples
classeiffelvoid-safety

Class attributes in Eiffel


I am trying to make a class in Eiffel, consisting of a few attributes and functions. I keep getting issues with the attributes not being either visible to setName or not being initialised correctly. The compiler error is: VEVI: Variable is not properly set. Attribute(s): name. I want to be able to instantiate a TESTER object in APPLICATION and invoke these methods.

class
    TESTER
create
    make

feature

    name: STRING
    score: INTEGER
    make

        do
            io.putstring ("I am making TESTER%N")
        end

        sleep
        do
            io.put_string ("TESTER is sleeping%N")
        end

        setName (name_: STRING)
        do
            name := name_
        end

end

Solution

  • This has to do with void-safety ( https://www.eiffel.org/doc/eiffel/Void-safe%20programming%20in%20Eiffel ).

    There are several ways to address the issue in the example, two of them are shown below:

    1. Declare name as detachable. By default class types are attached. Changing the type to detachable allows the attribute name to be initialized to the default value Void, i.e. not attached to any object.

      name: detachable STRING
      
    2. Attach an object to the attribute name in the creation procedure make.

      make
          do
              io.putstring ("I am making TESTER%N")
              name := "something"
          end
      

    The simplified version of the rule says that all attributes should be set at the end of a creation procedure.