Re: [phix] if 'number' then why not 'list' ?
- Posted by irv Feb 21, 2021
- 1007 views
Not the same. Here is a comparison:
-- contents of cow.e---- public integer horns = 0 ------------------------ include cow.e as cow1 include cow.e as cow2 cow1:horns = 2 cow2:horns = 1 ? cow1:horns ? cow2:horns
1 1
As you can see, there is only one variable to store the # of horns, even if you use different "namespaces".
On the other hand, classes provide a "horns" variable for each instance of cow, so we change cow.e so that it declares a class:
export class Cow public integer horns = 0; end class
include cow.e Cow cow1 = new() Cow cow2 = new() cow1.horns = 2 cow2.horns = 1 ? cow1.horns ? cow2.horns
2 1
More often than not, I will declare methods which are called when the number of horns on any cow is accessed, so the cow class would look more like this:
export class Cow private integer horns; procedure set_horns(integer n) this.horns = n end procedure function get_horns() return this.horns end function end class
Within those methods, you can do other stuff that might be needed, like popping up a warning if someone tries to give Bessie 3 horns...etc.