开发者

How to define a method in Scala that returns a Java object?

I want to define a private method in scala singleton class that loo开发者_如何学编程ks like;

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.

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:

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?


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:

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, class or trait definition. Function syntax would look like this:

val createDomNode: (String, Map[String , String]) => DomNode = { (tag, attrs) =>
    // ...
}

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

But it's important to understand, that method returns a "function that returns DomNode" in this case, but not DomNode itself.


About Programming in Scala reference. I think you are talking about Chapter 2 - Step 3 (in the intro)

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)
    }
}

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

The same happens in REPL. And by the way that's because I wrote at the beginning:

if you define it within object, class or trait definition

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 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

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

  • Function - returns some value
  • Procedure - returns no value (or in Scala context it returns 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 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.


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).

Hope this helps

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜