Questions tagged [implicit]

An implicit in Scala is a function applied or a parameter provided without explicitly appearing in the source code.

There are two variants of implicits in Scala: implicit conversions and implicit parameters.

Implicit conversions are applied by the scala compiler when it encounters an expression which it can not compile, but which can be compiled when one of the elements are passed to a function and the return value is used instead of the original element. A typical use case is the enrich-my-library pattern.

For example Scala allows the syntax "some.*regexp".r although the literal between the quotes is a String and String does not have a method r. But there is a method defined in Predef: augmentString(x: String): StringOps and StringOps has an r method. So this implicit conversion is applied.

Implicit parameters are parameters that are added to a method call by the compiler. A somewhat famous use case is in the collection library. Many methods accept a CanBuildFrom parameter which is used to define the type of collection returned by a method. In most cases this parameter is not specified explicitly but an implicit default value is used, which allows the collection library to return the "correct" specialized collection.

Only fields marked as such are considered for use as implicit parameters or conversions.

While this feature makes it possible to create powerful, concise and type safe DSLs and other APIs it should be used with care, since it also allows to create code that is rather hard to understand.

1780 questions
9
votes
1 answer

Type inference for a scala combinator calculus data model

I'm trying out a very light-weight encoding of combinator calculus in scala. Initially, I'm simply implementing the S and K combinators, application and constant values. Later I hope to lift scala functions in and allow evaluation of an expression…
drdozer
  • 329
  • 1
  • 3
  • 10
9
votes
2 answers

Implicit parameter resolution for higher kinded types

Consider the following code: object foo { trait Bar[Q[_]] implicit object OptionBar extends Bar[Option] def test[T, C[_]](c: C[T])(implicit bar: Bar[C]) = () def main(args: Array[String]) { test(Some(42): Option[Int]) …
Landei
  • 54,104
  • 13
  • 100
  • 195
9
votes
2 answers

How to use imports and implicits in Play Framework's routes file?

What's the scope for routes file to find implicits like PathBindable or QueryStringBindable? For custom types, it's trivial to simply define them in companion object like the following: case class Foo(data: String) object Foo { implicit val…
Daniel Shin
  • 5,086
  • 2
  • 30
  • 53
9
votes
2 answers

python (sympy) implicit function: get values instead of plot?

I am new to sympy but I already get a nice output when I plot the implicit function (actually the formula for Cassini's ovals) using sympy: from sympy import plot_implicit, symbols, Eq, solve x, y = symbols('x y') k=2.7 a=3 eq = Eq((x**2 +…
xaratustra
  • 647
  • 1
  • 14
  • 28
9
votes
1 answer

Scala Typeclasses with generics

I've been playing with the typeclass pattern in Scala, but I haven't been able to figure out how to implement the implicit companion object when the type I'm working with is generic. For example, let's say I've defined a trait for a typeclass that…
KChaloux
  • 3,918
  • 6
  • 37
  • 52
9
votes
3 answers

cannot create implicit token for string literal in non-combined grammar

so found a nice grammar for a calculator and copied it with some lil changes from here: https://dexvis.wordpress.com/2012/11/22/a-tale-of-two-grammars/ I have two Files: Parser and Lexer. Looks like this: parser grammar Parser; options{ …
FelRPI
  • 429
  • 6
  • 15
9
votes
1 answer

Does Inheritance in implicit value classes introduce an overhead?

I want to apply scala's value classes to one of my projects because they enable me to enrich certain primitive types without great overhead (I hope) and stay type-safe. object Position { implicit class Pos( val i: Int ) extends AnyVal with…
peri4n
  • 1,389
  • 13
  • 24
8
votes
1 answer

Methods versus Function and implicits in Scala

Let's declare a def and an equivalent function as a val: scala> def optional(x:Int):Option[String] = None optional: (x: Int)Option[String] scala> val optional2:(Int)=>Option[String] = (i:Int) => None optional2: Int => Option[String] =…
Kim Stebel
  • 41,826
  • 12
  • 125
  • 142
8
votes
7 answers

Implicit cast to string - toString and int + ""

Why when i use this: int a = 1; methodWithParamString(a + ""); a is cast to String, bu i can't use toString() on integer? int a = 1; methodWithParamString(a.toString()); Doesn't this: a+"" works like: a.toString() + "" ?
MicNeo
  • 716
  • 3
  • 12
  • 20
8
votes
2 answers

Why does Scala fail to find a secondary implicit value in this one particular case?

I am having a hard time explaining the difference in behavior between additional implicit values sought by either a primary implicit value or an implicit conversion. Specifically, this works: trait Foo[A] implicit def fooString: Foo[String] =…
Owen
  • 38,836
  • 14
  • 95
  • 125
8
votes
2 answers

Adding object methods implicitly

Is there a way to implicitly add methods in scala object? Upd: For example, Unfiltered scala library have singleton object Body which contains methods Body.string(req: HttpRequest) and Body.bytes(req: HttpRequest) for read body from http request.…
KkZz
  • 83
  • 1
  • 5
8
votes
1 answer

Scala Generics and Numeric Implicits

I need to pass two functions as parameters to a scala function. That function should then evaluate them and get a number from them where it will then operate on. This number can be either a Int, Double or any other numeric type. I would like the…
tiagoboldt
  • 2,426
  • 1
  • 24
  • 30
8
votes
1 answer

What was the reason to restrict on combining implicit parameters and view/context bounds?

One of the recent commits to Scala master removes restriction on combining context/view bounds with implicit parameters. That's a great improvement that reduces amount of boilerplate, but what was the reason of making that restriction before, and…
Vasil Remeniuk
  • 20,519
  • 6
  • 71
  • 81
8
votes
2 answers

Difference between Scala 2 implicits and Scala 3 given/using

What is the difference between the implicit keyword in Scala 2 and given+using in Scala 3? Is it just that implicit has been split up into two keywords, or are the semantics also different, and if so, how?
user
  • 7,435
  • 3
  • 14
  • 44
8
votes
2 answers

Why does the Option's orNull method have this superfluous implicit argument?

I wonder what is the reason for the (implicit ev: Null <:< A1) here: sealed abstract class Option[+A] extends Product with Serializable { def orNull[A1 >: A](implicit ev: Null <:< A1): A1 = this getOrElse null ... } Wouldn't def orNull[A]: A…
soc
  • 27,983
  • 20
  • 111
  • 215