score:5
In short, you can't.
Here are your options:
Forwarding implicits
Say you have a function that executes a Future
and you need an ExecutionContext
:
You can either import scala.concurrent.ExecutionContext.Implicits.global
(or define your own etc) in the same file as someFun
, and you get this:
import scala.concurrent.ExecutionContext.Implicits.global
def someFun(): Future[SomeThing] = {
Future(something)
}
Or if you don't want to import in the same file(class, object etc) as someFun
, you forward the implicit. This way, you can import it when you use someFun
in another file.
def someFun()(implicit ex: ExecutionContext): Future[SomeThing] {
Future(someThing)
}
Context bounds
Sounds like this is what you want, only you didn't implement it properly.
class Stub[T](name: String)
def myFunc[T: Moo](stub: Stub[T]) = {
println(stub + " " + implicitly[Moo[String]])
}
The above is the equivalent of:
def myFunc[T](stub: T)(implicit evidence: Moo[T]) = {
println(stub + " " + evidence.toString)
}
View bounds
Alternatively, if it makes more sense to implicitly convert a Stub
to a Moo
rather than bound it:
def myFunc[T <% Moo](stub: T) = {
println(stub + " " + implicitly[Moo[T]])
}
To use the above, you need to provide materialized implicits(e.g a way to convert a stub to a Moo). For example:
implicit def stubToMoo[T](stub: Stub[T]): Moo[T] = {
new Moo(stub) // or whatever
}
Bottom line, in your question usage of implicits makes no sense whatsoever. You can do the "thin-air" import as you describe, but based on the options above, see if it's worth it.
score:0
Then there is no need to declare it implicit
. Consider this:
object B {
val moo = "World!"
}
...
import B._
object A {
def myFunc(stub: String) {
println(stub + " " + moo)
}
}
myFunc("Hello")
Source: stackoverflow.com
Related Query
- How to use implicit value with out passing it in to function?
- How can I use an implicit function with two parameters (Scala)?
- How to use from_json standard function with custom schema (error: overloaded method value from_json with alternative)?
- How do you create scala anonymous function with multiple implicit parameters
- Scala - currying function with implicit value
- How to automatically generate a function to match a sealed case class family with implicit instances?
- How to pass a function with implicit as parameter
- How to use a partial function composed with orElse as a udf in spark
- Scala: How to define anonymous function with implicit parameter?
- how can I keep partition'number not change when I use window.partitionBy() function with spark/scala?
- Value and column operations in scala spark, how to use a value left of an operator with spark column?
- scala passing function with underscore produces a function not a value
- How to use Except function with spark Dataframe
- How to use function with Any input
- How to mock a function that returns a value class instance with Mockito?
- How would I curry the String.format function to use it with map over an array?
- Function reference as implicit parameter with default value not as expected
- Get value from map returned from function with implicit in Scala
- How to invokePrivate on a function with implicit parameters
- How to use a value as a parameter to target function
- How do I use scala and scalatest to see if a list contains an object with a field matching a specific value
- How to declare a class as extending a function with implicit parameters
- How to pass the initial value to foldLeft from a filtered List with function chaining?
- How to get value out of anonymous function in scala?
- How to define a type of function with default value in Scala
- How to parse None received for a Map value together with a function call?
- How do I get the type signature to agree with the scala compiler when extracting a value out of an Option[A]?
- How to use length function in lpad, where the length of lpad is decided based on the column value
- Scala: How to filter out maps with a common key value from a list of maps
- How to use pattern matching with function values in Scala
More Query from same tag
- Can I use Scala TypeTags and ClassTags to traverse an object model without instances?
- what is the best practice of implementing a factory pattern/method in scala?
- Best practice for global constants in Scala Application
- Find the proper first item in a list by checking it only one time, and return the checking result
- Scala Spark read last row under specific column only
- More readable way to write code with strings which contain quotes `"`
- how to use list.collect or list match to get proper data
- What is the idiomatic (and fast) way of treating the empty list/Seq as failure in a short-circuiting operation?
- I have a function which takes an implicit parameter. How can I pass the parameter implicitly using implicit parameters of an instance of some class?
- provide Get instance for Seq
- Spray REST API Application Structure - Suggestions Needed
- Is cons a method or a class?
- Can Akka Actors replace Service layer?
- Use cases of Scala collection forwarders and proxies
- Spray multiple post parameters
- Google App Engine API - Guava Dependency Conflict
- Process big data using hadoop parquet to CSV output
- "spark.memory.fraction" seems to have no effect
- How can I create a typed Tuple2 from Java / Spring?
- How to Global onRouteRequest directly to onBadRequest?
- Scala IO monad: what's the point?
- scala actors vs threads and blocking IO
- How to load complex xml files containing more than 1 row tag into dataframe using spark scala and save it as table(note generic solution)
- Implement static inner interface as anonymous class in Scala
- circe automatic derivation - struggling with the imports
- How can I get an empty collection of the same type as a given instance?
- To make a variable or column name an object in Spark
- Spark Iterating RDD over another RDD with filter conditions Scala
- Why are there multiple deserializers in kafka like string,byte, short?
- Spark accumulator empty when used in UDF