Scala:“静态值"特质? [英] Scala: "Static values" in traits?

查看:99
本文介绍了Scala:“静态值"特质?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我说:

trait X {
  val x: String
}

使用混合,我可以定义一个特征,例如

Using mix-in, I can define a trait such as

trait XPrinter {
  self: X =>
  def printX: String = "X is: " + x
}

使得实现XPrinter的值/对象实现x并为其诸如printX的方法提供对X中指定的值(例如x)的访问.

such that a value/object implementing XPrinter implements x and give its methods such as printX access to the values specified in X such as x.

到目前为止,太好了.

So far, so good.

我想知道是否存在一种具有以下特征的特征:

I want to know if there is a way of having a trait in the form of:

trait XDependent[T <: X] {
  def printX: String = ???
}

,以便XDependent实例可以访问T.x的值,其中x假定为粘贴有类型定义的静态值".

So that XDependent instances have access to the value of T.x, with x assumed to be a "static value" glued with the type definition.

现在我明白了为什么在XDependent中不能访问T.x的原因,因为类型子类型X甚至不必实现x的值,并且T.x可能是抽象的.

Now I understand why T.x can't be accessed in XDependent since a type subtyping X doesn't even have to implement the value of x and T.x might be abstract.

我了解到,尽管Scala提供了与路径有关的类型,所以可以在XDependent中使用X中定义的抽象类型,如下所示:

I understand that while Scala offers path-dependent types so that an abstract type defined in X can be used in XDependent, as shown here:

trait X {
  type Y //which can be constrained as desired.
}

trait XDependent[T <: X]{
  def foo(v:T#Y)
  def bar: T#Y
}

它没有提供相同的值,因为Scala中类型和值之间有明显的分隔.

it doesn't offer the same thing with values as there is a clear separation between types and values in Scala.

现在,我遇到了依赖于价值的类型基于文字的类型的想法.我想知道如上所述的类型的静态值"的想法是否与这些概念以及什么是连接有很多重叠.

Now I have come across the ideas of value-dependent types and literal-based types. I want to know if the idea of "static value for types", as illustrated above, has much overlap with the these concepts and what the connections are.

我还想了解不同语言所采用的不同方法,以模糊类型和值之间的分隔,它们与Scala的类型系统的兼容性如何,以及在集成静态值"方面的复杂性是什么与类型系统.即,(是否可以)/(如果发生了什么)被子类型覆盖,等等.

I'd also like to know about the different approaches taken in different languages, to blur the separation between types and values, how compatible they are with Scala's type system, and what the complications are in terms of integrating "static values" with the type-system. ie, (Can they be)/ (what happens if they are) overriden by a subtype, etc.

推荐答案

如果您可以放宽对XDependent必须为trait的要求,而改为使其为abstract class的要求,则好像仅提供一个空二进制方法x的typeclass正是您想要的:

If you can relax the requirement that XDependent has to be a trait, and make it an abstract class instead, then it seems as if a typeclass which provides a single null-ary method x is exactly what you want:

这是您的基本特征X(不包含X.x或其他任何不是静态"的内容):

Here is your base trait X (without X.x or anything, that wouldn't be "static"):

trait X

现在您可以定义一个类型类HasStaticX[T],该类型类保证对于类型T,我们可以提供一些字符串x:

Now you can define a typeclass HasStaticX[T] that guarantees that for a type T we can give some string x:

trait HasStaticX[T] {
  def x: String
}

然后您可以像这样使用它:

Then you can use it like this:

abstract class XDependent[T <: X : HasStaticX] {
  def printX: String = implicitly[HasStaticX[T]].x
}

HasStaticX的作用本质上是构建一个编译时部分函数,​​该函数可以采用类型T并生成与T相关联的字符串值x.因此,从某种意义上讲,它就像一个接受类型并返回值的函数.如果这是您想要的,那么集成静态值"无需执行任何操作,它仅适用于当前的非实验性Scala主流版本.

What HasStaticX does is essentially building a compile-time partial function that can take type T and produce a string-value x associated with T. So, in a way, it's something like a function that takes types and returns values. If this is what you want, then nothing has to be done to for "integrating static values", it just works in the current non-experimental mainstream versions of Scala.

与值相关的类型"恰恰相反:实际上就是那些将类型分配给值的函数".

The "value-dependent types" would be exactly the other way round: those would be essentially "functions" that assign types to values.

这篇关于Scala:“静态值"特质?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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