简体   繁体   中英

Why does this piece of Scala code not compile?

The error it yields is:

Funct.scala:5: 'val' expected but identifier found.
[error] class Funct[In,Out](function: In => Out, description: String, implicit m: Manifest[In => Out]) {

and the code in question is:

import scala.reflect.Manifest;

class Funct[In,Out](function: In => Out, description: String, implicit m: Manifest[In => Out]) {

  def isType[K](implicit man: Manifest[K]) = {
    m <:< man
  }

  def Apply(input: In): Out = {
    function.Apply(input)
  }

  def toString() = {
    description + m
  }
}

I simply do not see what the problem is.

There are a few problems you should be able to figure out, but the message is indeed a bit confusing.

The issue here is that the implicit keyword must mark the whole parameter group and not just individual parameters. Try:

class Funct[In,Out](function: In => Out, description: String)(implicit m: Manifest[In => Out])

function.Apply(input)应该是function.apply(input)或仅仅是function(input) ,但认真地,只需使用IntelliJ或Eclipse,它们会立即告诉您这些事情。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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