Skip to content

Commit

Permalink
Implementing support for function objects that only take one tuple pa…
Browse files Browse the repository at this point in the history
…ramter
  • Loading branch information
cpfr committed Nov 20, 2015
1 parent f71f8b8 commit 7c52f0c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/main/java/de/uni/bremen/monty/moco/visitor/ResolveVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,17 @@ public void visit(FunctionCall node) {
}
Declaration callableDeclaration = overloadResolution(argTypes, scope.resolveFunction(node.getIdentifier()));

// if the parameter is just one tuple, we might have to unpack it
if ((callableDeclaration == null) && (argTypes.size() == 1)
&& (argTypes.get(0).getIdentifier().getSymbol().startsWith("Tuple"))) {
List<ClassDeclaration> argCls = ((ClassDeclarationVariation) argTypes.get(0)).getConcreteGenericTypes();
argTypes = new ArrayList<>(argCls.size());
for (ClassDeclaration cls : argCls) {
argTypes.add(cls);
}
callableDeclaration = overloadResolution(argTypes, scope.resolveFunction(node.getIdentifier()));
}

if (callableDeclaration instanceof FunctionDeclaration) {
FunctionDeclaration function = (FunctionDeclaration) callableDeclaration;
node.setDeclaration(function);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Testing: 1-Tuple function objects without 1-Tuples
//
// Expected Output: 1\n8\n

// a function that takes exactly one tuple
Int takeFirst( (Int, Int) t):
return t._1
// and a function variable that points to this function
(Int, Int) -> Int fn := takeFirst

// call the function the standard way
println(takeFirst((1,2)))
// call the function variable with a tuple
println( fn((8,2)) )
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
1
8

0 comments on commit 7c52f0c

Please sign in to comment.