Can't return each element from a set when running it through a for all loop

151 Views Asked by At

I am trying to return each element from a set containing Brewage objects by running it through a for all loop. But the return value gets messed up when i try to do so.

The loop function:

public pure Scan: set of Brewage ==> Brewage
Scan(brewage) == 
for all q in set brewage do 
return q

Brewage Constructor:

public StringType = seq of char;
public StringLabel = seq of char;
public Char = char; 

instance variables
type : StringType;
label : StringLabel;
deposit : Char;

operations

public Brewage: StringType * StringLabel * Char  ==> Brewage
Brewage(ty, la, de) ==
(   type    := ty;
    label   := la;
    deposit := de
);

The error i get is in the Scan function and is the following:

Operation returns void value. Actual: (() | Brewage) Expected: Brewage

1

There are 1 best solutions below

5
On

You can use a let binding to select a Brewage in the set and then return it:

public pure Scan: set of Brewage ==> Brewage
Scan(brewage) == let b in set brewage in return b;

public main: ()==>Brewage
main()==
(
  let s = { new Brewage(), new Brewage() } in
    return Scan(s); 
);