0

I'm new in Scala but familiar with Java.

Please check this two methods

def jsonPar(name:String): ScallaopOption[Map[String, Map[String, String]]] : opt[Map[String, Map[String, String]]](name,required = true)(jsonMapper)
    
def jsonMapper : ValueConverter[Map[String, Map[String, String]]] = 
    scallop.singleArgConverter(json)

now there is opt method in ScallopConfBase class

def opt[A](name : scala.Predef.String, short : scala.Char, and so on upto 10 more params)(implicit conv : org.rogach.scallop.ValueConverter[A]) : org.rogach.scallop.ScallopopOption[A]

I'm stuck this code to understand, here are my concerns

  1. opt needs to have 10 input params to execute, but in def jsonPar, only (name,required = true) is passed to opt, how ?

  2. what's a importance of adding (implicit conv : org.rogach.scallop.ValueConverter[A]) : org.rogach.scallop.ScallopopOption[A] in opt method ?

  3. what ScallaopOption[Map[String, Map[String, String]]] : opt[Map[String, Map[String, String]]](name,required = true)(jsonMapper) part is doing ?

can anyone share ? I'm trying to thing from java perspective, but still not able to understand.

Levi Ramsey
  • 18,884
  • 1
  • 16
  • 30
Sophia
  • 43
  • 6
  • 2
    Usually it is better to [ask one question per post](https://meta.stackexchange.com/q/222735). – Pshemo Jul 14 '22 at 11:28
  • @Pshemo, these are linked to my doubt – Sophia Jul 14 '22 at 11:29
  • 1
    even if you are trying to think from the Java perspective, I doubt this is related to Java... as the [tag:java] tag states: "*... Use this tag when you're having problems using or understanding the language itself. ...*" (meant is the Java Programming Language) – user16320675 Jul 14 '22 at 11:51
  • @Levi Ramsey, can you please share something ? – Sophia Jul 14 '22 at 12:15
  • As mentioned, I voted to close because there are multiple questions and edited the tags because this doesn't really have anything to do with java. At least one of the questions you're asking can only really be answered with posting (e.g. a github link) the full definition of `opt`. – Levi Ramsey Jul 14 '22 at 12:27

1 Answers1

2

Broadly, what implicit does is provide hints to the compiler to use when normal compilation fails.

For example, implicit conversions (which shouldn't generally be used) and implicit classes come into play when you try to call a method on an object whose type doesn't include that method. In that scenario, the compiler will, rather than reject the code immediately, search for an implicit conversion to a type which has that method and include a call to that conversion so that the method can be called. implicit classes work similarly (and use basically the same mechanism under the hood) as implicit conversions: if there's an implicit class available which can take that object as a constructor parameter, the compiler will effectively insert a new ImplicitClassName(originalObject).methodThatDoesntExistOnOriginalObject(...).

implicit parameters meanwhile rely on the idea of an implicit scope. If there's no explicitly provided argument for the implicit parameter, then the compiler will use an argument of that type from the implicit scope of the caller. Inside the method taking the implicit parameter, those parameters become part of the implicit scope so that that method can make calls using implicits.

The : ScallopOption[Map[String, Map[String, String]]] just means that the method returns a ScallopOption[Map[String, Map[String, String]]]. : ScallaopOption[Map[String, Map[String, String]]] : opt is not valid Scala; I suspect that the second : is an =.

You haven't provided the definition of opt, but it looks like it uses default parameters, so explicit parameters can be omitted in favor of the default values.

Levi Ramsey
  • 18,884
  • 1
  • 16
  • 30
  • Note that because `implicit` is used for so many different things, Scala 3 removes the keyword and provides the different functionalities through different keywords. – Levi Ramsey Jul 14 '22 at 12:35
  • @LevisRamsey, opt is present in abstract class > ScallopConfBase.class, secondly can you please share why (name,required = true) used as opt's parameters, becasue if see the opt def it can have aprox 10 input params, then from where these two are being used and why ? – Sophia Jul 14 '22 at 12:52