如何在 Scala 中定义一个返回 Java 对象的方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6362352/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to define a method in Scala that returns a Java object?
提问by ace
I want to define a private method in scala singleton class that looks like;
我想在 Scala 单例类中定义一个私有方法,看起来像;
private def createDomNode(tag: String, attrs: Map[String , String]): DomNode {
}
DomNode is Java type, not scala type. attrs is scala Map with both key and value being of type String.
DomNode 是 Java 类型,而不是 Scala 类型。attrs 是 Scala 映射,键和值都是字符串类型。
But above gives error. What is the correct format?
但上面给出了错误。什么是正确的格式?
Thanks Easy Angel for the answer. There is still some confusion. According to Programming in Scala book written by the inventor of the language, the below is a function:
感谢易天使的回答。还是有些混乱。根据语言发明者编写的 Scala 编程书,以下是一个函数:
def max(x: Int, y: Int): Int = {
if (x > y) x
else y
}
But your answer says it is method and not function. Can you kindly explain?
但是你的回答说它是方法而不是函数。你能解释一下吗?
What is REPL?
什么是 REPL?
回答by tenshi
You should just put =:
你应该把=:
private def createDomNode(tag: String, attrs: Map[String , String]): DomNode = {
// ...
}
If you will not put =between method signature and body, then return type is Unit, so this:
如果您不会=在方法签名和正文之间放置,则返回类型为Unit,因此:
def m(i: Int) {}
is the same as
是相同的
def m(i: Int): Unit = {}
Response to the comment: What I described earlier is actually method, if you define it within object, classor traitdefinition. Function syntax would look like this:
对评论的回应:我之前描述的实际上是方法,如果你在 中定义它object,class或者trait定义。函数语法如下所示:
val createDomNode: (String, Map[String , String]) => DomNode = { (tag, attrs) =>
// ...
}
As You can see I define valwith name createDomNodeof function type. It can also be written like:
如您所见,我val使用createDomNode函数类型的名称进行定义。也可以写成:
val createDomNode: Function2[String, Map[String , String], DomNode] = { (tag, attrs) =>
// ...
}
here is another example. In this case I define method that generates new function each time you call it:
这是另一个例子。在这种情况下,我定义了每次调用时都会生成新函数的方法:
def createDomNode = (tag: String, attrs: Map[String , String]) => new DomNode
But it's important to understand, that method returns a "function that returns DomNode" in this case, but not DomNodeitself.
但重要的是要理解,DomNode在这种情况下,该方法返回一个“返回的函数”,而不是DomNode它本身。
About Programming in Scala reference. I think you are talking about Chapter 2 - Step 3 (in the intro)
关于在 Scala 参考中编程。我想你在谈论第 2 章 - 第 3 步(在介绍中)
As you can see maxfunction is defined in REPL, and it's really function. Actually you can also write something like this:
如您所见,max函数是在 REPL 中定义的,它确实是函数。其实你也可以这样写:
class MyClass {
def myMethod(i: Int): Int = {
def myInnerFn(x: Int) = x * x
myInnerFn(i)
}
}
In this case myMethodis method and myInnerFnis function. So as you can see, it highly depends on the context. I believe this syntax for myInnerFnis just syntactic sugar for (I need to look in spec in order to say for sure):
在这种情况下myMethod是方法和myInnerFn功能。如您所见,这在很大程度上取决于上下文。我相信这个语法 formyInnerFn只是语法糖(我需要查看规范才能确定):
val myInnerFn = (x: Int) => x * x
The same happens in REPL. And by the way that's because I wrote at the beginning:
在 REPL 中也会发生同样的情况。顺便说一句,那是因为我在开头写道:
if you define it within
object,classortraitdefinition
如果您在
object,class或trait定义中定义它
Sorry, I need to be more clear about this and describe it in more detail in my second update.
抱歉,我需要更清楚地说明这一点,并在我的第二次更新中更详细地描述它。
I looked in Scala spec. Seems that I'm not totally correct when I say that myInnerFnis syntactic sugar for the function. but seems that it's called Method Type. You can find it in spec section 3.3.1 Method Type:
我查看了 Scala 规范。当我说这myInnerFn是函数的语法糖时,我似乎并不完全正确。但似乎它被称为Method Type。您可以在规范第3.3.1节方法类型中找到它:
http://www.scala-lang.org/docu/files/ScalaReference.pdf
http://www.scala-lang.org/docu/files/ScalaReference.pdf
hope it will give you some clue, if you want to dive deeper in this. I think it's easy to get lost in terminology. You can functionin 2 contexts. In first we have
希望它能给你一些线索,如果你想更深入地研究这个。我认为很容易迷失在术语中。您可以function在 2 个上下文中。首先我们有
- Function - returns some value
- Procedure - returns no value (or in Scala context it returns
Unit)
- 函数 - 返回一些值
- 过程 - 不返回任何值(或在 Scala 上下文中它返回
Unit)
And in second context:
在第二种情况下:
- Function - executable piece of code that can be passes around and treated as value
- Method - belongs to the class
- 函数 - 可以传递并被视为值的可执行代码段
- 方法 - 属于类
And it's sometimes not clear in what context it meant. For example I can tell you that myMethodis as function just because it has return value (or in other words: myMethodit's not procedure). I believe it's the same case in book.
有时并不清楚它在什么上下文中的含义。例如,我可以告诉你这myMethod是作为函数,因为它有返回值(或者换句话说:myMethod它不是过程)。我相信书中的情况也是如此。
One more thing. Sections 8.1, 8.2, 8.3 from Programming in Scalacan help you to understand this terminology. And if I'm correct in my assumptions, what you think as Functionis called First-class functionin the book (it's described in section 8.3).
还有一件事。第8.1,8.2,8.3,从在Scala编程可以帮助你理解这个术语。如果我的假设是正确的,那么书中Function所说的就是您的想法First-class function(在第 8.3 节中进行了描述)。
Hope this helps
希望这可以帮助

