or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

abstract-syntax-trees.mdexpressions-reification.mdindex.mdmacro-development.mdmirrors-reflective-operations.mdruntime-reflection.mdsymbol-system.mdtype-system.mdtype-tags-type-information.md

type-system.mddocs/

0

# Type System

1

2

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.

3

4

## Capabilities

5

6

### Base Type API

7

8

Core type operations and properties available on all type instances.

9

10

```scala { .api }

11

/**

12

* Base type trait providing fundamental type operations

13

*/

14

trait TypeApi { this: Type =>

15

/** Type equality - checks if two types are exactly the same */

16

def =:=(that: Type): Boolean

17

18

/** Subtype relation - checks if this type is a subtype of that type */

19

def <:<(that: Type): Boolean

20

21

/** Weak subtype relation - less strict subtyping for implicit conversions */

22

def weak_<:<(that: Type): Boolean

23

24

/** Get all members (fields, methods, nested classes) of this type */

25

def members: MemberScope

26

27

/** Get all declarations directly defined in this type */

28

def decls: MemberScope

29

30

/** Find a specific member by name */

31

def member(name: Name): Symbol

32

33

/** Find a specific declaration by name */

34

def decl(name: Name): Symbol

35

36

/** Remove type aliases and get the underlying type */

37

def dealias: Type

38

39

/** Widen singleton types and constant types */

40

def widen: Type

41

42

/** Get the erased type (JVM erasure) */

43

def erasure: Type

44

45

/** Normalize the type */

46

def normalize: Type

47

48

/** Get type arguments for parameterized types */

49

def typeArgs: List[Type]

50

51

/** Get type constructor for parameterized types */

52

def typeConstructor: Type

53

54

/** Get type parameters for polymorphic types */

55

def typeParams: List[Symbol]

56

57

/** Get base classes in linearization order */

58

def baseClasses: List[Symbol]

59

60

/** Check if this type conforms to another type */

61

def conformsTo(that: Type): Boolean

62

63

/** Get string representation */

64

def toString: String

65

}

66

```

67

68

### Specific Type Variants

69

70

Different kinds of types in the Scala type system.

71

72

```scala { .api }

73

/**

74

* Singleton types representing specific values or paths

75

*/

76

abstract class SingletonType extends Type

77

78

/**

79

* This-types representing C.this.type

80

*/

81

abstract class ThisType extends SingletonType {

82

def sym: Symbol

83

}

84

85

/**

86

* Single types representing path-dependent types like obj.type

87

*/

88

abstract class SingleType extends SingletonType {

89

def pre: Type

90

def sym: Symbol

91

}

92

93

/**

94

* Super types representing C.super[T].type

95

*/

96

abstract class SuperType extends SingletonType {

97

def thistpe: Type

98

def supertpe: Type

99

}

100

101

/**

102

* Constant types representing literal values

103

*/

104

abstract class ConstantType extends SingletonType {

105

def value: Constant

106

}

107

108

/**

109

* Type references like List[Int], String, etc.

110

*/

111

abstract class TypeRef extends Type {

112

def pre: Type

113

def sym: Symbol

114

def args: List[Type]

115

}

116

117

/**

118

* Refined types like A with B { def foo: Int }

119

*/

120

abstract class RefinedType extends Type {

121

def parents: List[Type]

122

def decls: Scope

123

}

124

125

/**

126

* Class information types

127

*/

128

abstract class ClassInfoType extends Type {

129

def parents: List[Type]

130

def decls: Scope

131

def typeSymbol: Symbol

132

}

133

134

/**

135

* Method types representing method signatures

136

*/

137

abstract class MethodType extends Type {

138

def params: List[Symbol]

139

def resultType: Type

140

}

141

142

/**

143

* Polymorphic types with type parameters

144

*/

145

abstract class PolyType extends Type {

146

def typeParams: List[Symbol]

147

def resultType: Type

148

}

149

150

/**

151

* Existential types with existentially quantified variables

152

*/

153

abstract class ExistentialType extends Type {

154

def quantified: List[Symbol]

155

def underlying: Type

156

}

157

158

/**

159

* Annotated types carrying annotations

160

*/

161

abstract class AnnotatedType extends Type {

162

def annotations: List[Annotation]

163

def underlying: Type

164

}

165

166

/**

167

* Type bounds like >: Lo <: Hi

168

*/

169

abstract class TypeBounds extends Type {

170

def lo: Type

171

def hi: Type

172

}

173

```

174

175

### Type Factory Methods

176

177

Methods for creating and manipulating types.

178

179

```scala { .api }

180

/**

181

* Create a type reference

182

*/

183

def TypeRef(pre: Type, sym: Symbol, args: List[Type]): Type

184

185

/**

186

* Create a single type

187

*/

188

def SingleType(pre: Type, sym: Symbol): Type

189

190

/**

191

* Create a this type

192

*/

193

def ThisType(sym: Symbol): Type

194

195

/**

196

* Create a refined type

197

*/

198

def RefinedType(parents: List[Type], decls: Scope): Type

199

200

/**

201

* Create a method type

202

*/

203

def MethodType(params: List[Symbol], resultType: Type): Type

204

205

/**

206

* Create a polymorphic type

207

*/

208

def PolyType(typeParams: List[Symbol], resultType: Type): Type

209

210

/**

211

* Create type bounds

212

*/

213

def TypeBounds(lo: Type, hi: Type): Type

214

```

215

216

**Usage Examples:**

217

218

```scala

219

import scala.reflect.runtime.universe._

220

221

// Type equality and subtyping

222

val stringType = typeOf[String]

223

val anyType = typeOf[Any]

224

val intType = typeOf[Int]

225

226

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

227

println(stringType <:< anyType) // true

228

println(intType <:< anyType) // true

229

230

// Member lookup

231

val listIntType = typeOf[List[Int]]

232

val headMethod = listIntType.member(TermName("head"))

233

println(s"head method: $headMethod")

234

235

val allMembers = listIntType.members

236

allMembers.filter(_.isMethod).foreach { member =>

237

println(s"Method: ${member.name} -> ${member.info}")

238

}

239

240

// Type manipulation

241

val listType = typeOf[List[String]]

242

println(s"Original: $listType")

243

println(s"Dealias: ${listType.dealias}")

244

println(s"Widen: ${listType.widen}")

245

println(s"Erasure: ${listType.erasure}")

246

247

// Type arguments and constructor

248

println(s"Type args: ${listType.typeArgs}")

249

println(s"Type constructor: ${listType.typeConstructor}")

250

```

251

252

### Advanced Type Operations

253

254

Complex type operations for advanced use cases.

255

256

```scala { .api }

257

/**

258

* Type substitution - replace type parameters with concrete types

259

*/

260

def substituteTypes(from: List[Symbol], to: List[Type]): Type

261

262

/**

263

* Type transformation using a function

264

*/

265

def map(f: Type => Type): Type

266

267

/**

268

* Find all types matching a predicate

269

*/

270

def collect[T](pf: PartialFunction[Type, T]): List[T]

271

272

/**

273

* Check if type exists anywhere in the type tree

274

*/

275

def exists(p: Type => Boolean): Boolean

276

277

/**

278

* Transform types recursively

279

*/

280

def transform(transformer: TypeTransformer): Type

281

282

/**

283

* Get the least upper bound of two types

284

*/

285

def lub(tps: List[Type]): Type

286

287

/**

288

* Get the greatest lower bound of two types

289

*/

290

def glb(tps: List[Type]): Type

291

```

292

293

**Usage Example - Type Substitution:**

294

295

```scala

296

import scala.reflect.runtime.universe._

297

298

// Working with generic types

299

val listSymbol = typeOf[List[_]].typeSymbol

300

val typeParam = listSymbol.typeParams.head

301

302

// Create List[String] by substitution

303

val stringType = typeOf[String]

304

val listStringType = typeOf[List[_]].substituteTypes(List(typeParam), List(stringType))

305

println(s"Substituted type: $listStringType")

306

```

307

308

### Type Comparison and Analysis

309

310

```scala { .api }

311

/**

312

* Advanced type relationship checks

313

*/

314

trait TypeRelations {

315

/** Check if types are equivalent after erasure */

316

def =~=(that: Type): Boolean

317

318

/** Find common supertype */

319

def lub(that: Type): Type

320

321

/** Find common subtype */

322

def glb(that: Type): Type

323

324

/** Check type parameter variance */

325

def isContravariant: Boolean

326

def isCovariant: Boolean

327

def isInvariant: Boolean

328

}

329

```

330

331

**Complete Type Analysis Example:**

332

333

```scala

334

import scala.reflect.runtime.universe._

335

336

def analyzeType(tpe: Type): Unit = {

337

println(s"Analyzing type: $tpe")

338

println(s"Type symbol: ${tpe.typeSymbol}")

339

println(s"Type args: ${tpe.typeArgs}")

340

println(s"Is final: ${tpe.typeSymbol.isFinal}")

341

println(s"Is abstract: ${tpe.typeSymbol.isAbstract}")

342

343

// Base classes

344

println("Base classes:")

345

tpe.baseClasses.foreach { cls =>

346

println(s" - ${cls.fullName}")

347

}

348

349

// Members by category

350

val methods = tpe.members.filter(_.isMethod)

351

val fields = tpe.members.filter(m => m.isTerm && !m.isMethod)

352

val types = tpe.members.filter(_.isType)

353

354

println(s"Methods: ${methods.size}")

355

println(s"Fields: ${fields.size}")

356

println(s"Nested types: ${types.size}")

357

358

// Type relationships

359

println(s"Conforms to Any: ${tpe <:< typeOf[Any]}")

360

println(s"Conforms to AnyRef: ${tpe <:< typeOf[AnyRef]}")

361

}

362

363

// Analyze different types

364

analyzeType(typeOf[String])

365

analyzeType(typeOf[List[Int]])

366

analyzeType(typeOf[Option[String]])

367

```