Difference between revisions of "Forget / Keep Mechanism"

Line 3: Line 3:
 
===Example===
 
===Example===
  
 +
{| class="top-aligned"
 +
|
 
<e>
 
<e>
 
class ANIMAL
 
class ANIMAL
Line 12: Line 14:
 
end
 
end
 
</e>
 
</e>
 
+
|
 
<e>
 
<e>
 
class CAT
 
class CAT
Line 29: Line 31:
 
end
 
end
 
</e>
 
</e>
 +
|
 +
<e>
 +
class FOOD
 +
end
 +
 +
class CAT_FOOD
 +
 +
inherit
 +
 +
  FOOD
 +
 +
end
 +
</e>
 +
|}
  
cat-call:
+
===cat-call===
  
 
<e>
 
<e>
Line 44: Line 60:
 
</e>
 
</e>
  
forget-mechanism:
+
===forget-mechanism===
  
Types which have covariant redefined features will not be conform.
+
Types which have covariant redefined features will not be conform. A feature which is covariantly redefined changes its inherit clause implicitly to inherit from a parent class which has the redefined feature in a forget clause.
  
{| class="wikitable"
+
{| class="top-aligned"
 
|-
 
|-
| what you write  
+
! what you write  
| what is implied after you covariantly redefine <e>eat</e>
+
! what is implied
 
|-
 
|-
 
|
 
|
Line 64: Line 80:
 
     end
 
     end
  
...
+
feature
 +
 
 +
  eat (f: CAT_FOOD)
 +
 
 +
end
 
</e>
 
</e>
 
|
 
|
Line 79: Line 99:
 
     end
 
     end
  
...
+
feature
 +
 
 +
  eat (f: CAT_FOOD)
 +
 
 +
end
 
</e>
 
</e>
 
|}
 
|}
  
where <e>ANIMAL</e> does not conform to <e>ANIMAL forget all</e>:
+
The conformance between <e>ANIMAL</e> and <e>ANIMAL forget eat</e> and <e>ANIMAL forget all</e> is as follows:
  
 
<e>
 
<e>
 
local
 
local
 
   normal_animal: ANIMAL
 
   normal_animal: ANIMAL
   forget_animal: ANIMAL forget eat end
+
   forget_eat_animal: ANIMAL forget eat end
 +
  forget_all_animal: ANIMAL forget all end
 
do
 
do
     -- this assignment is legal
+
     -- this assignment is legal since all features present in the
   forget_animal := normal_animal
+
    -- forget types are also present in the normal type
 +
  forget_eat_animal := normal_animal
 +
   forget_all_animal := normal_animal
  
 
     -- this assignment is illegal since the forget_animal lacks
 
     -- this assignment is illegal since the forget_animal lacks
 
     -- the eat feature and thus cannot be used as an ANIMAL
 
     -- the eat feature and thus cannot be used as an ANIMAL
   normal_animal := forget_animal
+
   normal_animal := forget_eat_animal
 +
  normal_animal := forget_all_animal
 +
 
 +
    -- this assignment is legal since the forget all type
 +
    -- has fewer or equal features than the forget eat type
 +
  forget_all_animal := forget_eat_animal
 +
 
 +
    -- this assignment is only legal if only the feature `eat'
 +
    -- is covariantly redefined in the type ANIMAL and thus
 +
    -- forget eat is equivalent to forget all
 +
  forget_eat_animal := forget_all_animal
 +
 
 
end
 
end
 
</e>
 
</e>
  
Now the cat-call example:
+
Now the cat-call example with the new forget types:
  
 
<e>
 
<e>
Line 107: Line 145:
 
do
 
do
 
     -- illegal assignment, ANIMAL and CAT don't conform
 
     -- illegal assignment, ANIMAL and CAT don't conform
     -- since CAT inherits from ANIMAL forget eat
+
     -- since CAT implicitly inherits from ANIMAL forget eat
 
   a := c
 
   a := c
 
   a.eat (food)
 
   a.eat (food)
 +
 
end
 
end
 
</e>
 
</e>
Line 120: Line 159:
 
     -- legal, CAT conforms to ANIMAL forget all
 
     -- legal, CAT conforms to ANIMAL forget all
 
   a := c
 
   a := c
 +
 
     -- illegal, ANIMAL forget all doesn't have a feature eat
 
     -- illegal, ANIMAL forget all doesn't have a feature eat
 
   a.eat (food)
 
   a.eat (food)
 +
 
end
 
end
 
</e>
 
</e>

Revision as of 16:39, 19 February 2007

Warning.png Warning: Warning: Article under development

Example

class ANIMAL
 
feature
 
  eat (f: FOOD)
 
end
class CAT
 
inherit
 
  ANIMAL
    redefine
      eat
    end
 
feature
 
  eat (f: CAT_FOOD)
 
end
class FOOD
end
 
class CAT_FOOD
 
inherit
 
  FOOD
 
end

cat-call

local
  a: ANIMAL
  c: CAT
do
 
  a := c
  a.eat (food)
 
end

forget-mechanism

Types which have covariant redefined features will not be conform. A feature which is covariantly redefined changes its inherit clause implicitly to inherit from a parent class which has the redefined feature in a forget clause.

what you write what is implied
class CAT
 
inherit
 
  ANIMAL
    redefine
      eat
    end
 
feature
 
  eat (f: CAT_FOOD)
 
end
class CAT
 
inherit
 
  ANIMAL
    forget 
      eat
    redefine
      eat
    end
 
feature
 
  eat (f: CAT_FOOD)
 
end

The conformance between ANIMAL and ANIMAL forget eat and ANIMAL forget all is as follows:

local
  normal_animal: ANIMAL
  forget_eat_animal: ANIMAL forget eat end
  forget_all_animal: ANIMAL forget all end
do
    -- this assignment is legal since all features present in the
    -- forget types are also present in the normal type
  forget_eat_animal := normal_animal
  forget_all_animal := normal_animal
 
    -- this assignment is illegal since the forget_animal lacks
    -- the eat feature and thus cannot be used as an ANIMAL
  normal_animal := forget_eat_animal
  normal_animal := forget_all_animal
 
    -- this assignment is legal since the forget all type
    -- has fewer or equal features than the forget eat type
  forget_all_animal := forget_eat_animal
 
    -- this assignment is only legal if only the feature `eat'
    -- is covariantly redefined in the type ANIMAL and thus
    -- forget eat is equivalent to forget all
  forget_eat_animal := forget_all_animal
 
end

Now the cat-call example with the new forget types:

local
  a: ANIMAL
  c: CAT
do
    -- illegal assignment, ANIMAL and CAT don't conform
    -- since CAT implicitly inherits from ANIMAL forget eat
  a := c
  a.eat (food)
 
end
local
  a: ANIMAL forget all end
  c: CAT
do
    -- legal, CAT conforms to ANIMAL forget all
  a := c
 
    -- illegal, ANIMAL forget all doesn't have a feature eat
  a.eat (food)
 
end