Erdi İzgi Erdi İzgi - 27 days ago 18
Scala Question

_ must follow method; cannot follow () => Boolean

Hi I'm trying to run my code, I double checked everything. It supposed to work, I mean IDE doesn't give any warnings. But when it is compiling I am getting this error

Error:(20, 40) _ must follow method; cannot follow () => Boolean
properties += new Property(propName, formula _)


here is code part which causes this error

def property(propName: String)(formula: () => Boolean) {
properties += new Property(propName, formula _)
}


this is the Property class

class Property(val name: String, val func: () => Boolean)


what's the thing that I am missing here?

Answer

You don't need the underscore here. The underscore would only be needed when you want to convert a method to a function via eta-expansion. This isn't necessary here because formula is already a function of the type () => Boolean, which is what the Property class is looking for.

def property(propName: String)(formula: () =>  Boolean)  {
    properties += new Property(propName, formula)
}
Comments