简体   繁体   English

ScalaZ:什么是“ type Tagged [T] = {type Tag = T}”?

[英]ScalaZ: what is “type Tagged[T] = {type Tag = T}”?

I started to read scalaz's source code . 我开始阅读scalaz的源代码

package object scalaz {
  import Id._

  implicit val idInstance: Traverse1[Id] with Each[Id] with Monad[Id] with Comonad[Id] with Distributive[Id] with Zip[Id] with Unzip[Id] with Align[Id] with Cozip[Id] = Id.id

  type Tagged[T] = {type Tag = T}
  .
  .
  .

How should I interpret type Tagged[T] = {type Tag = T} ? 我应该如何解释type Tagged[T] = {type Tag = T}

What does this mean ? 这是什么意思 ?

Where is this Scala syntax described ? Scala语法在哪里描述?

I am totally confused with this. 我对此完全感到困惑。

What does it do ? 它有什么作用 ?

Why ? 为什么呢

Could someone give a super simple example that would explain this syntax for "dummies" ? 有人可以举一个超级简单的例子来解释“假人”的这种语法吗?

... and what it is good for ? ...这有什么用?

It's scala structural typing, basically it means that type that satisfying Tagged[T] should have declared 'type Tag' inside 这是scala结构化类型,基本上意味着满足Tagged [T]的类型应在内部声明“ type Tag”

  type Tagged[T] = {type Tag = T}

  def acceptOnlyTagged[T: ClassTag](tagged: Tagged[T]): Unit = {
    println(implicitly[ClassTag[T]])
  }

  class A {
    type Tag = Int
  }

  class B {
    type Tag = String
  }

  acceptOnlyTagged(new A)
  acceptOnlyTagged(new B)

Class A & B doesn't know anything about Tagged but can be viewed as Tagged because they share common structure A和B类对Tagged一无所知,但可以将其视为Tagged,因为它们具有相同的结构

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

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