2

How do you declare a variable that can be assigned with such method:

def inTransaction[A](a: =>A): A =
  if(! Session.hasCurrentSession)
    _executeTransactionWithin(SessionFactory.newSession, a _)
  else {
    a
  }

Thanks in advance, Etam.

2 Answers 2

2

As paradigmatic said, you need to assign a type. But as a workaround, you can use an object.

object myFunc {
  def apply[A](a: =>A): A = ???
}
Sign up to request clarification or add additional context in comments.

1 Comment

I am using anonymous class, I was just asking if it is possible to make it simpler.
1

You cannot directly assign a method to a variable, it must be transformed in a closure object (Function*). This transformation may happen implicitly or manually by adding an _ after the function name.

Since a variable cannot be generic, you must fix the type during assignation. For instance:

scala> def size[A]( lst: List[A] ) = lst.size
size: [A](lst: List[A])Int

scala> var f = size[Int] _
f: List[Int] => Int = <function1>

1 Comment

I don't want to specify the Int parameter.

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.