Scala Upper Bounds:值不是类型参数的成员 [英] Scala Upper Bounds : value is not a member of type parameter

查看:161
本文介绍了Scala Upper Bounds:值不是类型参数的成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么价格在SeqValue上找不到属性值?这看起来很简单,应该工作。



我得到错误

  [error] .... value value不是类型参数的成员SeqValue 
[error] def recalc [SeqValue](input:SeqValue)= Price(1 + seq,input.value)

以下代码

 密封特质SeqValue {
def seq:Int
def value:Float
override def toString = ToStringBuilder.reflectionToString(this,ToStringStyle.SHORT_PREFIX_STYLE)
}

密封特质Calc {
类型S<:SeqValue
def recalc [S](输入:S):SeqValue
}

案例类Price(seq:Int = 0,value:Float = .0f)使用Calc {

这个想法是,您可以重新计算价格对象,并传入任何类型的实现的对象SeqValue,因为SeqValue有一个值。

解决方案 c Calc 中的类型成员 S 正在被类型参数<$ c遮蔽$ c> S recalc 方法。第二个错误:抽象类型 S 将不得不在类 Price中定义。 / code>。



以下内容应该适用:

 密封特质SeqValue {
def seq:Int
def value:Float
override def toString = ToStringBuilder.reflectionToString(this,ToStringStyle.SHORT_PREFIX_STYLE)
}

密封特质Calc {
type S< ;: SeqValue
def recalc(输入:S):SeqValue
}

案例类价格(seq:Int = 0,value:Float = .0f)用Calc扩展SeqValue {
type S = SeqValue
def recalc(input:SeqValue)= Price(1 + seq,input.value)
}

编辑:(回应评论)



我不明白你准备做什么,但你可以在单独的mixin特性中分离出类型定义。

  trait SAsSeqValue {
type S = SeqValue
}

案例类价格(se q:Int = 0,value:Float = .0f)使用带SAsSeqValue的Calc扩展SeqValue {
def recalc(input:SeqValue)= Price(1 + seq,input.value)
}


Why can the Price not find the attribute value on the SeqValue? It seems so simple that is should work.

Im getting the error

[error]   .... value value is not a member of type parameter SeqValue
[error]   def recalc[SeqValue](input:SeqValue) = Price(1 + seq, input.value)    

for the following code

sealed trait SeqValue {
  def seq:Int
  def value:Float
  override def toString = ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE)
}

sealed trait Calc {
  type S <: SeqValue
  def recalc[S](input:S):SeqValue
}

case class Price(seq:Int=0, value:Float=.0f) extends SeqValue with Calc {
  def recalc[SeqValue](input:SeqValue) = Price(1 + seq, input.value)
}

The idea is that you can recalc on the price object, and pass in any type of object that implements SeqValue, because SeqValue has a value.

解决方案

The type member S in Calc is getting shadowed by type parameter S of recalc method.

Second mistake: The abstract type S will have to be defined in class Price.

The following should work:

sealed trait SeqValue {
  def seq:Int
  def value:Float
  override def toString = ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE)
}

sealed trait Calc {
  type S <: SeqValue
  def recalc(input:S):SeqValue
}

case class Price(seq:Int=0, value:Float=.0f) extends SeqValue with Calc {
  type S = SeqValue
  def recalc(input:SeqValue) = Price(1 + seq, input.value)
}

Edit: (in response to the comment)

I don't understand what you're exactly trying to do, but you could separate out the type definition in a separate mixin trait.

trait SAsSeqValue {
  type S = SeqValue
}

case class Price(seq:Int=0, value:Float=.0f) extends SeqValue with Calc with SAsSeqValue {      
  def recalc(input:SeqValue) = Price(1 + seq, input.value)
}

这篇关于Scala Upper Bounds:值不是类型参数的成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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