简体   繁体   English

`this`关键字的Scala类型编程类比是什么?

[英]What is the Scala type-programming analogy for the `this` keyword?

I'm trying to work my way into understanding type-programming in Scala, and I've found that most of what one needs to know about type programming has an analogous counterpart in value programming as is reflected in the type-level programming wiki page . 我正在努力学习Scala中的类型编程,并且我发现大多数关于类型编程需要知道的东西在值编程中都有类似的对应关系,这反映在类型级编程维基页面中 However, I've not found the analogy for the this key word or self-types. 但是,我没有找到this关键词或自我类型的类比。 I suspect maybe it doesn't make sense to expect such a thing, but I thought I would ask. 我怀疑期待这样的事情可能没有意义,但我想我会问。

For instance, I can write the following to represent Booleans as values at run time: 例如,我可以编写以下代码来表示Booleans在运行时的值:

sealed trait BoolVal {
  def not:BoolVal
  def or(that:BoolVal):BoolVal
  def and(that:BoolVal) =
    (this.not or that.not).not
  def imp(that:BoolVal) =
    this.not or that
}
case object TrueVal extends BoolVal {
  override val not = FalseVal
  override def or(that:BoolVal) = TrueVal
}
case object FalseVal extends BoolVal {
  override val not = TrueVal
  override def or(that:BoolVal) = that
}

Here my and and imp are able to take advantage of the fact it doesn't matter if I am a false object or a true object to be defined correctly. 在这里,我andimp能够利用这个事实,如果我是一个假对象或一个正确定义的真实对象并不重要。 My TrueVal and FalseVal objects can inherit the same code. 我的TrueValFalseVal对象可以继承相同的代码。

I can make the analogous type-level programming constructs, but I don't understand how to define And and Imp in my base trait. 我可以制作类似的类型级编程结构,但我不明白如何在我的基本特征中定义AndImp

sealed trait BoolType {
  type Not <: BoolType
  type Or[That <: BoolType] <: BoolType
  type And[That <: BoolType] = ???
  type Imp[That <: BoolType] = ???
}
sealed trait TrueType extends BoolType {
  override type Not = FalseType
  override type Or[That <: BoolType] = TrueType
}
sealed trait FalseType extends BoolType {
  override type Not = TrueType
  override type Or[That <: BoolType] = That
}

I can see where perhaps it doesn't make sense that my types inherit types, but the certainly inherit abstract types. 我可以看到我的类型继承类型可能没有意义,但肯定会继承抽象类型。 Is there a way to define And and Impl in my BoolType , or do I have to define each in the respective TrueType and FalseType traits? 有没有办法在我的BoolType定义AndImpl ,或者我必须在各自的TrueTypeFalseType特征中定义每个?

You can always define an abstract type on your boolean base type as follows: 您始终可以在布尔基类型上定义抽象类型,如下所示:

trait MyBool extends BoolType{
  type This <: BoolType
}

trait TrueType extends BoolType{
  type This = TrueType
}

and you should be good to go with a reference to yourself. 你应该善于引用自己。 Then you can use DeMorgan's Laws to do the following 然后您可以使用DeMorgan的法律来执行以下操作

 !(x && y) == (!x || !y)

Then by a double negative you can get you And condition going: 然后通过双重否定你可以得到你And条件:

 !(!x || !y) == !!(x && y) == (x && y)

I would suggest using self , example of your blog post adjusted: 我建议使用self ,调整你的博客文章的例子:

sealed trait BoolType { self =>
  type Not <: BoolType
  type Or[That <: BoolType] <: BoolType
  type And[That <: BoolType] = self.type#Not#Or[That#Not]#Not
  type Imp[That <: BoolType] = self.type#Not#Or[That]
}
sealed trait TrueType extends BoolType {
  override type Not = FalseType
  override type Or[That <: BoolType] = TrueType
}
sealed trait FalseType extends BoolType {
  override type Not = TrueType
  override type Or[That <: BoolType] = That
}

Why not just use the this keyword? 为什么不使用this关键字? When i explored type level programming myself, i could not see a difference when using this instead of self. 当我自己探索类型级编程时,在使用它而不是self时我看不出有什么区别。

sealed trait BoolType {
  type Not <: BoolType 
  type Or[That <: BoolType] <: BoolType
  type And[That <: BoolType] = this.type#Not#Or[That#Not]#Not
  type Imp[That <: BoolType] = this.type#Not#Or[That]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM