0

I learning Eiffel, and i know that in C i can do something like this to set the headers files for example::

#define USER_ACTIVE     0
#define WHEN_SOMETHING  1
#define WHERE_HAND      2
#define WHERE_ACTIVE    3
#define WHERE_GOOD      4

and also having instances for example:

typedef struct something {
   int user;
   int where;
   int somethingelse
}something;

or even collection functions pointers:

typedef struct decisions {
  void (*init)(struct something *s, int who, double factor);
}decisions;

And it is the same story for almost all normal programming languages.. i been looking around fx here besides that it is not well translated, for me is hard to grasp how to do it.. so is there a "normal" way to doing this in this language? or all has to be done spaghetti style?

thanks

Jorge Y. C. Rodriguez
  • 3,394
  • 5
  • 38
  • 61
  • 1
    A more complete example will help to understand the problem. Are you interested only in declaring constants that can be used in other classes? In particular, when talking about updating/adding/removing the original value, is it like **#define**, **#undef** in C-like languages? – Alexander Kogtenkov Oct 28 '13 at 12:15
  • @kwaxer yeah sorry, i should have made more clear from the beginning, i think now is better for future people looking for this :) – Jorge Y. C. Rodriguez Oct 28 '13 at 12:42
  • In Eiffel **typedef**s are mapped to classes and function pointers are mapped either to polymorphic features, to dedicated classes or to agents depending on the required level of flexibility. I'll extend my reply to cover those. – Alexander Kogtenkov Oct 28 '13 at 15:47

1 Answers1

1

The example can be translated into something like that:

class MY_CONSTANTS feature
    user_active: INTEGER = 0
    when_something: INTEGER = 1
    where_hand: INTEGER = 2
    where_active: INTEGER = 3
    where_good: INTEGER = 4
end

class SOMETHING
feature -- Access
    user: INTEGER
    where: INTEGER
    somethingelse: INTEGER
feature -- Modification
    set_user (u: like user) do user := u end
    set_where (w: like where) do where := w end
    set_somethingelse (s: like somethingelse) do somethingelse := s end
end

deferred class DECISION feature
    init (s: SOMETHING; who: INTEGER; factor: REAL_64) deferred end
end

class DECISION_1 inherit DECISION feature
    init (s: SOMETHING; who: INTEGER; factor: REAL_64)
        do
            ...
        end
end

class DECISION_2 inherit DECISION feature
    init (s: SOMETHING; who: INTEGER; factor: REAL_64)
        do
            ...
        end
end

class MY_CLASS inherit
    MY_CONSTANTS
... -- user_active, when_something, etc. can be used here
feature
    something: SOMETHING
    decision: DECISION
...
           -- Somewhere in the code
        create something
        if ... then
            create {DECISION_1} decision
        else
            create {DECISION_2} decision
        end
        ...
        decision.init (something, ..., ...)
    ...
end
Alexander Kogtenkov
  • 5,770
  • 1
  • 27
  • 35
  • so I dont need to say `#include` since all classes are global, right? – Jorge Y. C. Rodriguez Oct 28 '13 at 17:36
  • Indeed. But you need to inherit from SOMETHING to make its features available, or create an instance of it. PS: instead of #include there's actually a project/xace file that has the locations to the file. So #includes are not sprinkled in every source file. – Berend de Boer Oct 29 '13 at 04:42