From 237dfb95f61f572bcc45cf98fcb2c1b3af473cac Mon Sep 17 00:00:00 2001 From: Jon Bratseth Date: Thu, 1 Feb 2018 12:53:32 +0100 Subject: Allow type generalizations in if --- .../searchlib/rankingexpression/RankingExpression.java | 12 ++++++++++++ .../com/yahoo/searchlib/rankingexpression/rule/IfNode.java | 14 ++++++++------ 2 files changed, 20 insertions(+), 6 deletions(-) (limited to 'searchlib/src/main/java') diff --git a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/RankingExpression.java b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/RankingExpression.java index 1ec6ea4693b..c8d90e8c4e8 100755 --- a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/RankingExpression.java +++ b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/RankingExpression.java @@ -8,6 +8,8 @@ import com.yahoo.searchlib.rankingexpression.parser.RankingExpressionParser; import com.yahoo.searchlib.rankingexpression.parser.TokenMgrError; import com.yahoo.searchlib.rankingexpression.rule.ExpressionNode; import com.yahoo.searchlib.rankingexpression.rule.SerializationContext; +import com.yahoo.tensor.TensorType; +import com.yahoo.tensor.evaluation.TypeContext; import java.io.File; import java.io.FileNotFoundException; @@ -264,6 +266,16 @@ public class RankingExpression implements Serializable { return "rankingExpression(" + expressionName + ").rankingScript"; } + /** + * Validates the type correctness of the given expression with the given context and + * returns the type this expression will produce from the given type context + * + * @throws IllegalArgumentException if this expression is not type correct in this context + */ + public TensorType type(TypeContext context) { + return root.type(context); + } + /** * Returns the value of evaluating this expression over the given context. * diff --git a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/IfNode.java b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/IfNode.java index 076df327044..4f0ebc1c7e5 100755 --- a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/IfNode.java +++ b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/IfNode.java @@ -49,7 +49,7 @@ public final class IfNode extends CompositeNode { @Override public List children() { - List children = new ArrayList(4); + List children = new ArrayList<>(4); children.add(condition); children.add(trueExpression); children.add(falseExpression); @@ -78,11 +78,13 @@ public final class IfNode extends CompositeNode { public TensorType type(TypeContext context) { TensorType trueType = trueExpression.type(context); TensorType falseType = falseExpression.type(context); - if ( ! trueType.equals(falseType)) - throw new IllegalArgumentException("An if expression must produce a value of the same type in both " + - "alternatives, but the 'true' type is " + trueType + " while the " + - "'false' type is " + falseType); - return trueType; + + // Types of each branch must be compatible; the resulting type is the most general + if (trueType.isAssignableTo(falseType)) return falseType; + if (falseType.isAssignableTo(trueType)) return trueType; + throw new IllegalArgumentException("An if expression must produce compatible types in both " + + "alternatives, but the 'true' type is " + trueType + " while the " + + "'false' type is " + falseType); } @Override -- cgit v1.2.3