score:8
you can't. only methods can have implicit parameters.
when you do this:
// val f: a => unit =
{
implicit a: a => {
// some action
}
}
you're actually declaring an anonymous function of type a => unit
and you are declaring the argument a
as implicit in the function body
you can achieve something close to what you want using the magnet pattern:
class a
case class magnet()
object magnet {
implicit def fromunit(arg1: unit)(implicit a: a) = magnet()
}
object test extends app {
implicit val a = new a
{
args: magnet => {
//...
}
}.apply()
}
you'll get a deprecation warning though because the magnet must have at least one parameter and i used unit
, you should call it like .apply(())
to avoid it
score:3
as said by giovanni: you can't have such a parameter.
however you can use implicitly
to resolve implicits within your function:
case class foo(text : string)
implicit val foo = foo("world")
(() => {
val implfoo : foo = implicitly[foo]
println(s"hello ${implfoo.text}")
}).apply()
(but to be honest this sounds like it can be written better and you're going into spaghetti code territory with what you're doing.)
Source: stackoverflow.com
Related Query
- How do you create scala anonymous function with multiple implicit parameters
- Scala: How to define anonymous function with implicit parameter?
- How to define implicit function with a type parameter in Scala?
- Implicit keyword before a parameter in anonymous function in Scala
- Scala: How do I define an anonymous function with a variable argument list?
- How to pass a function with implicit as parameter
- How to get Implicit parameter into an anonymous function
- How to extend class with implicit parameter in Scala
- How to define scala function with undefined number and type of arguments
- How to call a function on an object with a generic type parameter using scala macros?
- How to call a Scala function with short parameter list?
- How to define a type of function with default value in Scala
- How much code does the body of an anonymous Scala function with "_" (underscore) comprise?
- Strange implicit def with function parameter behaviour in Scala
- Scala - how to pass implicit parameter into function (HOF)?
- How to call a Scala function with a single parameter and write the parameter first
- Scala how to reference a method that with an implicit parameter
- What type of FunctionN instance is created when creating anonymous function with no parameter in Scala
- How to pass an Exception, with default "cause" as function parameter in Scala
- How do I define a function parameter default associated with a generic parameter?
- Defining a function with multiple implicit arguments in Scala
- scala anonymous function missing parameter type error
- How can I define an anonymous generic Scala function?
- Scala How to use extends with an anonymous class
- Scala regex replace with anonymous function
- How to stub a method call with an implicit matcher in Mockito and Scala
- How to create a Scala class with private field with public getter, and primary constructor taking a parameter of the same name
- scala By-name parameter on a anonymous function
- In Scala, how to pass function with multiple parameter lists as an argument?
- How to define a scala method with type param that cannot be Any
More Query from same tag
- spark: how to include dependencies with build/sbt compile
- How can I make the "scalas" script runner more quiet?
- Flink stream processing count unique issues per repo
- Why does getActualTypeArguments return Object instead of Integer for Option[Int]?
- How to define a new setting and set its value for a task?
- Converting mutable hashmap to immutable hashmap
- Using "_" in Scala instead of Explicit Types
- Storing thrift-serialized objects on disk
- Exception in thread "main" java.lang.UnsupportedOperationException: Schema for type Unit is not supported
- How to show scala doc from Java Editor in Eclipse?
- Specifying flywayUrl through system property in SBT
- Inject a list of objects to play application context
- Aggregate multiple columns in Spark dataframe
- Closing a resource stored in Option[ ]
- Class objects inheritance / covariance
- Whats the equivalent of RDD aggregateByKey for Spark dataframe using scala?
- Scalacheck arbitrary with generics
- Cassandra auto create table code in production
- Convert OpenCV Mat in Java to NumPy Array in Scala
- Scala find duplicate in list
- do not want string as type when using foreach in scala spark streaming?
- Apache Kafka does not consume from api
- how to assign column names available in csv file as header to orc file
- Serialization of Try/Success/Failure in Scala Akka
- How to call the scala interpreter in a Simple Build Tool project?
- How to combine PartialFunction with another function and return PartialFunction?
- Match classes in Scala with Mockito
- Add value with groupByKey
- scala - getting java.util.NoSuchElementException
- scala return types with traits and concrete classes