score:11
I think you should revisit using all these implicit unless you are doing some really funky DSL.
Here is one way to solve the problem you are having. As you may have guessed, implicit work on the Type not the name, so having two implicit with the same type is a No-No.
Since Scala 2.10, Scala allows you to "inline" classes using AnyVal (SIP-15). Here is an example of a so called value class:
case class Path(p: String) extends AnyVal
Now instead of using Strings to represent your entities you enclose them in this nice "wrapper" class. The cool thing about this is that the compiler will take care of replacing all Path objects in your code with a String during compilation time. So in terms of compiled code, you get the same performance as if you used a String. You gain a lot of type safety without paying a runtime penalty.
Now coming back to your case, here is how to solve the problem you have:
case class Path(s: String) extends AnyVal
case class BaseUrl(s: String) extends AnyVal
def foo(implicit a: Path, b: BaseUrl) = a.s ++ b.s
implicit val a: Path = Path("a")
implicit val b: BaseUrl = BaseUrl("b")
Here is how to use it:
scala> foo
res0: String = ab
score:2
As Marios says, the problem is that you're using implicits for types like String and Int. Since implicits work on type and not on name the compiler would not know where to put an 'implicit String'. That's why custom types should be used. One thing I took away from last ScalaDays conference is that you should create types for everything because the compiler can than help you to verify you're program is correct.
However, looking at your solution, I would not use implicits at all. It's better to use a single argument list and provide defaults for all or most values. Then 'makeRequest' could default do a 'Get /' and this behavior can be changed by providing 'path = "/search"' or 'method = "POST"' for example.
Also use some kind of scoped type for http method since you already know what valid values are and which ones you'd like to support.
Source: stackoverflow.com
Related Query
- Scala multiple implicit parameters with defaults resulting in ambiguous values
- How do you create scala anonymous function with multiple implicit parameters
- Scala implicit parameters with defaults defined in the companion object
- Using query parameters with multiple values in Scala Dispatch 0.9.5
- Scala ambiguous reference to overloaded definition with two implicit parameters
- How to apply a customized function with multiple parameters to each group of a dataframe and union the resulting dataframes in Scala Spark?
- Defining a function with multiple implicit arguments in Scala
- scala cats ambiguous implicit values
- Scala named and default arguments in conjunction with implicit parameters
- Scala by name parameters with multiple argument lists and currying
- Enums in Scala with multiple constructor parameters
- Multiple constructors with the same number of parameters exception while transforming data in spark using scala
- Type of a function with Implicit parameters in Scala
- scala implicit method with multiple arguments
- Scala apply method call as parentheses conflicts with implicit parameters
- Scala resolution of multiple implicit parameters
- Scala generic implicit values ambiguous when overloading?
- Vectorize Scala function with multiple return values
- Auxiliary Constructor in Scala with multiple parameters
- How to get match type with multiple type parameters to work correctly in Scala 3
- Scala Typeclass with multiple parameters error
- Scala operator overloading with multiple parameters
- Scala Elasticsearch query with multiple parameters
- How to check whether multiple columns values of a row are not null and then add a true/false resulting column in Spark Scala
- In a scala project with multiple modules, where to define values accessible globally in all the build.sbt's within the same project
- Scala UDF with multiple parameters used in Pyspark
- Mutable map with multiple values in Scala
- Providing multiple instances of same implicit specialized with different type parameters
- Question about Scala implicits with multiple type parameters
- How implicit is chosen by Scala compile when we have multiple ambiguous implicit present in scope
More Query from same tag
- Problem with sort in scala, got "Diverging implicit expansion ....." error. Sorting a list of tuples based on its first element but in reverse order
- Force single argument in scala varargs
- Best way to unpack an option field inside a map operation in scala
- Testing higher order functions in scala
- Reads[T] validator error using ReactiveMongo
- How to include timestamp(created_at and updated_at) on my models using SORM?
- How to build a Zscale core? (RISC-V, rocket-chip)
- writing netbeans RCP apps on scala
- Split a string if a substring is present
- Spark SQL data frame
- Scala CSV-like data source to flat json representation
- how can I connect a jar file to commits of the source code?
- Using Scala companion object as factory by extending a factory trait. Better solution?
- How is the "Any" class works with the "this" variable?
- Generic stream and abstract data types
- Scala: InputStream to Array[Byte]
- Why is this matching not allowed in scala?
- Showing inferred types of Scala expressions
- How to optimize window aggregation over large windows?
- How to open TCP connection with TLS in scala using akka
- Sort Order for Scala Set and shuffling
- How to get current project name in sbt build.scala
- How to read from standard input line by line?
- Http4s circe can not decode children
- In Scala how to ensure for comprehension iterates through all non empty lists?
- Concurrent operations on Spark DataFrame
- Scala: Filtering collection based on type parameter
- In Scala, how can I reassign tuple values?
- Get Class object of class with companion object
- Custom equality between classes