Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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

like image 374
ace Avatar asked Jun 15 '11 18:06

ace


People also ask

How do you define a method in Scala?

We can define a method's list of parameters in parentheses after its name. After the parameters list, we can provide a return type for the method with the leading colon sign. We then define a method's body after the equals sign. The compiler allows us to skip the curly braces if there is only one statement of code.

What is the return type of a procedure in Scala?

If you don't specify any return type of a function, default return type is Unit which is equivalent to void in Java. = : In Scala, a user can create function with or without = (equal) operator. If the user uses it, the function will return the desired value.

How do you call a method in Scala?

Method Invocation is a technique that demonstrates different syntax in which we dynamically call methods of a class with an object. There should not be any space between the invocation object/target and the dot(.) nor a space between the dot and method name.


1 Answers

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

like image 136
tenshi Avatar answered Sep 28 '22 01:09

tenshi