0

Can you do something like this were Java is a java library

class TryStuff(method:() => Any) {
   private val lib: Java = new Java

   val tryStuff:TryStuff = new TryStuff(lib.method)
}

Is there someway to convert lib.method to a scala object

1
  • 1
    What do you mean by library? Package? I don't see anything problematic in the code Commented Nov 16, 2013 at 12:57

2 Answers 2

1
val tryStuff:TryStuff = new TryStuff(x => lib.method(x))

or more concisely

val tryStuff:TryStuff = new TryStuff(lib.method _)
Sign up to request clarification or add additional context in comments.

3 Comments

or more concisely: val tryStuff = new TryStuff(lib.method _)
It does not compile. The 'method' function passed in the TryStuff constructor does not take parameter.
It was just an example, the original code did not compile either.
0

You do not need to do anything special. Java methods are ready to be invoked in the Scala code.

In your example any method that does not have parameters can be used, in this case I use the method exists() of class java.io.File:

import java.io.File

class TryStuff(method:() => Any) {
    private val lib: File = new File("a.file")
    val tryStuff:TryStuff = new TryStuff(lib.exists)
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.