score:12
I've found a workaround:
class Greetings(implicit val greetings: String = "hello") {
def say(name: String): String = greetings + " " + name
}
Like this I can have a default value and override it if I want:
new Greetings().say("loic") //> res0: String = hello loic
implicit val greetings = "hi" //> greetings : java.lang.String = hi
new Greetings().say("loic") //> res1: String = hi loic
new Greetings()("coucou").say("loic") //> res2: String = coucou loic
Note: new Greetings()("coucou")
is working, not new Greetings("coucou")
, because of a syntax strangeness explained here.
score:25
It is a bad idea to use such a general type as String
in an implicit.
The main reason is that implicit lookup is solely base on the type, so what if someone else defines another implicit value of type String? You might end up with a conflict. So you should define your own specific type for your own purpose (a simple wrapper around String).
Another reason is that when looking for implicit values, the compiler will look (among other places) into the companion object (if any) of the implicit value type. You can easily see how useful it is, as the companion object is the natural place to put a default implicit value (as in your case). But if the implicit value is of a type that you don't own (such as String
) you just cannot write a companion object for it, while with your own wrapper type there is no problem.
OK, enough verbiage, here is how you can do it:
case class Greetings( value: String ) {
override def toString = value
}
object Greetings {
// this implicit is just so that we don't have to manually wrap
// the string when explicitly passing a Greetings instance
implicit def stringToGreetings( value: String ) = Greetings( value )
// default implicit Greetings value
implicit val greetings: Greetings ="hello"
def say(name: String)(implicit greetings: Greetings): String = greetings + " " +name
}
Greetings.say("loic")
Greetings.say("loic")("hi")
Source: stackoverflow.com
Related Query
- How to provide default value for implicit parameters at class level
- How to check which parameters of case class have default value using scala reflection 2.10
- Default value for implicit parameter of class method
- How can I fix the missing implicit value for parameter ta: TildeArrow in a test spec
- How can I obtain the default value for a type in Scala?
- How do I supply an implicit value for an akka.stream.Materializer when sending a FakeRequest?
- How do I create an explicit companion object for a case class which behaves identically to the replaced compiler provided implicit companion object?
- Circe encoder for generic case class with default parameters
- scala Map.getOrElse - how to provide function for default
- How can provide JsonFormats for case class that references itself?
- How to define implicit class for Traversable with CanBuildFrom?
- How do I write a scala extractor for a case class with default parameters?
- How to define cast and implicit cast operations for a Scala class of mine?
- Can I provide a default value for an abstract type in Scala?
- Implicit conversions for value class types?
- How to generate values for case class parameters in scala
- How scala determines implicit type parameters for TreeSet constructor
- How to make a generic implicit class for any collection in Scala?
- Scala - How to set default value instead None for Option[Int]
- Why do I get this compilation error: "could not find implicit value for kstream.Consumed" and how could I fix it?
- Play how to implement an implicit Writes or Format for a case class including an Enumeration
- How to declare a class as extending a function with implicit parameters
- Play2 + Casbah: How to provide an implicit Writes for ObjectId
- How to declare in scala a default param in a method of an implicit class
- Scala: How to define the default value for a constructor parameter within the companion object?
- Writing a single implicit class for both a value and a functor of that value
- How to write implicit Writes for case class having more than 22 fields
- How to iterate on scala class members for found null value
- How to specify default value when extracting Option from Option case class
- Scala: Multiple type parameters for implicit class
More Query from same tag
- Does type inference slow down auto-completion in the IDE
- sbt high cpu usage with ~run
- How can we check existence of element in postgresql scala?
- Accessing a json array nested in a structure using Spark
- JavaScript source code generation library for scala
- Complex Json schema into custom spark dataframe
- Play! Scala JSON object handling
- Skuber delete items in a specific namespace
- How to add JFROG artifactory to the sbt.build file?
- Play Framework Dependency Injection error
- Left Outer join for unequla records fro two data frames in spark scala
- Scala: Convert a Sealed Trait instance to an HList
- Why doesn't Scala Source close the underlying InputStream?
- In what way is Scala's Option fold a catamorphism?
- Is it possible to override form helpers?
- Universal copy function as a Macro
- Scala illegal start of simple expression with postfix function
- Difference between class parametrs and injected parameters
- Case class inheritance is prohibited, but how to model dependency from library?
- to_timestamp with spark scala is returning null
- "Method mapping in object Forms" Error
- Path-dependent types in scala
- How to flatten a sequence within a case class
- Distinct Last By
- Searching the Scala documentation for #::
- How to parse a string with filter criteria in scala and use it to filter objects
- How to change runtime JAVA heap space in intellij IDEA on MacOS?
- Mutable maps with object keys
- Calling map on a parallel collection via a reference to an ancestor type
- Spark join 2 dataframe based on multiple columns