1

I tried to find various use cases of implicit functions in scala, but I found the examples with only one parameter to an implicit function, can it have 2 parameters if yes what will be the use case for it also, I want to know which are the real-world scenarios where implicit functions are used?

5
  • Based on your deleted comment "what you have explained is a function with 2 implicit parameters, what I am looking for is an implicit function with 2 parameters" you're looking for implicit conversions with multiple parameters stackoverflow.com/questions/20290630/… stackoverflow.com/questions/21131509/… stackoverflow.com/questions/2416733/… Commented Oct 9, 2020 at 18:45
  • stackoverflow.com/questions/10635406/… Commented Oct 9, 2020 at 18:45
  • Do these links answer your question? Commented Oct 9, 2020 at 18:48
  • Important are implicit defs with multiple implicit parameters, so probably implicit defs with multiple ordinary parameters are kept for consistency although they are not so useful. Commented Oct 9, 2020 at 19:13
  • I mean, not so useful as implicit defs with single ordinary parameter (implicit conversions) or implicit defs with single/multiple implicit parameters (conditional implicits). Commented Oct 9, 2020 at 19:49

3 Answers 3

4

Scala doesn't have a feature called implicit functions, but it does have one that's called views, or sometimes implicit conversions. It works as follows: If you have an implicit value of type A => B, or an implicit def that can be expanded to such a value in scope, when a value of type B is expected where you pass a value of type A, or when you make a selection a.x where there some x on B but not on A, then that conversion is called on a first.

so a implicit def foo(a: A): B = ??? can be expanded into an implicit val A => B and is an eligible implicit conversion, but an implicit def foo(a: A, b: B): C = ??? can't, so isn't eligible as a conversion.

Sign up to request clarification or add additional context in comments.

11 Comments

"Scala doesn't have a feature called implicit functions" – While that is technically true, I feel that you are splitting hairs. Yes, they were renamed to Context Functions last Summer, and yes, technically, Scala 3 isn't released yet, but it's clear what the OP is talking about.
I never heard anything called implicit functions myself. I'm assuming they're talking about views/implicit conversions, but I'm not 100% sure. Scala 3 context functions are really something else entirely.
Context functions were called implicit functions for almost the entire time that they existed, and the vast majority of resources still calls them that. Even the tutorial that is linked on the Context Functions page calls them Implicit Functions.
Ah, so they are. But are scala3/dotty implicit functions indeed what the question means? I doubt that.
@JörgWMittag regardless of whether you think OP ought to have meant something else, it has every appearance of them not talking about the former name of the yet unreleased dotty feature.
|
1

Probably you mean method extension. You can extend tuple in this case(or case class)

  def main(args: Array[String]): Unit = {
    val tuple = ("John", "Smith")
    val concat = tuple.tupleConcat
    println(concat)
  }

  implicit class TupleExt(val tuple: (String, String)) extends AnyVal {
    def tupleConcat = s"${tuple._1} ${tuple._2}"
  }

Also, you can read the official docs IMPLICIT CLASSES

2 Comments

The OP is asking about implicit functions, though, which are a very different concept from implicit classes.
@JörgWMittag The OP seems NOT to be asking about Scala-3 implicit functions. And extension methods (introduced by implicit classes) are sometimes legitimate replacement for implicit conversions (including absent implicit conversions with multiple parameters).
1

Please note that Implicit Functions were renamed to Context Functions. They are now documented here.

I tried to find various use cases of implicit functions in scala, but I found the examples with only one parameter to an implicit function, can it have 2 parameters

Yes. It can have as many parameters as you want. (The 22 parameter limit no longer exists in Scala 3.)

It is clear from the specification that there is no limit:

Context function types are shorthands for class types that define apply methods with context parameters. Specifically, the N-ary function type T1,… , TN => R is a shorthand for the class type ContextFunctionN[T1 ,… , TN, R]. Such class types are assumed to have the following definitions, for any value of N >= 1: […]

and further down:

Context function types generalize to N > 22 in the same way that function types do

So, it is clear that there can be an arbitrary number of parameters.

if yes what will be the use case for it

The obvious use case would be if you don't want a single context, but want to compose your context out of multiple orthogonal bits. Imagine a scenario where you need both logging and transactions. There is no reason why this should be a single monolithic context.

also, I want to know which are the real-world scenarios where implicit functions are used?

Scala 3 isn't released yet, so the body of code is still limited. The largest system that is built using Scala 3 is probably the Scala 3 compiler itself, which indeed uses context functions heavily. Not quite as heavy as context parameters, though.

I suspect that the vast majority of implicit functions will only ever have a single parameter. But that's not a reason to artificially restrict them. Some of the major goals of Scala are simplicity, regularity, and orthogonality, without any weird exceptions or corner cases.

Methods can have arbitrarily many parameters, ordinary functions can have arbitrarily many parameters, type constructors can have arbitrarily many parameters, constructors can have arbitrarily many parameters, in fact everything that can take parameters at all can take arbitrarily many of them, it would be strange to artificially restrict only implicit functions.

I mean, the vast majority of type constructors, methods, and ordinary functions probably also only have one parameter, or at most two, but we don't restrict them either. In fact, the arbitrary 22 parameter limit for Products, Tuples, Functions, PartialFunctions, and case classes has been a significant source of pain in Scala 2.

4 Comments

It seems OP meant not Scala-3 implicit functions but implict conversions with multiple parameters in Scala 2.
I do not see any statements of the OP to that effect in either the question or the comments to any of the answers. In a comment on a deleted answer, the OP even reaffirmed they are asking about implicit functions.
From that deleted Martijn's answer and Vivek's comment it's clear that Vivek (the OP) calls def example(normal: Int, params: String)(implicit config: Configuration, context: ExecutionContext) = ??? (Martijn's code snippet) a "function with 2 implicit parameters" but is looking for "an implicit function with 2 parameters" i.e., I guess, something like implicit def example(normal: Int, params: String).
I'll copy the deleted answer for those who can't see it snipboard.io/i9Ze5l.jpg

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.