简体   繁体   English

Scala:如何初始化应该返回未来的抽象方法

[英]Scala: How to initialize an abstract method that is supposed to return a future

I am trying to interpret a body of existing Scala code. 我试图解释现有的Scala代码。 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. 在这段代码中,我有一个特质-我称之为MyUsefulTrait-声明了我混合了上述特质的类中为其提供实现的几个方法。 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. 现在,IDE抱怨我最好给这些方法一个实现或声明我的类抽象。 And I would rather not declare my class abstract. 而且我宁愿不声明我的班级抽象。

Ao, here are my method declarations that need to be given a default implementation: Firstly. o,这是我的方法声明,需要提供默认的实现:首先。 method 1 goes as follows: 方法1如下:

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. 其中,MyFileInfo是一个密封特性,它定义了几个File元数据,例如path,最后一次访问时及其作者。

and, AskReport is a case class that defines a few things like a Map, an exception message, etc. 而且,AskReport是一个案例类,它定义了一些内容,例如Map,异常消息等。

And, lastly the FileHandledReport is a case class that defines things like some File metadata, based on some findings on the file handled. 而且,最后,FileHandledReport是一个案例类,它基于对所处理文件的一些发现来定义诸如某些文件元数据之类的内容。

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. 提前致谢。

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: 其返回类型为Nothing (这是任何其他类型的子类型),并且如果您尝试使用这些方法,则抛出NotImplementedError异常:

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. 此外,如果您正在使用它,这是IntelliJ IDEA的默认操作。 Hope it helped. 希望能有所帮助。

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 . 从第一个开始,您必须返回函数(MyFileInfo, AskReport)并返回FileHandledReport If you know what FileHandledReport should be, you can go with: 如果知道FileHandledReport应该是什么,则可以使用:

def foo(MyFileInfo, AskReport): FileHandledReport = ... //code 
def conveyFindingsOnFile: (MyFileInfo, AskReport) => FileHandledReport = foo

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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