trait Types extends AnyRef
EXPERIMENTAL
A trait that defines types and operations on them.
Type instances represent information about the type of a corresponding symbol. This includes its members (methods, fields, type parameters, nested classes, traits, etc.) either declared directly or inherited, its base types, its erasure and so on. Types also provide operations to test for type conformance or equivalence or for widening.
To instantiate a type, most of the time, the scala.reflect.api.TypeTags#typeOf method can be used. It takes
a type argument and produces a Type
instance which represents that argument. For example:
scala> typeOf[List[Int]] res0: reflect.runtime.universe.Type = scala.List[Int]
In this example, a scala.reflect.api.Types#TypeRef is returned, which corresponds to the type constructor List
applied to the type argument Int
.
In the case of a generic type, you can also combine it with other types using scala.reflect.api.Types#appliedType. For example:
scala> val intType = typeOf[Int] intType: reflect.runtime.universe.Type = Int scala> val listType = typeOf[List[_]] listType: reflect.runtime.universe.Type = List[_] scala> appliedType(listType.typeConstructor, intType) res0: reflect.runtime.universe.Type = List[Int]
Note: Method typeOf
does not work for types with type parameters, such as typeOf[List[A]]
where A
is
a type parameter. In this case, use scala.reflect.api.TypeTags#weakTypeOf instead.
For other ways to instantiate types, see the corresponding section of the Reflection Guide.
Common Operations on Types
Types are typically used for type conformance tests or are queried for declarations of members or inner types.
- Subtyping Relationships can be tested using
<:<
andweak_<:<
. - Type Equality can be checked with
=:=
. It's important to note that==
should not be used to compare types for equality--==
can't check for type equality in the presence of type aliases, while=:=
can.
Types can be queried for members and declarations by using the members
and declarations
methods (along with
their singular counterparts member
and declaration
), which provide the list of definitions associated with that type.
For example, to look up the map
method of List
, one can do:
scala> typeOf[List[_]].member(TermName("map")) res1: reflect.runtime.universe.Symbol = method map
For more information about Type
s, see the Reflection Guide: Symbols, Trees, and Types
- Self Type
- Universe
- Source
- Types.scala
- Grouped
- Alphabetic
- By Inheritance
- Types
- AnyRef
- Any
- by any2stringadd
- by StringFormat
- by Ensuring
- by ArrowAssoc
- Hide All
- Show All
- Public
- Protected
Type Members
- abstract type AnnotatedType >: Null <: Universe.AnnotatedTypeApi with Universe.Type
The
AnnotatedType
type signature is used for annotated types of the for<type> @<annotation>
. - trait AnnotatedTypeApi extends Universe.TypeApi
The API that all annotated types support.
The API that all annotated types support. The main source of information about types is the scala.reflect.api.Types page.
- abstract class AnnotatedTypeExtractor extends AnyRef
An extractor class to create and pattern match with syntax
AnnotatedType(annotations, underlying)
.An extractor class to create and pattern match with syntax
AnnotatedType(annotations, underlying)
. Here,annotations
are the annotations decorating the underlying typeunderlying
.selfSym
is a symbol representing the annotated type itself. - abstract type BoundedWildcardType >: Null <: Universe.BoundedWildcardTypeApi with Universe.Type
BoundedWildcardTypes, used only during type inference, are created in two places:
BoundedWildcardTypes, used only during type inference, are created in two places:
- If the expected type of an expression is an existential type, its hidden symbols are replaced with bounded wildcards. 2. When an implicit conversion is being sought based in part on the name of a method in the converted type, a HasMethodMatching type is created: a MethodType with parameters typed as BoundedWildcardTypes.
- trait BoundedWildcardTypeApi extends Universe.TypeApi
The API that all this types support.
The API that all this types support. The main source of information about types is the scala.reflect.api.Types page.
- abstract class BoundedWildcardTypeExtractor extends AnyRef
An extractor class to create and pattern match with syntax
BoundedWildcardTypeExtractor(bounds)
withbounds
denoting the type bounds. - abstract type ClassInfoType >: Null <: Universe.ClassInfoTypeApi with Universe.CompoundType
The
ClassInfo
type signature is used to define parents and declarations of classes, traits, and objects.The
ClassInfo
type signature is used to define parents and declarations of classes, traits, and objects. If a class, trait, or object C is declared like thisC extends P_1 with ... with P_m { D_1; ...; D_n}
its
ClassInfo
type has the following form:ClassInfo(List(P_1, ..., P_m), Scope(D_1, ..., D_n), C)
- trait ClassInfoTypeApi extends Universe.TypeApi
The API that all class info types support.
The API that all class info types support. The main source of information about types is the scala.reflect.api.Types page.
- abstract class ClassInfoTypeExtractor extends AnyRef
An extractor class to create and pattern match with syntax
ClassInfo(parents, decls, clazz)
Here,parents
is the list of parent types of the class,decls
is the scope containing all declarations in the class, andclazz
is the symbol of the class itself. - abstract type CompoundType >: Null <: Universe.CompoundTypeApi with Universe.Type
A subtype of Type representing refined types as well as
ClassInfo
signatures. - trait CompoundTypeApi extends AnyRef
Has no special methods.
Has no special methods. Is here to provides erased identity for
CompoundType
. - abstract type ConstantType >: Null <: Universe.ConstantTypeApi with Universe.SingletonType
A
ConstantType
type cannot be expressed in user programs; it is inferred as the type of a constant.A
ConstantType
type cannot be expressed in user programs; it is inferred as the type of a constant. Here are some constants with their types and the internal string representation:1 ConstantType(Constant(1)) Int(1) "abc" ConstantType(Constant("abc")) String("abc")
ConstantTypes denote values that may safely be constant folded during type checking. The
deconst
operation returns the equivalent type that will not be constant folded. - trait ConstantTypeApi extends Universe.TypeApi
The API that all constant types support.
The API that all constant types support. The main source of information about types is the scala.reflect.api.Types page.
- abstract class ConstantTypeExtractor extends AnyRef
An extractor class to create and pattern match with syntax
ConstantType(constant)
Here,constant
is the constant value represented by the type. - abstract type ExistentialType >: Null <: Universe.ExistentialTypeApi with Universe.Type
The
ExistentialType
type signature is used for existential types and wildcard types. - trait ExistentialTypeApi extends Universe.TypeApi
The API that all existential types support.
The API that all existential types support. The main source of information about types is the scala.reflect.api.Types page.
- abstract class ExistentialTypeExtractor extends AnyRef
An extractor class to create and pattern match with syntax
ExistentialType(quantified, underlying)
.An extractor class to create and pattern match with syntax
ExistentialType(quantified, underlying)
. Here,quantified
are the type variables bound by the existential type andunderlying
is the type that's existentially quantified. - abstract type MethodType >: Null <: Universe.MethodTypeApi with Universe.Type
The
MethodType
type signature is used to indicate parameters and result type of a method - trait MethodTypeApi extends Universe.TypeApi
The API that all method types support.
The API that all method types support. The main source of information about types is the scala.reflect.api.Types page.
- abstract class MethodTypeExtractor extends AnyRef
An extractor class to create and pattern match with syntax
MethodType(params, restpe)
Here,params
is a potentially empty list of parameter symbols of the method, andrestpe
is the result type of the method.An extractor class to create and pattern match with syntax
MethodType(params, restpe)
Here,params
is a potentially empty list of parameter symbols of the method, andrestpe
is the result type of the method. If the method is curried,restpe
would be anotherMethodType
. Note:MethodType(Nil, Int)
would be the type of a method defined with an empty parameter list.def f(): Int
If the method is completely parameterless, as in
def f: Int
its type is a
NullaryMethodType
. - abstract type NullaryMethodType >: Null <: Universe.NullaryMethodTypeApi with Universe.Type
The
NullaryMethodType
type signature is used for parameterless methods with declarations of the formdef foo: T
- trait NullaryMethodTypeApi extends Universe.TypeApi
The API that all nullary method types support.
The API that all nullary method types support. The main source of information about types is the scala.reflect.api.Types page.
- abstract class NullaryMethodTypeExtractor extends AnyRef
An extractor class to create and pattern match with syntax
NullaryMethodType(resultType)
.An extractor class to create and pattern match with syntax
NullaryMethodType(resultType)
. Here,resultType
is the result type of the parameterless method. - abstract type PolyType >: Null <: Universe.PolyTypeApi with Universe.Type
The
PolyType
type signature is used for polymorphic methods that have at least one type parameter. - trait PolyTypeApi extends Universe.TypeApi
The API that all polymorphic types support.
The API that all polymorphic types support. The main source of information about types is the scala.reflect.api.Types page.
- abstract class PolyTypeExtractor extends AnyRef
An extractor class to create and pattern match with syntax
PolyType(typeParams, resultType)
.An extractor class to create and pattern match with syntax
PolyType(typeParams, resultType)
. Here,typeParams
are the type parameters of the method andresultType
is the type signature following the type parameters. - abstract type RefinedType >: Null <: Universe.RefinedTypeApi with Universe.CompoundType
The
RefinedType
type defines types of any of the forms on the left, with their RefinedType representations to the right.The
RefinedType
type defines types of any of the forms on the left, with their RefinedType representations to the right.P_1 with ... with P_m { D_1; ...; D_n} RefinedType(List(P_1, ..., P_m), Scope(D_1, ..., D_n)) P_1 with ... with P_m RefinedType(List(P_1, ..., P_m), Scope()) { D_1; ...; D_n} RefinedType(List(AnyRef), Scope(D_1, ..., D_n))
- trait RefinedTypeApi extends Universe.TypeApi
The API that all refined types support.
The API that all refined types support. The main source of information about types is the scala.reflect.api.Types page.
- abstract class RefinedTypeExtractor extends AnyRef
An extractor class to create and pattern match with syntax
RefinedType(parents, decls)
Here,parents
is the list of parent types of the class, anddecls
is the scope containing all declarations in the class. - abstract type SingleType >: Null <: Universe.SingleTypeApi with Universe.SingletonType
The
SingleType
type describes types of any of the forms on the left, with their TypeRef representations to the right.The
SingleType
type describes types of any of the forms on the left, with their TypeRef representations to the right.(T # x).type SingleType(T, x) p.x.type SingleType(p.type, x) x.type SingleType(NoPrefix, x)
- trait SingleTypeApi extends Universe.TypeApi
The API that all single types support.
The API that all single types support. The main source of information about types is the scala.reflect.api.Types page.
- abstract class SingleTypeExtractor extends AnyRef
An extractor class to create and pattern match with syntax
SingleType(pre, sym)
Here,pre
is the prefix of the single-type, andsym
is the stable value symbol referred to by the single-type. - abstract type SingletonType >: Null <: Universe.SingletonTypeApi with Universe.Type
The type of Scala singleton types, i.e., types that are inhabited by only one nun-null value.
The type of Scala singleton types, i.e., types that are inhabited by only one nun-null value. These include types of the forms
C.this.type C.super.type x.type
as well as constant types.
- trait SingletonTypeApi extends AnyRef
Has no special methods.
Has no special methods. Is here to provides erased identity for
SingletonType
. - abstract type SuperType >: Null <: Universe.SuperTypeApi with Universe.SingletonType
The
SuperType
type is not directly written, but arises whenC.super
is used as a prefix in aTypeRef
orSingleType
.The
SuperType
type is not directly written, but arises whenC.super
is used as a prefix in aTypeRef
orSingleType
. Its internal presentation isSuperType(thistpe, supertpe)
Here,
thistpe
is the type of the corresponding this-type. For instance, in the type arising from C.super, thethistpe
part would beThisType(C)
.supertpe
is the type of the super class referred to by thesuper
. - trait SuperTypeApi extends Universe.TypeApi
The API that all super types support.
The API that all super types support. The main source of information about types is the scala.reflect.api.Types page.
- abstract class SuperTypeExtractor extends AnyRef
An extractor class to create and pattern match with syntax
SuperType(thistpe, supertpe)
- abstract type ThisType >: Null <: Universe.ThisTypeApi with Universe.SingletonType
A singleton type that describes types of the form on the left with the corresponding
ThisType
representation to the right:A singleton type that describes types of the form on the left with the corresponding
ThisType
representation to the right:C.this.type ThisType(C)
- trait ThisTypeApi extends Universe.TypeApi
The API that all this types support.
The API that all this types support. The main source of information about types is the scala.reflect.api.Types page.
- abstract class ThisTypeExtractor extends AnyRef
An extractor class to create and pattern match with syntax
ThisType(sym)
wheresym
is the class prefix of the this type. - abstract type Type >: Null <: Universe.TypeApi
The type of Scala types, and also Scala type signatures.
The type of Scala types, and also Scala type signatures. (No difference is internally made between the two).
- abstract class TypeApi extends AnyRef
The API of types.
The API of types. The main source of information about types is the scala.reflect.api.Types page.
- abstract type TypeBounds >: Null <: Universe.TypeBoundsApi with Universe.Type
The
TypeBounds
type signature is used to indicate lower and upper type bounds of type parameters and abstract types.The
TypeBounds
type signature is used to indicate lower and upper type bounds of type parameters and abstract types. It is not a first-class type. If an abstract type or type parameter is declared with any of the forms on the left, its type signature is the TypeBounds type on the right.T >: L <: U TypeBounds(L, U) T >: L TypeBounds(L, Any) T <: U TypeBounds(Nothing, U)
- trait TypeBoundsApi extends Universe.TypeApi
The API that all type bounds support.
The API that all type bounds support. The main source of information about types is the scala.reflect.api.Types page.
- abstract class TypeBoundsExtractor extends AnyRef
An extractor class to create and pattern match with syntax
TypeBound(lower, upper)
Here,lower
is the lower bound of theTypeBounds
pair, andupper
is the upper bound. - abstract type TypeRef >: Null <: Universe.TypeRefApi with Universe.Type
The
TypeRef
type describes types of any of the forms on the left, with their TypeRef representations to the right.The
TypeRef
type describes types of any of the forms on the left, with their TypeRef representations to the right.T # C[T_1, ..., T_n] TypeRef(T, C, List(T_1, ..., T_n)) p.C[T_1, ..., T_n] TypeRef(p.type, C, List(T_1, ..., T_n)) C[T_1, ..., T_n] TypeRef(NoPrefix, C, List(T_1, ..., T_n)) T # C TypeRef(T, C, Nil) p.C TypeRef(p.type, C, Nil) C TypeRef(NoPrefix, C, Nil)
- trait TypeRefApi extends Universe.TypeApi
The API that all type refs support.
The API that all type refs support. The main source of information about types is the scala.reflect.api.Types page.
- abstract class TypeRefExtractor extends AnyRef
An extractor class to create and pattern match with syntax
TypeRef(pre, sym, args)
Here,pre
is the prefix of the type reference,sym
is the symbol referred to by the type reference, andargs
is a possible empty list of type arguments.
Abstract Value Members
- abstract val AnnotatedType: Universe.AnnotatedTypeExtractor
The constructor/extractor for
AnnotatedType
instances. - abstract val BoundedWildcardType: Universe.BoundedWildcardTypeExtractor
The constructor/extractor for
BoundedWildcardType
instances. - abstract val ClassInfoType: Universe.ClassInfoTypeExtractor
The constructor/extractor for
ClassInfoType
instances. - abstract val ConstantType: Universe.ConstantTypeExtractor
The constructor/extractor for
ConstantType
instances. - abstract val ExistentialType: Universe.ExistentialTypeExtractor
The constructor/extractor for
ExistentialType
instances. - abstract val MethodType: Universe.MethodTypeExtractor
The constructor/extractor for
MethodType
instances. - abstract val NoPrefix: Universe.Type
This constant is used as a special value denoting the empty prefix in a path dependent type.
This constant is used as a special value denoting the empty prefix in a path dependent type. For instance
x.type
is represented asSingleType(NoPrefix, <x>)
, where<x>
stands for the symbol forx
. - abstract val NoType: Universe.Type
This constant is used as a special value that indicates that no meaningful type exists.
- abstract val NullaryMethodType: Universe.NullaryMethodTypeExtractor
The constructor/extractor for
NullaryMethodType
instances. - abstract val PolyType: Universe.PolyTypeExtractor
The constructor/extractor for
PolyType
instances. - abstract val RefinedType: Universe.RefinedTypeExtractor
The constructor/extractor for
RefinedType
instances. - abstract val SingleType: Universe.SingleTypeExtractor
The constructor/extractor for
SingleType
instances. - abstract val SuperType: Universe.SuperTypeExtractor
The constructor/extractor for
SuperType
instances. - abstract val ThisType: Universe.ThisTypeExtractor
The constructor/extractor for
ThisType
instances. - abstract val TypeBounds: Universe.TypeBoundsExtractor
The constructor/extractor for
TypeBounds
instances. - abstract val TypeRef: Universe.TypeRefExtractor
The constructor/extractor for
TypeRef
instances. - abstract val WildcardType: Universe.Type
An object representing an unknown type, used during type inference.
An object representing an unknown type, used during type inference. If you see WildcardType outside of inference it is almost certainly a bug.
- abstract def appliedType(sym: Universe.Symbol, args: Universe.Type*): Universe.Type
- See also
- abstract def appliedType(sym: Universe.Symbol, args: List[Universe.Type]): Universe.Type
- See also
- abstract def appliedType(tycon: Universe.Type, args: Universe.Type*): Universe.Type
- See also
- abstract def appliedType(tycon: Universe.Type, args: List[Universe.Type]): Universe.Type
A creator for type applications.
A creator for type applications.
Useful to combine and create types out of generic ones. For example:
scala> val boolType = typeOf[Boolean] boolType: reflect.runtime.universe.Type = Boolean scala> val optionType = typeOf[Option[_]] optionType: reflect.runtime.universe.Type = Option[_] scala> appliedType(optionType.typeConstructor, boolType) res0: reflect.runtime.universe.Type = Option[Boolean]
- abstract def glb(ts: List[Universe.Type]): Universe.Type
The greatest lower bound of a list of types, as determined by
<:<
. - abstract def lub(xs: List[Universe.Type]): Universe.Type
The least upper bound of a list of types, as determined by
<:<
.
Concrete Value Members
- final def !=(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- final def ##: Int
- Definition Classes
- AnyRef → Any
- def +(other: String): String
- def ->[B](y: B): (Types, B)
- final def ==(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- final def asInstanceOf[T0]: T0
- Definition Classes
- Any
- def clone(): AnyRef
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.CloneNotSupportedException]) @native()
- def ensuring(cond: (Types) => Boolean, msg: => Any): Types
- def ensuring(cond: (Types) => Boolean): Types
- def ensuring(cond: Boolean, msg: => Any): Types
- def ensuring(cond: Boolean): Types
- final def eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- def equals(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef → Any
- def finalize(): Unit
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.Throwable])
- final def getClass(): Class[_ <: AnyRef]
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
- def hashCode(): Int
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
- final def isInstanceOf[T0]: Boolean
- Definition Classes
- Any
- final def ne(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- final def notify(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
- final def notifyAll(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
- final def synchronized[T0](arg0: => T0): T0
- Definition Classes
- AnyRef
- def toString(): String
- Definition Classes
- AnyRef → Any
- final def wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
- final def wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
- final def wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException]) @native()
Deprecated Value Members
- def formatted(fmtstr: String): String
- Implicit
- This member is added by an implicit conversion from Types toStringFormat[Types] performed by method StringFormat in scala.Predef.
- Definition Classes
- StringFormat
- Annotations
- @deprecated @inline()
- Deprecated
(Since version 2.12.16) Use
formatString.format(value)
instead ofvalue.formatted(formatString)
, or use thef""
string interpolator. In Java 15 and later,formatted
resolves to the new method in String which has reversed parameters.
- def →[B](y: B): (Types, B)
- Implicit
- This member is added by an implicit conversion from Types toArrowAssoc[Types] performed by method ArrowAssoc in scala.Predef.
- Definition Classes
- ArrowAssoc
- Annotations
- @deprecated
- Deprecated
(Since version 2.13.0) Use
->
instead. If you still wish to display it as one character, consider using a font with programming ligatures such as Fira Code.
Types - Operations
Types
API
The methods available for each reflection entity, without the implementation. Since the reflection entities are later overridden by runtime reflection and macros, their API counterparts guarantee a minimum set of methods that are implemented.
Extractors
Extractors provide the machinery necessary to allow pattern matching and construction of reflection entities that is similar to case classes, although the entities are only abstract types that are later overridden.