正在加载图片...
652 GLOBAL OBJECTS AND CONSTANTS $18.4 Once functions,anchoring and genericity (This section addresses a specific technical point and may be skipped on first reading.) Once functions of class types carry a potential incompatibility with anchored types and genericity. Let us start with genericity.In a generic class EXAMPLE [G]assume a once function returning a value whose type is the formal generic parameter: f:G is once...end Warning:not valid. See below. and consider a possible use: character_example:EXAMPLE [CHARACTER] print (character example.f) So far so good.But you also try to do something with another generic derivation: integer_example:EXAMPLE [INTEGER] print (integer example.f+1) The last instruction adds two integer values.Unfortunately,the first of them,the result of calling /has already been computed since is a once function;and it is a character,not an integer.The addition is not valid. The problem is that we are sharing a value between different generic derivations which expect the type of that value to depend on the actual generic parameter. A similar issue arises with anchored types.Assume a class B which adds an attribute to the features of its parent A: class B inherit A feature attribute of B:INTEGER end Assume that 4 had a once function /returning a result of anchored type: f:like Current is once !Result.make end Warning:not valid. See below. and that the first evaluation of is in a2:=al.f with a/and a2 of type 4.The evaluation of/creates a direct instance ofA,and attaches it to entity a2,also of type 4.Fine.But assume now that a subsequent use of/is b2:=bl.f where b/and b2 are of type B.If/were a non-once function,this would not cause any problem,since the call would now produce and return a direct instance of B.Since here we have a once function,the result has already been computed through the first call;and that result is a direct instance of A,not B.So an instruction such as print (b2.attribute of B)652 GLOBAL OBJECTS AND CONSTANTS §18.4 Once functions, anchoring and genericity (This section addresses a specific technical point and may be skipped on first reading.) Once functions of class types carry a potential incompatibility with anchored types and genericity. Let us start with genericity. In a generic class EXAMPLE [G] assume a once function returning a value whose type is the formal generic parameter: f: G is once … end and consider a possible use: character_example: EXAMPLE [CHARACTER] … print (character_example ● f) So far so good. But you also try to do something with another generic derivation: integer_example: EXAMPLE [INTEGER] … print (integer_example ● f + 1) The last instruction adds two integer values. Unfortunately, the first of them, the result of calling f, has already been computed since f is a once function; and it is a character, not an integer. The addition is not valid. The problem is that we are sharing a value between different generic derivations which expect the type of that value to depend on the actual generic parameter. A similar issue arises with anchored types. Assume a class B which adds an attribute to the features of its parent A: class B inherit A feature attribute_of_B: INTEGER end Assume that A had a once function f, returning a result of anchored type: f: like Current is once !! Result ● make end and that the first evaluation of f is in a2 := a1 ● f with a1 and a2 of type A. The evaluation of f creates a direct instance of A, and attaches it to entity a2, also of type A. Fine. But assume now that a subsequent use of f is b2 := b1 ● f where b1 and b2 are of type B. If f were a non-once function, this would not cause any problem, since the call would now produce and return a direct instance of B. Since here we have a once function, the result has already been computed through the first call; and that result is a direct instance of A, not B. So an instruction such as print (b2 ● attribute_of_B) Warning: not valid. See below. Warning: not valid. See below
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有