Does Scala provide a way to make an anonymous val?

Can you make an anonymous val?

I am going to do something like this:

case class NumIterations[A](num: Int)
case class Seed[A](seed: A)
case class Manipulate[A](f: A => A)
. . .

def funcWithGobsOfImplicitArgs[A](
  implicit numIterations: NumIterations[A],
           seed: Seed[A],
           manipulate: Manipulate[A],
           . . .
): A = . . .

def apply(): String = {
  implicit val NumIterations[String](10)  // This is where I want anonymous vals
  implicit val Seed("xyz")
  . . . 
  funcWithGobsOfImplicitArgs
}

It’s good that all implicit function arguments will probably be overboard, but I really have a real application where it’s convenient to insert some functional parameters into the scope and then reuse and redefine them. This makes the experiment with the function very convenient. I can play with one parameter at a time explicitly, and let all the others be provided implicitly.

val, , val . vals, _, , Scala val, . vals _, Scala .

+1
1

, . , . , , , , vals .

class Arg1(arg: Int) extends AnyVal
class Arg2(arg: Int) extends AnyVal
def f1(implicit arg: Arg1)
def f2(implicit arg: Arg2)
implicit val arg1 = new Arg1(1)
implicit val arg2 = new Arg2(2)
f1 // will pick arg1
f2 // will pick arg2

(, ExecutionContext), executeContext , , , .

+1

All Articles