正向引用延伸到价值问题的界定, [英] Forward reference extends over definition of value problem

查看:262
本文介绍了正向引用延伸到价值问题的界定,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在斯卡拉一些问题解决隐含的价值观,和我有NetBeans中神秘的错误消息:


  

错误:正向引用延伸的价值定义...


或阶控制台我有一个其他错误消息


  

型mistmatch:29:错误:类型不匹配;实测值:
  Factory.type(与基础类型的对象厂)要求:
  GenericFactory


我的阶级和主要功能的一些描述:

 导入了java.util.Random//////////
// //主//隐随机的所有类和对象
隐VAL aprng =新的随机在其他类中使用//隐式对象厂
隐VAL厂=厂抽象类GenericFactory {
打造高清
}对象工厂扩展GenericFactory {
高清打造=的println(返回工厂)
}A级(速度:随机=>双= aprng => aprng.nextFloat,VAL工厂:GenericFactory){  DEF这(速度:双,工厂:GenericFactory)=这(_ =>月率,工厂)  DEF操作(基因组:IndexedSeq [INT])(隐含aprng:随机)= {
的println(计算与aprng随机值基因组)}
}对象B {
VAL instanceOfA =新的A(率=> 0.5D,厂)
}

我有2个问题,因为1)我可以在A类的参数传递一个对象,所以我定义一个抽象类,通过这个抽象类,但不能在这里工作:/
2)在此之后,我定义为A类隐含的价值不被认可,并失败,出现错误信息。

你有一个想法或一个答案来解决这个问题呢?

编辑1

我更新code与agisteel的帮助下,它的确定,code运行:)

 导入了java.util.Random//////////
// //主//隐随机的所有类和对象
隐VAL aprng =新的随机
隐Val函数:随机=>双= arpng => arpng.nextFloat抽象类GenericFactory
{
  高清打造=的println(建)
}隐式对象厂延伸GenericFactory
{
  高清打造=的println(返回工厂)
}A类(隐率:随机=>双,隐VAL工厂:GenericFactory){  //这个DEF(速度:双,工厂:GenericFactory)=这(_ =>月率,工厂)  DEF操作(基因组:IndexedSeq [INT])(隐含aprng:随机)= {
    的println(计算与aprng随机值基因组)}
}对象B {
VAL instanceOfA =新的A
}


解决方案

我不知道你正试图在这里实现的目标,但这里有一些提示:

1),而不是:

 隐VAL厂=厂
对象工厂扩展GenericFactory {...

试试这个:

 隐式对象延伸厂{GenericFactory ...

2)lambda表达式的默认值只能通过implicits尽量提供我所知,所以不是:

  A级(速度:随机=>双= aprng => aprng.nextFloat,...

试试这个:

 隐Val函数:随机=>双= arpng => arpng.nextFloat
A类(隐率:随机=>双人间)(...

我希望这个信息是非常有用的。

i have some problem in scala to resolve implicit values, and i have the cryptic error message in netbeans :

"error : Forward reference extends over definition of value ..."

or in the scala console i have an other error message

"type mistmatch :29: error: type mismatch; found : Factory.type (with underlying type object Factory) required: GenericFactory"

Some description of my class and main function :

import java.util.Random

//////////
// Main //

//Implicit random for all classes and object
implicit val aprng = new Random

//Implicit object Factory used in other class
implicit val factory = Factory

abstract class GenericFactory {
def build 
}

object Factory extends GenericFactory{
def build = println("return factory")
}

class A (rate: Random => Double = aprng => aprng.nextFloat, val factory : GenericFactory) {

  def this(rate : Double, factory : GenericFactory) = this( _ => rate, factory)

  def operate(genomes: IndexedSeq[Int])(implicit aprng: Random) = { 
println("calculate genomes with aprng random values")}
}

object B{
val instanceOfA = new A(rate => 0.5d,factory)
}

I have 2 problem because 1) i can pass an object in argument of class A, so i define an abstract class to pass this abstract class, but that not work here :/ 2) After that, my implicit value defined into class A is not recognized, and fail with error message.

Do you have an idea or an answer to resolve this problem ?

Edit 1

I update the code with help of agisteel, it's ok, code running :)

import java.util.Random

//////////
// Main //

//Implicit random for all classes and object
implicit val aprng = new Random
implicit val function: Random => Double = arpng => arpng.nextFloat

abstract class GenericFactory 
{
  def build = println("build")
}

implicit object Factory extends GenericFactory
{
  def build = println("return factory")
}

class A (implicit rate: Random => Double, implicit val factory : GenericFactory) {

  //def this(rate : Double, factory : GenericFactory) = this( _ => rate, factory)

  def operate(genomes: IndexedSeq[Int])(implicit aprng: Random) = { 
    println("calculate genomes with aprng random values")}
}

object B{
val instanceOfA = new A
}

解决方案

I'm not sure what you are trying to achieve here, but here are few tips:

1) instead of:

implicit val factory = Factory
object Factory extends GenericFactory { ...

try this:

implicit object Factory extends GenericFactory { ...

2) Default values for lambdas can only be provided via implicits as far as I know, so instead of:

class A (rate: Random => Double = aprng => aprng.nextFloat, ...

try this:

implicit val function: Random => Double = arpng => arpng.nextFloat
class A (implicit rate: Random => Double) ( ...

I hope this info is useful.

这篇关于正向引用延伸到价值问题的界定,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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