Multiple Return Type Syntax in Boo?

140 Views Asked by At

I'm trying to define a method in Boo which returns two things, but the compiler is spitting out the message:

expecting "COLON", found ','.

Here's how I tried defining the method:

from System.Collections.Generic import HashSet

# ValueParameter is a class defined elsewhere.
def evaluate(s as string, limit as string) as double, HashSet[of ValueParameter]:

I've looked through the docs, and though I see examples of how to return multiple items, I don't see any examples where they declare the return type as returning multiple types.

2

There are 2 best solutions below

1
Mason Wheeler On BEST ANSWER

I found the swap example on the wiki, which declares a function that uses multiple return values, and ran it through the compiler with the -p:boo flag, which outputs a code representation of the final form of the AST after all the processing has been done. It reports that the type of this function is (int). When you return two dissimilar types, such as your double and HashSet, the return type is (object).

0
theodox On

You can import Tuple from System and use that to return a tuple with types:

import System

def string_and_int(s as string, i as int) as Tuple[of string, int]:
    return Tuple[of string, int](s, i)

This preserves the type for each element correctly. However be warned that in Boo, anyway, the Tuple type is not iterable or sliceable so you have to get it using .Item1, .Item2 etc:

 example = string_and_int("s", 2)
 print example.Item1
 # 's'
 print example.Item2
 # 2