Skip to content

Instantly share code, notes, and snippets.

@misto
Created February 7, 2012 09:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save misto/1758655 to your computer and use it in GitHub Desktop.
Save misto/1758655 to your computer and use it in GitHub Desktop.
Scala Trait linearization
Welcome to Scala version 2.9.1.r0-b20120125223512 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_02).
Type in expressions to have them evaluated.
Type :help for more information.
scala> trait A
defined trait A
scala> trait C extends A
defined trait C
scala> trait B extends A
defined trait B
scala> trait D extends B
defined trait D
scala> trait E extends C
defined trait E
scala> class D extends A with D with E
<console>:12: error: class D inherits itself
class D extends A with D with E
^
scala> class F extends A with D with E
defined class F
scala> :power
** Power User mode enabled - BEEP BOOP SPIZ **
** :phase has been set to 'typer'. **
** scala.tools.nsc._ has been imported **
** global._ and definitions._ also imported **
** Try :help, vals.<tab>, power.<tab> **
scala> intp.types("F").tpe.baseTypeSeq.toList
res0: List[$r.intp.global.Type] = List(F, D, E, C, B, ScalaObject, A, java.lang.Object, Any)
Tada!
@shaun-here
Copy link

Getting following error:

scala> intp.types("F").tpe.baseTypeSeq.toList
:46: error: value types is not a member of scala.tools.nsc.interpreter.IMain
intp.types("F").tpe.baseTypeSeq.toList

@vpetro
Copy link

vpetro commented Jan 18, 2018

This works:

Welcome to Scala 2.12.4 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_144).
Type in expressions for evaluation. Or try :help.

scala> :paste
// Entering paste mode (ctrl-D to finish)

trait A { def foo = "a" }
trait B extends A {  override def foo = "b" + super.foo }
trait D extends A {  override def foo = "d" + super.foo }
trait C extends B {  override def foo = "c" + super.foo }
class Qux extends A with C with B with D

// Exiting paste mode, now interpreting.

defined trait A
defined trait B
defined trait D
defined trait C
defined class Qux

scala> :power
Power mode enabled. :phase is at typer.
import scala.tools.nsc._, intp.global._, definitions._
Try :help or completions for vals._ and power._

scala> intp.exprTyper.typeOfTypeString("Qux").baseClasses
res0: List[$r.intp.exprTyper.repl.global.Symbol] = List(class Qux, trait D, trait C, trait B, trait A, class Object, class Any)

Documentation for the method is here: https://github.com/scala/scala/blob/v2.12.4/src/reflect/scala/reflect/internal/Types.scala#L909

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment