CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-scala-lang--scala-reflect

Reflection Library for the Scala Programming Language providing runtime reflection capabilities and metaprogramming support

Pending
Overview
Eval results
Files

type-system.mddocs/

Type System

Comprehensive type system representation enabling type inspection, comparison, member lookup, and type transformations. The Scala reflection type system captures all aspects of Scala's rich type system including generics, path-dependent types, refinements, and existential types.

Capabilities

Base Type API

Core type operations and properties available on all type instances.

/**
 * Base type trait providing fundamental type operations
 */
trait TypeApi { this: Type =>
  /** Type equality - checks if two types are exactly the same */
  def =:=(that: Type): Boolean
  
  /** Subtype relation - checks if this type is a subtype of that type */
  def <:<(that: Type): Boolean
  
  /** Weak subtype relation - less strict subtyping for implicit conversions */
  def weak_<:<(that: Type): Boolean
  
  /** Get all members (fields, methods, nested classes) of this type */
  def members: MemberScope
  
  /** Get all declarations directly defined in this type */
  def decls: MemberScope
  
  /** Find a specific member by name */
  def member(name: Name): Symbol
  
  /** Find a specific declaration by name */
  def decl(name: Name): Symbol
  
  /** Remove type aliases and get the underlying type */
  def dealias: Type
  
  /** Widen singleton types and constant types */
  def widen: Type
  
  /** Get the erased type (JVM erasure) */
  def erasure: Type
  
  /** Normalize the type */
  def normalize: Type
  
  /** Get type arguments for parameterized types */
  def typeArgs: List[Type]
  
  /** Get type constructor for parameterized types */
  def typeConstructor: Type
  
  /** Get type parameters for polymorphic types */
  def typeParams: List[Symbol]
  
  /** Get base classes in linearization order */
  def baseClasses: List[Symbol]
  
  /** Check if this type conforms to another type */
  def conformsTo(that: Type): Boolean
  
  /** Get string representation */
  def toString: String
}

Specific Type Variants

Different kinds of types in the Scala type system.

/**
 * Singleton types representing specific values or paths
 */
abstract class SingletonType extends Type

/**
 * This-types representing C.this.type
 */
abstract class ThisType extends SingletonType {
  def sym: Symbol
}

/**
 * Single types representing path-dependent types like obj.type
 */  
abstract class SingleType extends SingletonType {
  def pre: Type
  def sym: Symbol
}

/**
 * Super types representing C.super[T].type
 */
abstract class SuperType extends SingletonType {
  def thistpe: Type  
  def supertpe: Type
}

/**
 * Constant types representing literal values
 */
abstract class ConstantType extends SingletonType {
  def value: Constant
}

/**
 * Type references like List[Int], String, etc.
 */
abstract class TypeRef extends Type {
  def pre: Type
  def sym: Symbol  
  def args: List[Type]
}

/**
 * Refined types like A with B { def foo: Int }
 */
abstract class RefinedType extends Type {
  def parents: List[Type]
  def decls: Scope
}

/**
 * Class information types
 */
abstract class ClassInfoType extends Type {
  def parents: List[Type]
  def decls: Scope
  def typeSymbol: Symbol
}

/**
 * Method types representing method signatures
 */
abstract class MethodType extends Type {
  def params: List[Symbol]
  def resultType: Type
}

/**
 * Polymorphic types with type parameters
 */
abstract class PolyType extends Type {
  def typeParams: List[Symbol]
  def resultType: Type
}

/**
 * Existential types with existentially quantified variables
 */
abstract class ExistentialType extends Type {
  def quantified: List[Symbol]
  def underlying: Type
}

/**
 * Annotated types carrying annotations
 */
abstract class AnnotatedType extends Type {
  def annotations: List[Annotation]
  def underlying: Type
}

/**
 * Type bounds like >: Lo <: Hi
 */
abstract class TypeBounds extends Type {
  def lo: Type
  def hi: Type
}

Type Factory Methods

Methods for creating and manipulating types.

/**
 * Create a type reference
 */
def TypeRef(pre: Type, sym: Symbol, args: List[Type]): Type

/**
 * Create a single type  
 */
def SingleType(pre: Type, sym: Symbol): Type

/**
 * Create a this type
 */
def ThisType(sym: Symbol): Type

/**
 * Create a refined type
 */
def RefinedType(parents: List[Type], decls: Scope): Type

/**
 * Create a method type
 */
def MethodType(params: List[Symbol], resultType: Type): Type

/**
 * Create a polymorphic type
 */
def PolyType(typeParams: List[Symbol], resultType: Type): Type

/**
 * Create type bounds
 */
def TypeBounds(lo: Type, hi: Type): Type

Usage Examples:

import scala.reflect.runtime.universe._

// Type equality and subtyping
val stringType = typeOf[String]
val anyType = typeOf[Any]
val intType = typeOf[Int]

println(stringType =:= typeOf[String])  // true
println(stringType <:< anyType)         // true  
println(intType <:< anyType)           // true

// Member lookup
val listIntType = typeOf[List[Int]]
val headMethod = listIntType.member(TermName("head"))
println(s"head method: $headMethod")

val allMembers = listIntType.members
allMembers.filter(_.isMethod).foreach { member =>
  println(s"Method: ${member.name} -> ${member.info}")
}

// Type manipulation
val listType = typeOf[List[String]]
println(s"Original: $listType")
println(s"Dealias: ${listType.dealias}")
println(s"Widen: ${listType.widen}")
println(s"Erasure: ${listType.erasure}")

// Type arguments and constructor
println(s"Type args: ${listType.typeArgs}")
println(s"Type constructor: ${listType.typeConstructor}")

Advanced Type Operations

Complex type operations for advanced use cases.

/**
 * Type substitution - replace type parameters with concrete types
 */
def substituteTypes(from: List[Symbol], to: List[Type]): Type

/**
 * Type transformation using a function
 */
def map(f: Type => Type): Type

/**
 * Find all types matching a predicate
 */
def collect[T](pf: PartialFunction[Type, T]): List[T]

/**
 * Check if type exists anywhere in the type tree
 */
def exists(p: Type => Boolean): Boolean

/**
 * Transform types recursively
 */
def transform(transformer: TypeTransformer): Type

/**
 * Get the least upper bound of two types
 */
def lub(tps: List[Type]): Type

/**
 * Get the greatest lower bound of two types  
 */
def glb(tps: List[Type]): Type

Usage Example - Type Substitution:

import scala.reflect.runtime.universe._

// Working with generic types
val listSymbol = typeOf[List[_]].typeSymbol
val typeParam = listSymbol.typeParams.head

// Create List[String] by substitution
val stringType = typeOf[String]  
val listStringType = typeOf[List[_]].substituteTypes(List(typeParam), List(stringType))
println(s"Substituted type: $listStringType")

Type Comparison and Analysis

/**
 * Advanced type relationship checks
 */
trait TypeRelations {
  /** Check if types are equivalent after erasure */
  def =~=(that: Type): Boolean
  
  /** Find common supertype */
  def lub(that: Type): Type
  
  /** Find common subtype */
  def glb(that: Type): Type
  
  /** Check type parameter variance */
  def isContravariant: Boolean
  def isCovariant: Boolean
  def isInvariant: Boolean
}

Complete Type Analysis Example:

import scala.reflect.runtime.universe._

def analyzeType(tpe: Type): Unit = {
  println(s"Analyzing type: $tpe")
  println(s"Type symbol: ${tpe.typeSymbol}")
  println(s"Type args: ${tpe.typeArgs}")
  println(s"Is final: ${tpe.typeSymbol.isFinal}")
  println(s"Is abstract: ${tpe.typeSymbol.isAbstract}")
  
  // Base classes
  println("Base classes:")
  tpe.baseClasses.foreach { cls =>
    println(s"  - ${cls.fullName}")
  }
  
  // Members by category
  val methods = tpe.members.filter(_.isMethod)
  val fields = tpe.members.filter(m => m.isTerm && !m.isMethod)
  val types = tpe.members.filter(_.isType)
  
  println(s"Methods: ${methods.size}")
  println(s"Fields: ${fields.size}")  
  println(s"Nested types: ${types.size}")
  
  // Type relationships
  println(s"Conforms to Any: ${tpe <:< typeOf[Any]}")
  println(s"Conforms to AnyRef: ${tpe <:< typeOf[AnyRef]}")
}

// Analyze different types
analyzeType(typeOf[String])
analyzeType(typeOf[List[Int]])
analyzeType(typeOf[Option[String]])

Install with Tessl CLI

npx tessl i tessl/maven-org-scala-lang--scala-reflect

docs

abstract-syntax-trees.md

expressions-reification.md

index.md

macro-development.md

mirrors-reflective-operations.md

runtime-reflection.md

symbol-system.md

type-system.md

type-tags-type-information.md

tile.json