0

I've just got the ACCOUNT old sample and write some code with the STRING owner's type:

class
ACCOUNT
create
make
feature
balance: INTEGER
owner: STRING
make
    do
        create owner.make_empty
    end
minimum_balance: INTEGER = 1000
open (who: STRING)
    do
        owner := who
    end

The application's code is:

acc: ACCOUNT
make
    do
        create acc.make
        acc.open ("Jill")
        ...

It is compiled and works. After I want to change owner type to a PERSON

owner: PERSON
...
open (who: PERSON)
    do
        owner := who
    end

and I created the PERSON class just as an extension to the STRING class:

class
PERSON
inherit
STRING
end

I believe this can work in every language but seems not in Eiffel. The code fails to compile with VGCC(6) and VEVI errors. Any ideas?

SeregASM
  • 75
  • 12

1 Answers1

1

I would not recommend making a person a descendant of a string for several reasons:

  • This does not model external world properly: a person has a name, it is not a name. In particular, it is allowed to change a name (e.g., after marriage), but this does not change the person.

  • This exposes features of string that are not related to a person. For example, what should return feature split?

  • This introduces unnecessary dependencies between a person and a string.

The last point is crucial to understand why you get compile-time errors: the descendant has to fulfil invariants of the ancestor. One of the invariants is that attributes of attached types should be initialized in the creation procedure. In your code there is no creation procedure that initializes attributes, hence the error VEVI for the default creation procedure default_create that is empty unless redefined.

Similarly, when you create a person object, a creation procedure needs to be called. But in the class PERSON no creation procedure is specified, so the compiler reports VGCC(6) - either the corresponding creation procedure is not specified in the creation instruction or the specified feature is not a creation procedure.

Alexander Kogtenkov
  • 5,770
  • 1
  • 27
  • 35