Difference between revisions of "Catcall checkpoints"

 
m (faces of covariance)
Line 14: Line 14:
  
 
class B [G]
 
class B [G]
inherit A [G]
+
inherit A [G] redefine f end
redefine f end
+
 
feature
 
feature
 
f (u: U) do end
 
f (u: U) do end

Revision as of 17:59, 23 June 2007

These examples should be considered for all catcall solutions:

faces of covariance

The following example shows the difference between generic covariance and non-generic covariance.

class A [G]
feature
	f (t: T) do end
	put (g: G) do end
end
 
class B [G]
inherit A [G] redefine f end
feature
	f (u: U) do end
end
 
local
  a_any: A [ANY]
  a_string: A [STRING]
  b_any: B [ANY]
  b_string: B [STRING]
  t: T
do
  a_any := a_string
  a_any.put (5) -- catcall for generic feature
  a_any.f (t) -- no catcall
 
  a_any := b_any
  a_any.put (5) -- no catcall
  a_any.f (t) -- catcall for non-generic feature
 
  a_any := b_string
  a_any.put (5) -- catcall for generic feature
  a_any.f (t) -- catcall for non-generic feature
end

A safe and expressive solution should have a way to allow all valid cases and prohibit the invalid ones. This needs a way to address the generic and non-generic features indiviually.

contravariance

class SORTER [G]
feature
  sort (a_list: LIST [G]; a_comparator: COMPARATOR [G])
    do
        -- Somewhere in the loop:
      a_comparator.compare (l_string_1, l_string_2)
    end
end
 
class EXAMPLE
feature
  make
    local
      l_list: LIST [STRING]
      l_string_sorter: SORTER [STRING]
      l_any_comparator: COMPARATOR [ANY]
    do
        -- Should be allowed to sort the string list with an
        -- ANY comparator
      l_string_sorter.sort (l_list, l_any_comparator)
    end
end

Solutions which pass this test are likely to be expressive for agents as well.