如何在 Scala 中定义一个返回 Java 对象的方法? [英] How to define a method in Scala that returns a Java object?
问题描述
我想在 Scala 单例类中定义一个私有方法,如下所示;
I want to define a private method in scala singleton class that looks like;
private def createDomNode(tag: String, attrs: Map[String , String]): DomNode {
}
DomNode 是 Java 类型,不是 Scala 类型.attrs 是 Scala Map,键和值都是 String 类型.
DomNode is Java type, not scala type. attrs is scala Map with both key and value being of type String.
但上面给出了错误.什么是正确的格式?
But above gives error. What is the correct format?
感谢 Easy Angel 的回答.还是有些混乱.根据语言发明者编写的Scala编程书,以下是一个函数:
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:
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?
什么是 REPL?
推荐答案
你应该把 =
:
private def createDomNode(tag: String, attrs: Map[String , String]): DomNode = {
// ...
}
如果不将=
放在方法签名和正文之间,则返回类型为Unit
,因此:
If you will not put =
between method signature and body, then return type is Unit
, so this:
def m(i: Int) {}
与
def m(i: Int): Unit = {}
<小时>
对评论的回应:我之前描述的实际上是方法,如果你在object
、class
或trait中定义它
定义.函数语法如下所示:
Response to the comment: What I described earlier is actually method, if you define it within object
, class
or trait
definition. Function syntax would look like this:
val createDomNode: (String, Map[String , String]) => DomNode = { (tag, attrs) =>
// ...
}
如您所见,我使用函数类型的名称 createDomNode
定义了 val
.也可以写成:
As You can see I define val
with name createDomNode
of function type. It can also be written like:
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
但重要的是要理解,在这种情况下,该方法返回一个返回 DomNode
的函数",而不是 DomNode
本身.
But it's important to understand, that method returns a "function that returns DomNode
" in this case, but not DomNode
itself.
关于 Scala 编程参考.我想您是在谈论第 2 章 - 第 3 步(介绍中)
如你所见,max
函数是在 REPL 中定义的,它是真正的函数.其实你也可以这样写:
As you can see max
function is defined in REPL, and it's really function. Actually you can also write something like this:
class MyClass {
def myMethod(i: Int): Int = {
def myInnerFn(x: Int) = x * x
myInnerFn(i)
}
}
在这种情况下,myMethod
是方法,myInnerFn
是函数.如您所见,这在很大程度上取决于上下文.我相信 myInnerFn
的这种语法只是语法糖(我需要查看规范才能确定):
In this case myMethod
is method and myInnerFn
is function. So as you can see, it highly depends on the context. I believe this syntax for myInnerFn
is just syntactic sugar for (I need to look in spec in order to say for sure):
val myInnerFn = (x: Int) => x * x
在 REPL 中也会发生同样的情况.顺便说一句,那是因为我在开头写道:
The same happens in REPL. And by the way that's because I wrote at the beginning:
如果你在object
、class
或trait
定义中定义
if you define it within
object
,class
ortrait
definition
抱歉,我需要更清楚地说明这一点,并在我的第二次更新中对其进行更详细的描述.
Sorry, I need to be more clear about this and describe it in more detail in my second update.
我查看了 Scala 规范.当我说 myInnerFn
是函数的语法糖时,我似乎并不完全正确.但似乎它被称为方法类型.您可以在规范部分3.3.1 方法类型中找到它:
I looked in Scala spec. Seems that I'm not totally correct when I say that myInnerFn
is 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:
http://www.scala-lang.org/docu/files/ScalaReference.pdf一个>
如果你想深入了解,希望它能给你一些线索.我认为很容易迷失在术语中.您可以在 2 个上下文中 function
.首先我们有
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 function
in 2 contexts. In first we have
- 函数 - 返回一些值
- 过程 - 不返回任何值(或在 Scala 上下文中它返回
Unit
)
在第二种情况下:
- 函数 - 可执行代码段,可以传递并作为值处理
- 方法 - 属于类
有时并不清楚它的含义.例如,我可以告诉你 myMethod
只是因为它具有返回值(或者换句话说:myMethod
它不是过程)就作为函数.我相信书中的案例也是如此.
And it's sometimes not clear in what context it meant. For example I can tell you that myMethod
is as function just because it has return value (or in other words: myMethod
it's not procedure). I believe it's the same case in book.
还有一件事.Scala 编程 中的第 8.1、8.2、8.3 节可以帮助您理解这个术语.如果我的假设是正确的,那么您认为的 Function
在书中称为 First-class function
(在第 8.3 节中进行了描述).
One more thing. Sections 8.1, 8.2, 8.3 from Programming in Scala can help you to understand this terminology. And if I'm correct in my assumptions, what you think as Function
is called First-class function
in the book (it's described in section 8.3).
希望能帮到你
这篇关于如何在 Scala 中定义一个返回 Java 对象的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!