Scala:如何扩展不可变List

Rol*_*our 0 scala implicit sealed

程序的许多方法作为参数接收List [Map [String,String]].

我想通过定义一个类来形式化它并使其更具可读性,例如:

class MyClass extends  List[Map[String, String]]
Run Code Online (Sandbox Code Playgroud)

但是它会抛出一个错误:

Illegal inheritance from sealed class 'List'
Run Code Online (Sandbox Code Playgroud)

有没有正确的方法来处理它?

sen*_*vic 5

你需要的东西叫做类型别名:

type MyClass = List[Map[String, String]]
Run Code Online (Sandbox Code Playgroud)

https://alvinalexander.com/scala/scala-type-aliases-syntax-examples

您得到错误是因为您正在尝试扩展密封特征,该特征只能在定义特征的同一文件中扩展.

https://alvinalexander.com/scala/scala-type-aliases-syntax-examples https://underscore.io/blog/posts/2015/06/02/everything-about-sealed.html