@Override public Optional<Type> getCommonSuperType(Type firstType, Type secondType) { TypeCompatibility compatibility = compatibility(firstType, secondType); if (!compatibility.isCompatible()) { return Optional.empty(); } return Optional.of(compatibility.getCommonSuperType()); }
private TypeCompatibility typeCompatibilityForCovariantParametrizedType(Type fromType, Type toType) { checkState(fromType.getClass().equals(toType.getClass())); ImmutableList.Builder<TypeSignatureParameter> commonParameterTypes = ImmutableList.builder(); List<Type> fromTypeParameters = fromType.getTypeParameters(); List<Type> toTypeParameters = toType.getTypeParameters(); if (fromTypeParameters.size() != toTypeParameters.size()) { return TypeCompatibility.incompatible(); } boolean coercible = true; for (int i = 0; i < fromTypeParameters.size(); i++) { TypeCompatibility compatibility = compatibility(fromTypeParameters.get(i), toTypeParameters.get(i)); if (!compatibility.isCompatible()) { return TypeCompatibility.incompatible(); } coercible &= compatibility.isCoercible(); commonParameterTypes.add(TypeSignatureParameter.of(compatibility.getCommonSuperType().getTypeSignature())); } String typeBase = fromType.getTypeSignature().getBase(); return TypeCompatibility.compatible(getType(new TypeSignature(typeBase, commonParameterTypes.build())), coercible); }
private TypeCompatibility typeCompatibilityForRow(RowType firstType, RowType secondType) { List<Field> firstFields = firstType.getFields(); List<Field> secondFields = secondType.getFields(); if (firstFields.size() != secondFields.size()) { return TypeCompatibility.incompatible(); } ImmutableList.Builder<RowType.Field> fields = ImmutableList.builder(); boolean coercible = true; for (int i = 0; i < firstFields.size(); i++) { Type firstFieldType = firstFields.get(i).getType(); Type secondFieldType = secondFields.get(i).getType(); TypeCompatibility typeCompatibility = compatibility(firstFieldType, secondFieldType); if (!typeCompatibility.isCompatible()) { return TypeCompatibility.incompatible(); } Type commonParameterType = typeCompatibility.getCommonSuperType(); Optional<String> firstParameterName = firstFields.get(i).getName(); Optional<String> secondParameterName = secondFields.get(i).getName(); Optional<String> commonName = firstParameterName.equals(secondParameterName) ? firstParameterName : Optional.empty(); // ignore parameter name for coercible coercible &= typeCompatibility.isCoercible(); fields.add(new RowType.Field(commonName, commonParameterType)); } return TypeCompatibility.compatible(RowType.from(fields.build()), coercible); }
if (coercedType.isPresent()) { TypeCompatibility typeCompatibility = compatibility(fromType, coercedType.get()); if (!typeCompatibility.isCompatible()) { return TypeCompatibility.incompatible();