/** * A {@link UnionType} contains a given type (alternate) iff the member * vector contains it. * * @param type The alternate which might be in this union. * * @return {@code true} if the alternate is in the union */ public boolean contains(JSType type) { return anyMatch(type::isEquivalentTo, alternatesRetainingStructuralSubtypes); }
/** * Tests whether this type is voidable. */ @Override public boolean isVoidable() { return anyMatch(JSType::isVoidable, alternatesRetainingStructuralSubtypes); }
/** * This predicate is used to test whether a given type can appear in a * numeric context, such as an operand of a multiply operator. * * @return true if the type can appear in a numeric context. */ @Override public boolean matchesNumberContext() { // TODO(user): Reverse this logic to make it correct instead of generous. return anyMatch(JSType::matchesNumberContext, alternatesRetainingStructuralSubtypes); }
/** * This predicate is used to test whether a given type can appear in a {@code Symbol} context * * @return {@code true} if not it maybe a symbol or Symbol object */ @Override public boolean matchesSymbolContext() { // TODO(user): Reverse this logic to make it correct instead of generous. return anyMatch(JSType::matchesSymbolContext, alternatesRetainingStructuralSubtypes); }
/** * This predicate determines whether objects of this type can have the * {@code null} value, and therefore can appear in contexts where * {@code null} is expected. * * @return {@code true} for everything but {@code Number} and * {@code Boolean} types. */ @Override public boolean isNullable() { return anyMatch(JSType::isNullable, alternatesRetainingStructuralSubtypes); }
/** Tests whether this type explicitly allows undefined (as opposed to ? or *). */ @Override public boolean isExplicitlyVoidable() { return anyMatch(JSType::isExplicitlyVoidable, alternatesRetainingStructuralSubtypes); }
@Override public boolean hasAnyTemplateTypesInternal() { return anyMatch(JSType::hasAnyTemplateTypes, alternatesRetainingStructuralSubtypes); }
@Override public boolean isUnknownType() { return anyMatch(JSType::isUnknownType, alternatesRetainingStructuralSubtypes); }
/** * This predicate is used to test whether a given type can appear in a * {@code String} context, such as an operand of a string concat ({@code +}) * operator.<p> * * All types have at least the potential for converting to {@code String}. * When we add externally defined types, such as a browser OM, we may choose * to add types that do not automatically convert to {@code String}. * * @return {@code true} if not {@link VoidType} */ @Override public boolean matchesStringContext() { // TODO(user): Reverse this logic to make it correct instead of generous. return anyMatch(JSType::matchesStringContext, alternatesRetainingStructuralSubtypes); }
/** * This predicate is used to test whether a given type can appear in an * {@code Object} context, such as the expression in a {@code with} * statement.<p> * * Most types we will encounter, except notably {@code null}, have at least * the potential for converting to {@code Object}. Host defined objects can * get peculiar.<p> * * VOID type is included here because while it is not part of the JavaScript * language, functions returning 'void' type can't be used as operands of * any operator or statement.<p> * * @return {@code true} if the type is not {@link NullType} or * {@link VoidType} */ @Override public boolean matchesObjectContext() { // TODO(user): Reverse this logic to make it correct instead of generous. return anyMatch(JSType::matchesObjectContext, alternatesRetainingStructuralSubtypes); }
/** * Gets the alternate types of this union type. * * @return The alternate types of this union type. The returned set is immutable. */ public ImmutableList<JSType> getAlternates() { if (anyMatch(JSType::isUnionType, alternatesRetainingStructuralSubtypes)) { rebuildAlternates(); } return alternatesCollapsingStructuralSubtypes; }
/** * Gets the alternate types of this union type, including structural interfaces and implicit * implementations as distinct alternatesCollapsingStructuralSubtypes. * * @return The alternate types of this union type. The returned set is immutable. */ public ImmutableList<JSType> getAlternatesWithoutStructuralTyping() { if (anyMatch(JSType::isUnionType, alternatesRetainingStructuralSubtypes)) { rebuildAlternates(); } return alternatesRetainingStructuralSubtypes; }
@Override public boolean isStruct() { return anyMatch(JSType::isStruct, getAlternates()); }
@Override public boolean isDict() { return anyMatch(JSType::isDict, getAlternates()); }