显示类线性化的简便方法? [英] handy ways to show linearization of a class?

查看:50
本文介绍了显示类线性化的简便方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我敢打赌,有一些时髦的方法可以通过新的反射库和/或 repl 电源模式在 repl 中显示线性化.这些是什么?

I'll bet there are some snazzy ways to show linearization in the repl via the new reflection libraries and/or repl power mode. What are they?

在我的特殊情况下,我试图理解 ArrayBuffer 实例的线性化.
(我正在尝试创建一个类似的类,它是一个 Buffer 和一个 IndexedSeqOptimized,我发现编译器抱怨覆盖方法 seq 的类型不兼容)

In my particular case, I'm trying to understand the linearization of ArrayBuffer instances.
(I'm trying to make a similar class that's a Buffer and an IndexedSeqOptimized, and I'm finding complaints from the compiler that overriding method seq has incompatible type)

scala 类的线性化规则在 scala 规范中描述 第 5.1.2 节.据我了解,方法覆盖按线性化顺序进行,初始化按反向线性化顺序进行.

The rules for linearization of scala classes are described in the scala spec section 5.1.2. As I understand it, method overrides go in linearization order, and initialization goes in reverse linearization order.

推荐答案

Scala 反射中的 TypeClassSymbol 上有一个方法 baseClassesAPI,根据其 scaladoc,返回:

There is a method baseClasses on Type and ClassSymbol in Scala reflection API which, according to its scaladoc, returns:

该类型的所有基类(包括它自己的 typeSymbol)的列表,以反向线性化顺序,从类本身开始,以类 Any 结束.

The list of all base classes of this type (including its own typeSymbol) in reverse linearization order, starting with the class itself and ending in class Any.

这个怎么用?这是一个以反向线性化顺序打印 scala.collection.immutable.List 的所有超类的示例:

How to use this? Here's an example that prints all superclasses of scala.collection.immutable.List in reverse linearization order:

import scala.reflect.runtime.universe._
val tpe = typeOf[scala.collection.immutable.List[_]]
tpe.baseClasses foreach { s => println(s.fullName) }

输出:

scala.collection.immutable.List
scala.collection.LinearSeqOptimized
scala.Product
scala.collection.immutable.LinearSeq
scala.collection.LinearSeq
scala.collection.LinearSeqLike
scala.collection.immutable.Seq
scala.collection.immutable.Iterable
scala.collection.immutable.Traversable
scala.Immutable
scala.collection.AbstractSeq
scala.collection.Seq
scala.collection.SeqLike
scala.collection.GenSeq
scala.collection.GenSeqLike
scala.PartialFunction
scala.Function1
scala.collection.AbstractIterable
scala.collection.Iterable
scala.collection.IterableLike
scala.Equals
scala.collection.GenIterable
scala.collection.GenIterableLike
scala.collection.AbstractTraversable
scala.collection.Traversable
scala.collection.GenTraversable
scala.collection.generic.GenericTraversableTemplate
scala.collection.TraversableLike
scala.collection.GenTraversableLike
scala.collection.Parallelizable
scala.collection.TraversableOnce
scala.collection.GenTraversableOnce
scala.collection.generic.FilterMonadic
scala.collection.generic.HasNewBuilder
java.lang.Object
scala.Any

这篇关于显示类线性化的简便方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆