Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala 是否可以将trait的上下文更改为声明为其类型参数的类型?_Scala_Traits - Fatal编程技术网

Scala 是否可以将trait的上下文更改为声明为其类型参数的类型?

Scala 是否可以将trait的上下文更改为声明为其类型参数的类型?,scala,traits,Scala,Traits,我想完成的事情与此类似: class Foo( val bar: String = "Hello!" ) extends MyTrait[ Foo ] trait MyTrait[ T ] { self : T => T.bar } 将self:T更改为self:Foo显然是可行的,但MyTrait可能会扩展另一个同样具有bar的类,因此self:Foo是不可接受的 我可能弄错了,有什么想法吗?它已经起作用了 scala> trait Foo[A] { self: A =>

我想完成的事情与此类似:

class Foo( val bar: String = "Hello!" ) extends MyTrait[ Foo ]

trait MyTrait[ T ] { self : T =>
 T.bar
}
将self:T更改为self:Foo显然是可行的,但MyTrait可能会扩展另一个同样具有bar的类,因此self:Foo是不可接受的

我可能弄错了,有什么想法吗?

它已经起作用了

scala> trait Foo[A] { self: A =>
     | }
defined trait Foo

scala> class Meh extends Foo[Meh]
defined class Meh

scala> class Duh extends Foo[Meh]
<console>:36: error: illegal inheritance;
 self-type Duh does not conform to Foo[Meh]'s selftype Foo[Meh] with Meh
       class Duh extends Foo[Meh]
                         ^
编辑:

对不起,我误解了这个问题@4e6是对的。你需要一个结构类型。他的解决方案略有变化:

scala> trait Foo[A <: { def bar: String }] { self: A =>
     | }
defined trait Foo

scala> class Bar extends Foo[Bar] {
     |   def bar = ""
     | }
defined class Bar

scala> class Baz extends Foo[Baz]
<console>:35: error: type arguments [Baz] do not conform to trait Foo's type parameter bounds [A <: AnyRef{def bar: Stri
ng}]
       class Baz extends Foo[Baz]
                         ^
它已经起作用了

scala> trait Foo[A] { self: A =>
     | }
defined trait Foo

scala> class Meh extends Foo[Meh]
defined class Meh

scala> class Duh extends Foo[Meh]
<console>:36: error: illegal inheritance;
 self-type Duh does not conform to Foo[Meh]'s selftype Foo[Meh] with Meh
       class Duh extends Foo[Meh]
                         ^
编辑:

对不起,我误解了这个问题@4e6是对的。你需要一个结构类型。他的解决方案略有变化:

scala> trait Foo[A <: { def bar: String }] { self: A =>
     | }
defined trait Foo

scala> class Bar extends Foo[Bar] {
     |   def bar = ""
     | }
defined class Bar

scala> class Baz extends Foo[Baz]
<console>:35: error: type arguments [Baz] do not conform to trait Foo's type parameter bounds [A <: AnyRef{def bar: Stri
ng}]
       class Baz extends Foo[Baz]
                         ^

我认为,结构类型是您需要的:

trait MyTrait {
  self: { val bar: String } =>
 def showBar = bar
}

class Foo(val bar: String) extends MyTrait

我认为,结构类型是您需要的:

trait MyTrait {
  self: { val bar: String } =>
 def showBar = bar
}

class Foo(val bar: String) extends MyTrait

嗨,谢谢你的回复!有可能将Foo中的字段暴露给Foo中的MyTrait吗?我不太理解你的想法。我猜,你最初的多态性解决方案就是这样。我的第二个猜测是,如果您想要更复杂的scala方式依赖项注入,请查看库。您好,谢谢您的回复!有可能将Foo中的字段暴露给Foo中的MyTrait吗?我不太理解你的想法。我猜,你最初的多态性解决方案就是这样。我的第二个猜测是,如果您想要更复杂的scala方式依赖项注入,请查看库。