What is this Scala construct?

Sometimes when you're just learning a new language, it's very difficult to find an answer to something very fundamental just because the keywords to search for are either very trivial (lead to too many irrelevant search results) or I don't know which keywords to use.

I watched the first movie from Jetbrains regarding their IntelliJ with Scala features ( https://www.youtube.com/watch?v=UJe4wPUtcUQ#t=235 ). In this frame, you see this code:

test("Email with valid address") {
    val email = Email("name@gmail.com")
    assert(email.address != null)
}

      

This seems strange (coming from Java) and looking at the Scalatest.FunSuite API docs. I see that Funsuite is a functional test suite, but I cannot find the function test

in the docs FunSuite

( http://doc.scalatest.org/2.2.1/index.html#org.scalatest.FunSuite ).

My guess was that the function test

somehow requires you to define an anonymous function after it, but what does that mean? What returns test

? Where can I find the API document test

if not in the class FunSuite

?

+3


source to share


2 answers


In the area you should read, there is Scala support for passing functions as arguments to other functions. What you're looking for is the Scala syntax for a function that returns a function, which in turn takes another function as an argument (something delicious!). Some code will make it clearer, Scala source code:

test("Email with valid address") {
    val email = Email("name@gmail.com")
    assert(email.address != null)
}

      

which is the same as

test("Email with valid address")( {
    val email = Email("name@gmail.com")
    assert(email.address != null)
} )   

      

Note the extra binding, assuming that the block of code is passed as an argument to another function.

To make it a little more specific, in Java we can see it like this:



test("Email with valid address").apply( new Function0() {
    public void invoke() {
        val email = Email("name@gmail.com")
        assert(email.address != null)    
    }
}

      

Or in Java 8

test("Email with valid address").apply( 
    () -> {
        val email = Email("name@gmail.com")
        assert(email.address != null)    
    }
}

      

Note that the Scala variation is quite concise and is intended to support the extension of the language itself. In this way, libraries can create APIs that look like language-level syntax.

For more information, the following two blog posts will be helpful: defining custom control structures and on the name parameter for the function

+5


source


You are having difficulty finding the method test

in the API docs because by default, non-public members are not shown, but test

marked as protected

.

enter image description here

If you click the show all button, the method test

appears in the list of participants.



enter image description here

Extending its documents will also show the attribute where it is defined (definition classes FunSuiteLike

).

enter image description here

+2


source







All Articles