Re: Heaven help me, I miss the C++ class (not a question,

new topic     » goto parent     » topic index » view thread      » older message » newer message

On Thu, 24 Feb 2000, SR Williamson wrote:

> <snip>
> Now here's the sticky part (speaking of which, thanks for sticking with
> this wordy message so far....). In C++, I can define procedures to go with
> my people. And using inheritance, I can make them different. Say I want to
> initiate a subclass of people, call them Good, and another subclass, call
> them Bad.

> My Good::people and Bad::people are exactly the same except for their
> actions (and maybe their thoughts and feelings if I want), but these folks
> all inherit from the same generic people class.
> ....
> But my problem comes in when I now have to give them behaviors and
> attributes. I have to do it individually. I could use a procedure with a
> sequence passed in, like this
>
> procedure beGood(sequence aPeople)
> aPeople[good] = blah, blah, blah
>
> and that would help cut down on errors (but not make them almost out of the
> question, like in C++). And the C++ statement is very elegant
>
> good.beGood()
>
> And it does it for all good people. (I think)
>
> Is there an easy, elegant way to do this in EU?
>
> Is there a way to emulate this using sequences that I'm missing? Would it
> be impossible, or useless, or worse, dangerous, to be able to have a
> *procedure or function* be part of a sequence, say in a future release?

I can't say whether you'll consider this elegant, but it's quite possible to
define a sequence to contain function or procedure id's:

constant NAME = 1, GETS_RICH = 2, BECOMES_FAMOUS = 3, GREED = 4

function DoGood(object person)
 -- do stuff
 return person
end function
constant NICE = routine_id("DoGood")

function DoEvil(object person)
 person[GREED] += 1
 HireMoreLawyers()
 return person
end function
constant NAUGHTY = routine_id("DoEvil")

constant -- create a generic person
 person = {"",      -- name
               NICE, -- default action when "gets rich"
               NICE, -- default action when "becomes famous"
               .0,      -- greed factor
               ...}      -- other attributes

sequence
 CShultz, BillG

 CShultz = person -- make a copy of the generic person
 CShultz[NAME] = "Charles"
 -- CShultz inherits the "NICE" action from the generic person,
 -- as well as the greed factor and other attributes

 BillG = person
 BillG[NAME] =  "Wild Bill"
 BillG[GETS_RICH] = NAUGHTY -- overrides the default niceness

function react_to(object person, integer event)
 return call_func(person[event],{person})
end function

 BillG = react_to(BillG,GETS_RICH)
 CShultz = react_to(CShultz,GETS_RICH)

So, you're basicaily defining varied responses to the same event.
Usually the event will be a mouse click or something.
In these calls, I am returning a modified (self), so
that, for example, when BillG makes money, the DoEvil function can increment
this person's greed factor, and return an even more greedy BillG.
---
Legal Disclaimer: the variables above are not intended to bear any resemblance
to  persons living.
---
Irv

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu