0

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

Theis Hansen
  • 53
  • 1
  • 5

1 Answers1

0

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); 
);
Lausdahl
  • 61
  • 2
  • I can't get that to return each element from the set. When i run a test it only returns the first element. – Theis Hansen Oct 29 '19 at 07:53
  • Do you mean that you want successive calls to Scan to return one Brewage instance? Each call either has to return one, or return a subset or subsequence perhaps. If you want successive calls to return individual items, you have to maintain some state to say which values have already been returned. – Nick Battle Oct 29 '19 at 09:12
  • (Incidentally, you get that error because if the set of objects passed is empty, the for loop will drop through and the operation returns "void") – Nick Battle Oct 29 '19 at 09:13
  • Okay. So if i want to use a for all loop, then i first need to check if the set is non empty. – Theis Hansen Oct 30 '19 at 11:11
  • Or how should i approach this if i want to use the for all loop? – Theis Hansen Oct 31 '19 at 17:17