Newsgroups: comp.object,comp.lang.eiffel,comp.lang.sather,comp.lang.beta Path: news.daimi.aau.dk!news.uni-c.dk!sunic!news.funet.fi!news.eunet.fi!EU.net!howland.reston.ans.net!math.ohio-state.edu!news.acns.nwu.edu!ftpbox!mothost!schbbs!news From: shang@corp.mot.com (David L. Shang) Subject: Re: Cows, Hogs & Farms Reply-To: shang@corp.mot.com Organization: MOTOROLA Date: Tue, 17 Jan 1995 16:12:07 GMT Message-ID: <1995Jan17.161207.24578@schbbs.mot.com> References: <3f9mav$7bf@network.ucsd.edu> Sender: news@schbbs.mot.com (SCHBBS News Account) Nntp-Posting-Host: 129.188.128.126 Lines: 106 Xref: news.daimi.aau.dk comp.object:23946 comp.lang.eiffel:7217 comp.lang.sather:1444 comp.lang.beta:223 In article <3f9mav$7bf@network.ucsd.edu> mbk@inls1.ucsd.edu (Matt Kennel) writes: > -- Sather > > type $HERBIVORE is > try(food:$PLANT); > end; > > type $HERBIVORE{MYFOOD < $PLANT} is > try(food:$PLANT); > eat(food:MYFOOD); > end; > Does Sather consider the above two types different? If so, does Sather allow two different type in the same scope with a same name? In general, the second type should be a subtype of the first, as it is shown in the following Cluster program: class HERBIVORE declare proc try(food:$PLANT); end; class HERBIVORE_ON_FOOD is HERBIVORE [MYFOOD <: PLANT] declare proc eat(food:MYFOOD); end; > > class COW < $HERBIVORE{GRASS} is > -- blah blah blah implementation > end; > > class HOG < $HERBIVORE{CORN} is > -- implementation > end; > > type $IOWA_FARM_ANIMAL > COW,HOG is > try(food:$PLANT); > end; > I believe that defining a superclass in terms of a list of exisiting subclasses is sometimes a useful feature. Cluster-2 does this by traditional "union" type. For example: class IOWA_FARM_ANIMAL is union {COW, HOG}; IOWA_FARM_ANIMAL -- union{COW,HOG} -- is a superclass of COW or HOG, but a subclass of the LCA of COW and HOG, i.e., a subclass of HERBIVORE[MYFOOD]. > class IOWAFARM is > -- has hogs and cows. > > attr animals: LIST{$FARM_ANIMAL} ^^^^^^^^^^^ I suppose IOWA_FARM_ANIMAL here. > attr food : LIST{$PLANT}; > > feed is > loop > animals.elt!.try(food.elt!); > end; > end; > > feed_corn is > -- Cluster does this one a little cleaner > loop > f ::= food.elt!; > a ::= animals.elt!; > typecase f > when CORN > typecase a > when $HERBIVORE{CORN} then a.feed(f); end; ^^^^ I suppose "eat" here. In general, you are not necessary to know the exact type of animal and the exact type of the food. What you really want to make sure is that whether the food is the type that the animal accepts. To maintain a mixed farm like this, you have to achieve the following goals: 1. do not waste food; 2. let each animal have food; and 3. for efficeincy, do not "try" blindly. I have given a Cluster example in my previous post. > end; > end; > end; > end; > > In general Cluster lets you get at the foodtype for animals for run-time > selection OUTSIDE their class easily, That's true. > in Sather it's usually easier to put the typecase inside the 'try'. > It is equally easy in Cluster to put the typecase inside the 'try'. David Shang