I'm trying to design a couple of classes that inherit a partial function, but I don't seem to be able to get the syntax quite right. My superclass looks like this:
abstract class Controller {
val react:PartialFunction[Event,Unit]
}
And the subclass looks like:
class BoardRendererController(val renderer:BoardRenderer, val board:Board) extends Controller {
override val react {
case PieceMovedEvent(piece, origin, destination) => println("Moving now")
}
}
But this fails to compile with this error
[ERROR] /workspace/pacman/src/main/scala/net/ceilingfish/pacman/BoardRendererController.scala:14: error: '=' expected but '{' found.
[INFO] override val react {
[INFO] ^
[ERROR] /workspace/pacman/src/main/scala/net/ceilingfish/pacman/BoardRendererController.scala:17: error: illegal start of simple expression
[INFO] }
[INFO] ^
I've tried loads of variations on this, anyone know what the correct syntax is?
In addition to abhin4v's terse suggestion, you still have to supply a type annotation in the definition, so I recommend this addition to your base class:
type PFEU = PartialFunction[Event, Unit]
Then your subclass would look like this:
class BoardRendererController(val renderer:BoardRenderer, val board:Board)
extends Controller
{
override val react: PFEU = {
case PieceMovedEvent(piece, origin, destination) => println("Moving now")
}
}
Related
I am building a class composed of multiple traits, one of which provides the capability to include a list of "members" of the class:
trait WithNestedMembers[T] {
val members = new scala.collection.mutable.ListBuffer[T]
}
class MainClass extends WithNestedMembers[MainClass] {
// ...
}
This seems to work fine.
Now, I would like to use the "nesting" capability in other traits:
trait NestingUser {
this: WithNestedMembers[NestingUser] =>
var nestedValue = 0
def sumNested = nestedValue + members.map(_.nestedValue).sum
}
The definition seems to work fine by itself. However, I cannot use this as part of MainClass. The following gives an error:
class MainClass extends WithNestedMembers[MainClass] with NestingUser {
// Illegal inheritance, self-type MainClass does not conform to WithNestedMembers[NestingUser]
}
I suspect that I need to use some sort of type bounds somewhere, to explain that WithNestedMembers[MainClass] is good enough for requirement WithNestedMembers[NestingUser]. But where?
Trying making T covariant with +T like so
trait WithNestedMembers[+T] {
val members = List.empty[T]
}
I have reduced this example from something encountered in a much larger project; the fundamental issue seems to be that Scala 2.10 has weird or possibly broken handling of view constraints:
trait Nameish {
def name: String
}
abstract class Namer[A <% Nameish](e: Class[A]) {
def doNameThing(x: A) = println("argument is named " + x.name)
}
class SubNamer[A <% Nameish](e: Class[A]) extends Namer(e) {
}
object TestViews extends App {
import scala.language.implicitConversions
implicit def nameStr(x: String): Nameish = new StringNamer(x);
class StringNamer(x: String) extends Nameish {
def name = x
}
println(new SubNamer(classOf[String]).doNameThing("foo"))
}
When I try to compile this, in scala 2.10 I get the errors:
TestViews.scala:8: error: No implicit view available from A => Nameish.
class SubNamer[A <% Nameish](e: Class[A]) extends Namer(e) {
^
TestViews.scala:18: error: value doNameThing is not a member of SubNamer[String]
println(new SubNamer(classOf[String]).doNameThing("foo"))
^
two errors found
Note that Scala 2.11 is fine with this code.
Unfortunately, re-tooling this code to a newer scala version would make this task explode in size. I need to find a way to make the existing scala accept a class hierarchy with a type parameter that has a view constraint.
Another attempt at a way around this has uncovered a different scala-2.10-only error case:
trait Nameish {
def name: String
}
abstract class Namer[A](e: Class[A])(implicit view: A => Nameish) {
def doNameThing(x: A) = println("argument is named " + x.name)
}
class SubNamer[A](e: Class[A])(implicit view: A => Nameish) extends Namer(e)(view) {
}
object TestViews extends App {
import scala.language.implicitConversions
implicit def nameStr(x: String): Nameish = new StringNamer(x);
class StringNamer(x: String) extends Nameish {
def name = x
}
println(new SubNamer(classOf[String]).doNameThing("foo"))
}
This is just replacing the view constraints with implicit arguments.
With this code, it again compiles just fine with Scala 2.11, but on Scala 2.10:
TestViews.scala:8: error: `implicit' modifier cannot be used for top-level objects
class SubNamer[A](e: Class[A])(implicit view: A => Nameish) extends Namer(e)(view) {
^
one error found
I don't understand what's going on here: I'm not trying to declare an implicit object, I'm trying to declare that the class takes an implicit parameter. Why is it fine for the first class, but not the second one?
Adding parameter type parameter A to Namer (in the Subnamer inheritance) worked for me (Scala version 2.10.7):
class SubNamer[A <% Nameish](e: Class[A]) extends Namer[A](e)
By the way your example without modification worked for me only from the Scala version 2.11.5.
Hope this helps.
I am attempting to build a wrapper around scalatest's it keyword. However, this solution does not seem to work as intended. Moreover, it does not even compile:
trait MyFunSpec extends FunSpec {
private val _it: FunSpec.this.ItWord = it
protected def it(specText: String, testTags: org.scalatest.Tag*)
// ...do something extra here...
(testFun: => Any /* Assertion */)(implicit pos: Position): Unit = {
_it(specText, testTags: _*)(testFun)(pos)
// ...do something extra here...
}
}
The error message I am getting after compiling this code is as follows:
[error] MyFunSpec.scala: ambiguous reference to overloaded definition,
[error] both method it in trait MyFunSpec of type (specText: String, testTags:
org.scalatest.Tag*)(testFun: => Any)(implicit pos:
org.scalactic.source.Position)Unit
[error] and value it in trait FunSpecLike of type => FunSpecSpec.this.ItWord
[error] match argument types (String)
Please note the main idea is that method's name remains it, so renaming it to something like alternativeIt is not a satisfactory solution here.
Any suggestions, what am I doing wrong here? Any solution would be highly appreciated! Thanks!
Try this:
trait MyFunSpec extends FunSpec {
override protected val it = new ItWord {
override def apply(specText: String,testTags: org.scalatest.Tag*)(testFun: => Any)(implicit pos: org.scalactic.source.Position): Unit = {
println("Before")
super.apply(specText, testTags:_*)(testFun)(pos)
println("After")
}
}
}
Scala version 2.11.8
I have parent class
abstract class FR(externalId:String, code:String, message:String) extends Serializable {
val this.externalId=externalId;
val this.code = code;
val this.message = message;
def toString:String={
return "FRworks";
}
}
The child class is:
class RD extends FR {
def this(lpTransaction:LPTransaction)={
externalId =lpTransaction.getField("somethinghere").toString
...
}
}
The error is:
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=256m; support was removed in 8.0
[info] Loading project definition from F:\workspace\frankcheckAPI\project
[info] Set current project to frankcheckapi (in build file:/F:/workspace/frankcheckAPI/)
[info] Compiling 20 Scala sources to F:\workspace\frankcheckAPI\target\scala-2.11\classes...
[error] F:\workspace\frankcheckAPI\src\main\scala\com\cardaccess\fraudcheck\RD.scala:9: 'this' expected but identifier found.
[error] externalId =lpTransaction.getField("somethinghere").toString
[error] ^
[error] one error found
[error] (compile:compileIncremental) Compilation failed
when I add this in front of externalId the error still:
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=256m; support was removed in 8.0
[info] Loading project definition from F:\workspace\frankcheckAPI\project
[info] Set current project to frankcheckapi (in build file:/F:/workspace/frankcheckAPI/)
[info] Compiling 20 Scala sources to F:\workspace\frankcheckAPI\target\scala-2.11\classes...
[error] F:\workspace\frankcheckAPI\src\main\scala\com\cardaccess\fraudcheck\ReDFraudCheckResponse.scala:9: '}' expected but '.' found.
[error] this.externalId =lpTransaction.getField("somethinghere").toString
[error] ^
[error] F:\workspace\frankcheckAPI\src\main\scala\com\cardaccess\fraudcheck\ReDFraudCheckResponse.scala:12: eof expected but '}' found.
[error] }
[error] ^
[error] two errors found
[error] (compile:compileIncremental) Compilation failed
Your code is very Java-influenced. There are couple of things wrong here; when you fix the obvious ones like missing class parameters in RD it boils down to not being able to reassign to val.
Let me give you an improved, Scala-fied version of the whole code.
abstract class FR(val externalId: String, val code: String, val message: String) extends Serializable
// dummy class
class LPTransaction {
def getField(s: String) = s
}
class RD(externalId: String, code: String, message: String) extends FR(externalId, code, message) {
def this(lpTransaction: LPTransaction) = {
this(lpTransaction.getField("somethinghere").toString, "defaultCode", "defaultMessage")
}
println(externalId)
}
val a = new RD(new LPTransaction) // prints "somethinghere"
Main improvements are:
You don't need private fields to be populated using arguments in the constructor. Scala favors immutability. Make your class arguments "vals", this means they will be available as public fields (instead of getters you will access them directly; this is contrary to OOP's encapsulation principle, but here it's ok because nobody can mess with them anyway since they are immutable; they may only be fetched)
Your subclass RD should be taking same fields as parameters as its parent class. Of course, you can then define an auxiliary constructor that takes only LPTransaction, but then you need to feed the parent class with some default values for the other parameters.
The rest kind of follows from this. I added the dummy implementation of LPTransaction to be able to compile. I also threw in a println statement in RD class just for the sake of example. Feel free to ask if something's not clear.
//scala automatically generates getters for passed in params. No need to set them explicitly. For immutable params use val, for mutable use var
abstract class FR(var externalId:String, val code:String, val message:String) extends Serializable {
//need to use override annotation for superclass methods
override def toString:String={
return "FRworks";
}
}
// notice how constructor parameters are passed to the base class when defining the child class.
class RD extends FR("someID","code","msg") {
def printId() = println(externalId)
}
val x = new RD
x.externalId = "new ID" //works because externalId is var (mutable)
x.code = "new code" //error because code is val (immutable)
x.printId //correctly prints the external id: someID
Can someone help me to understand what is wrong with the code below? The problem is inside the "join" method - I am not able to set "state" field. Error message is -
No implicit view available from code.model.Membership.MembershipState.Val => _14.MembershipState.Value.
[error] create.member(user).group(group).state(MembershipState.Accepted).save
[error] ^
[error] one error found
[error] (compile:compile) Compilation failed
What does _14 mean? I tried similar thing with MappedGender and it works as expected, so why MappedEnum fails?
scala 2.10
lift 2.5
Thanks
package code
package model
import net.liftweb.mapper._
import net.liftweb.util._
import net.liftweb.common._
class Membership extends LongKeyedMapper[Membership] with IdPK {
def getSingleton = Membership
object MembershipState extends Enumeration {
val Requested = new Val(1, "Requested")
val Accepted = new Val(2, "Accepted")
val Denied = new Val(3, "Denied")
}
object state extends MappedEnum(this, MembershipState)
{
override def defaultValue = MembershipState.Requested
}
object member extends MappedLongForeignKey(this, User) {
override def dbIndexed_? = true
}
object group extends MappedLongForeignKey(this, Group) {
override def dbIndexed_? = true
}
}
object Membership extends Membership with LongKeyedMetaMapper[Membership] {
def join (user : User, group : Group) = {
create.member(user).group(group).state(MembershipState.Accepted).save
}
}
Try moving your MembershipState enum outside of the MembershipClass. I was getting the same error as you until I tried this. Not sure why, but the code compiled after I did that.
_14 means a compiler-generated intermediate anonymous value. In other words, the compiler doesn't know how to express the type it's looking in a better way.
But if you look past that, you see the compiler is looking for a conversion from [...].Val to [...].Value. I would guess that changing
val Requested = new Val(1, "Requested")
to
val Requested = Value(1, "Requested")
would fix the error.
(I'm curious where you picked up the "new Val" style?)
What's strange is that Val actually extends Value. So if the outer type was known correctly (not inferred to the odd _14) Val vs. Value wouldn't be a problem. The issue here is that Lift from some reason defines the setters to take the now-deprecated view bound syntax. Perhaps this causes the compiler, rather than going in a straight line and trying to fit the input type into the expected type, instead to start from both ends, defining the starting type and the required type, and then start searching for an implicit view function that can reconcile the two.