Reputation: 585
I am trying to interpret a body of existing Scala code. In this body of code I have a trait - that I call MyUsefulTrait - that declares a couple of methods for whom I supply an implementation in the class I mix aforementioned trait. My goal is simply to provide an initialization for these function so that my class compiles. Right now the IDE is complaining that I better give an implementation to these method or declare my class abstract. And I would rather not declare my class abstract.
Ao, here are my method declarations that need to be given a default implementation: Firstly. method 1 goes as follows:
def conveyFindingsOnFile: (MyFileInfo, AskReport) => FileHandledReport
where, MyFileInfo is a sealed trait that defines a couple of File metadata like path, when accessed last and its author.
and, AskReport is a case class that defines a few things like a Map, an exception message, etc.
And, lastly the FileHandledReport is a case class that defines things like some File metadata, based on some findings on the file handled.
I would like to provide a basic implementation or initialization, just to make the compiler happy .
My second method goes as follows:
def doResearch:Option[(AskReport) => Future[AskReport]]
Based on the answer I get for the first method, i will try to figure out the default implementation for the second one. I will try to. But yes, I would like the second method to also be given some initialization. Thanks in advance.
Upvotes: 0
Views: 214
Reputation: 1467
The second one is easier because you can simply write
def doResearch:Option[(AskReport) => Future[AskReport]] = None
From the first one you have to return function taking (MyFileInfo, AskReport)
and returning FileHandledReport
. If you know what FileHandledReport
should be, you can go with:
def foo(MyFileInfo, AskReport): FileHandledReport = ... //code
def conveyFindingsOnFile: (MyFileInfo, AskReport) => FileHandledReport = foo
Upvotes: 1
Reputation: 6242
If, as you mention, you only want to
(...) provide an initialization for these function so that my class compiles (...)
and
(...) make the compiler happy (...)
then you can just make those methods return some useless value according to their respective return types. For example,
override def conveyFindingsOnFile: (MyFileInfo, AskReport) ⇒ FileHandledReport = (a,b) ⇒ FileHandledReport(/* fill default values for the case class members here */)
override def doResearch: Option[(AskReport) ⇒ Future[AskReport]] = None
as long as you will not use them. Alternatively, you can use the built-in method ???
whose return type is Nothing
(which is a subtype of any other type) and throws an NotImplementedError
exception if you try to use those methods:
override def conveyFindingsOnFile: (MyFileInfo, AskReport) ⇒ FileHandledReport = ???
override def doResearch: Option[(AskReport) ⇒ Future[AskReport]] = ???
Moreover, this is the default action of IntelliJ IDEA, in case you are working with it. Hope it helped.
Upvotes: 1