I thought putting final in Function would stop this issue from happening? I'm not too familiar with Scala. Can someone explain?
Class 'Country must either be declared abstract or implement abstract member 'execute():Object' in path.to.Invokable'
class Country extends MarketFunction("america") {}
abstract class MarketFunction(function: String) extends Function {
...
}
trait Function extends Invokable {
final def execute(): AnyRef = {
// not important
}
}
public interface Invokable {
Object execute();
}
Is it because AnyRef is not a direct comparison to java Object?
This worked for me
class Country extends MarketFunction("america") {}
abstract class MarketFunction(function: String) extends Function[AnyRef] {
...
}
trait Function[A <: AnyRef] extends Invokable {
override final def execute(): A = {
// not important
}
}
public interface Invokable {
Object execute();
}
Related
How can I make arbitrary type T to conform to trait Run without having inherit the trait? Type class works at method level but class is expecting type Run and I cannot change method signature of run() as it is overriding an interface.
trait Run[T] {
def run(): Unit
}
class SomeClass(runner: Run[_]) extends Runnable {
#override def run() {
runner.run()
}
}
Current solution:
class SomeRunnable(someRunnable: SomeType) {
#override def run(){
someRunabble.someMethod()
}
}
I cannot make work the following code:
object Factory {
def apply[U <: Cda](type: MyType.Value): MyUtilTrait[U] = {
type match {
case MyType.Value.one => MyOneUtilCustom
case MyType.Value.two => MyTwoUtilCustom
}
}
}
=> Expression of type Factory.MyType doesn't conform to expected type MyUtilTrait[U]
trait MyUtilTrait[T <: Cda] {}
object MyOneUtilCustom extends MyUtilTrait[CdaOneCustom] { }
object MyTwoUtilCustom extends MyUtilTrait[CdaTwoCustom] { }
case class CdaOneCustom(...) extends Cda {}
case class CdaTwoCustom(...) extends Cda {}
abstract class Cda(...) {}
object MyType extends Enumeration {
val one, two = Value
}
With the apply, I am supposed to return a MyUtilTrait parametrized with a subtype of Cda, so what's wrong?
It's almost completely impossible to implement this apply method with such a signature [1], because someone could come along, define
class Unobtanium extends Cda {
// implement all `Cda` methods by `???`
}
and then invoke
Factory.apply[Unobtanium](MyType.one)
How is a factory supposed to create a MyUtilTrait[Unobtanium], if it knows nothing about Unobtanium, and it is the first time it sees this strange type?
Use existential type instead:
abstract class Cda {}
case class CdaOneCustom() extends Cda {}
case class CdaTwoCustom() extends Cda {}
trait MyUtilTrait[T <: Cda] {}
object MyOneUtilCustom extends MyUtilTrait[CdaOneCustom] { }
object MyTwoUtilCustom extends MyUtilTrait[CdaTwoCustom] { }
object MyType extends Enumeration {
val one, two = Value
}
object Factory {
def apply(typ: MyType.Value): MyUtilTrait[_] = {
import MyType._
typ match {
case `one` => MyOneUtilCustom
case `two` => MyTwoUtilCustom
}
}
}
[1] Unless your MyUtilTrait[X] is something trivial, like Nil (which is a List[X] for any X), or Consumer[Any] of some sort, which does not actually care about the type parameter.
I have a question concerning Scala override (as my title suggests)
Now I have the following classes/traits:
trait FSM {def transitionGraph:Map[String,(JsValue,FSM)]
abstract class AClass: FSM { def transitionGraph }
class Class extends AClass{ override def transitionGraph ... } <-- Wont work
trait OverrideTrait extends AClass { abstract override def transitionGraph } <-- works
class NewClass extends OverrideTrait { } <--- Works, I can use the overridden transitionGraph
My question is: Why can I not override things from an abstract class. Is it because I am never allowed to instantiate an abstract class. Thus the behavior :
val AClass class = new Class
is never allowed to happen?
Thanks.
There seems to be a lot of stuff omitted from the code you've given, so I'm not sure I get the question, but here's something similar that does compile:
trait FSM { def transitionGraph: String }
abstract class AClass extends FSM { def transitionGraph: String }
class Class extends AClass { override def transitionGraph = ??? }
trait OverrideTrait extends AClass { override def transitionGraph = ??? }
class NewClass extends OverrideTrait { }
Does this help at all?
Your code example wouldn't compile. But it should work once you corrected a few things:
trait FSM {def transitionGraph:Map[String,(JsValue,FSM)]}
abstract class AbstractClass extends FSM { def transitionGraph }
class ConcreteClass extends AbstractClass{ def transitionGraph = ??? }
val someClass: AbstractClass = new ConcreteClass
How to extend an object in Scala with an abstract class that has a constructor, and apply method of the object returns the object as subtype of the abstract?
for example :
abstract class AbstractResource(amount:Int) {
val amount:Int
def getAmount = amount
}
case object Wood extends AbstractResource{
def apply(amount: Int) = {
// something that returns the subtype
}
}
I think a good solution is:
abstract class AbstractResource {
val amount: Int = 0
def getAmount = amount
}
case object Wood extends AbstractResource {
def apply(quantity: Int) = {
new AbstractResource {
override val amount = quantity
}
}
}
But my problem is I can't edit AbstractResource
I have no idea why should Wood extend AbstractResource, but this works:
class AbstractResource(val amount:Int) {
def getAmount = amount
}
case object Wood extends AbstractResource(0) {
def apply(amount: Int) = {
new AbstractResource(amount)
}
}
I have some classes with a protected constructor and the factory method is inside the companion object of an abstract super class. As of Scala 2.9.0.RC4 this doesn't compile anymore. I have "fixed" the issue by making the constructors package protected. But I don't want other classes even inside the same package to be able to call the constructors.
So what should I?
sealed abstract class A
object A {
//the factory method, returning either a B or C
def apply(): A
}
class B protected (...) extends A
class C protected (...) extends A
You could make them private inner classes of the object.
object A {
private class B extends A
private class C extends A
}
Since you need the classes accessible for pattern matching, I would suggest creating a new subpackage for them and making the constructor private to that package. Now only the import statements in your client code need to be changed.
sealed abstract class A {
}
package myPackage.subPackage {
object A {
def apply(): A = new B
}
class B private[subPackage] () extends A {
}
}
package other {
object Foo {
def foo {
myPackage.subPackage.A()
//does not compile: new myPackage.subPackage.B
}
}
}
Another option is to create companion objects for each implementation of A and delegate construction to a factory method in this object:
sealed abstract class A
object A {
//the factory method, returning either a B or C
def apply(): A = {
if (...) B()
else C()
}
}
object B {
def apply() : B = new B()
}
class B private (...) extends A
object C {
def apply() : C = new C()
}
class C private (...) extends A