Questions tagged [implicit]

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

implicit
Filter by
Sorted by
Tagged with
439 votes
2 answers
57k views

Where does Scala look for implicits?

An implicit question to newcomers to Scala seems to be: where does the compiler look for implicits? I mean implicit because the question never seems to get fully formed, as if there weren't words for ...
Daniel C. Sobral's user avatar
288 votes
1 answer
42k views

What are Scala context and view bounds?

In a simple way, what are context and view bounds and what is the difference between them? Some easy-to-follow examples would be great too!
chrsan's user avatar
  • 3,390
215 votes
9 answers
136k views

Implicit type conversion rules in C++ operators

I want to be better about knowing when I should cast. What are the implicit type conversion rules in C++ when adding, multiplying, etc. For example, int + float = ? int * float = ? float * int = ? ...
Matt Montag's user avatar
  • 7,315
174 votes
4 answers
68k views

What is the Scala identifier "implicitly"?

I have seen a function named implicitly used in Scala examples. What is it, and how is it used? Example here: scala> sealed trait Foo[T] { def apply(list : List[T]) : Unit }; object Foo { | ...
oluies's user avatar
  • 17.8k
152 votes
15 answers
89k views

Can we define implicit conversions of enums in c#?

Is it possible to define an implicit conversion of enums in c#? something that could achieve this? public enum MyEnum { one = 1, two = 2 } MyEnum number = MyEnum.one; long i = number; If not, ...
Adam Naylor's user avatar
  • 6,222
83 votes
9 answers
56k views

explicit and implicit c#

I'm new to C# and learning new words. I find it difficult to understand what's the meaning of these two words when it comes to programming c#. I looked in the dictionary for the meaning and here's ...
tintincutes's user avatar
  • 5,688
82 votes
8 answers
196k views

Android, How to read QR code in my application?

In my application I need to read Qr code. I searched the net and found Zing codes however lots of developers had problem with using it and it seems it is buggy! If i assume that my customers has qr ...
Hesam's user avatar
  • 53k
78 votes
3 answers
9k views

How can I chain implicits in Scala?

The pimp-my-library pattern allows me to seemingly add a method to a class by making available an implicit conversion from that class to one that implements the method. Scala does not allow two such ...
Daniel C. Sobral's user avatar
74 votes
8 answers
23k views

Good example of implicit parameter in Scala? [closed]

So far implicit parameters in Scala do not look good for me -- it is too close to global variables, however since Scala seems like rather strict language I start doubting in my own opinion :-). ...
greenoldman's user avatar
  • 20.5k
73 votes
2 answers
262k views

TypeError: Can't convert 'int' object to str implicitly [duplicate]

I am trying to write a text game and I have run into an error in the function I am defining that lets you basically spend your skill points after you make your character. At first, the error stated ...
anon's user avatar
  • 1,437
67 votes
11 answers
23k views

What are type classes in Scala useful for?

As I understand from this blog post "type classes" in Scala is just a "pattern" implemented with traits and implicit adapters. As the blog says if I have trait A and an adapter B -> A then I can ...
Michael's user avatar
  • 10.2k
56 votes
7 answers
20k views

ReSharper and var [duplicate]

Possible Duplicate: Why does ReSharper want to use 'var' for everything? I have ReSharper 4.5 and have found it invaluable so far but I have a concern; It seems to want to make every ...
Refracted Paladin's user avatar
53 votes
0 answers
1k views

Materialize the Value for a Type that has One Inhabitant

Thanks to @MilesSabin's answer I can write a type level Fibonacci sequence: sealed trait Digit case object Zero extends Digit case object One extends Digit sealed trait Dense { type N <: Dense } ...
beefyhalo's user avatar
  • 1,741
52 votes
1 answer
4k views

Can't prove that singleton types are singleton types while generating type class instance

Suppose I've got a type class that proves that all the types in a Shapeless coproduct are singleton types: import shapeless._ trait AllSingletons[A, C <: Coproduct] { def values: List[A] } ...
Travis Brown's user avatar
50 votes
1 answer
712 views

Unexpected implicit resolution based on inference from return type

Given a typeclass where instance selection should be performed based on the return type: case class Monoid[A](m0: A) // We only care about the zero here implicit def s[T] : Monoid[Set[T]] = Monoid(...
Patrick Prémont's user avatar
49 votes
1 answer
20k views

What is a diverging implicit expansion error?

While trying to find a solution to another question ([1]) I came across a diverging implicit expansion error. I'm looking for an explanation about what this means Here's the use case: scala> ...
IttayD's user avatar
  • 28.7k
42 votes
5 answers
30k views

How to declare traits as taking implicit "constructor parameters"?

I'm designing a class hierarchy, which consists of a base class along with several traits. The base class provides default implementations of several methods, and the traits selectively override ...
Andrzej Doyle's user avatar
40 votes
6 answers
47k views

Is it possible to plot implicit equations?

I would like to plot implicit equations (of the form f(x, y)=g(x, y) eg. X^y=y^x) in Matplotlib. Is this possible?
Geddes's user avatar
  • 1,209
40 votes
5 answers
19k views

Scala: Passing one implicit parameter implicitly and the other explicitly. Is it possible?

Let's consider the function: def foo(implicit a:Int, b:String) = println(a,b). Now, let us assume that there is an implicit String and Int (implicit val i1=1) in scope but we want to pass an other, ...
jhegedus's user avatar
  • 20.5k
37 votes
4 answers
6k views

Is there a way to declare an implicit val inside a for comprehension?

I have some code with nested calls to flatMap like so: foo.flatMap(implicit f => bar(123).flatMap(b => /* and so on... implicit f is still in scope here.*/ )) Normally, one would write that ...
Kim Stebel's user avatar
32 votes
1 answer
2k views

Log implicits only for "diverging implicit expansion"s

Other answers suggest using "-Xlog-implicits" option for debugging "diverging implicit expansion" errors. However, it also logs a lot of implicits in places unrelated to these errors. Is there some ...
Alexey Romanov's user avatar
31 votes
5 answers
19k views

Get companion object of class by given generic type Scala

What I am trying to do is to make a function that would take a generic class and use a static method in it (sorry for Java language, I mean method of its companion object). trait Worker {def doSth: ...
Wojtek Erbetowski's user avatar
30 votes
1 answer
9k views

Conditions under which compiler will not define implicits (constructor, destructor, copy constructor, copy assignment) [duplicate]

This is supposed to be a trivial question but I could not find it explicitly on stackoverflow. The following will be defined implicitly if not provided by the user. default (parameterless) ...
aiao's user avatar
  • 4,701
30 votes
3 answers
7k views

Implicit keyword before a parameter in anonymous function in Scala

I understand implicit parameters and implicit conversions in Scala but I saw this for the first time today: the implicit keyword in front of a parameter in an anonymous function: Action { implicit ...
platypus's user avatar
  • 1,185
27 votes
2 answers
25k views

"Cannot find an implicit ExecutionContext" error in scala.js example app.

Here is an example from the Hands-on Scala.js ebook: package webpage import org.scalajs.dom.ext.Ajax import scala.scalajs.js import scala.scalajs.js.annotation.JSExport import scalatags.JsDom.all._ ...
qed's user avatar
  • 22.7k
26 votes
8 answers
3k views

Why do members of a static class need to be declared as static? Why isn't it just implicit?

Obviously there can't be an instance member on a static class, since that class could never be instantiated. Why do we need to declare members as static?
richard's user avatar
  • 12.6k
25 votes
3 answers
5k views

Scala Functional Literals with Implicits

Forgive me if this has already been asked elsewhere. I have a Scala syntax question involving function-values and implicit parameters. I'm comfortable using implicits with Scala's currying feature....
shj's user avatar
  • 1,628
24 votes
2 answers
10k views

What are implicit objects?

I was reading about type classes where implicit objects were mentioned: object Math { trait NumberLike[T] { def plus(x: T, y: T): T def divide(x: T, y: Int): T def minus(x: T, y: T): T ...
jhegedus's user avatar
  • 20.5k
23 votes
2 answers
2k views

Scala implicit usage choices

I've been wondering whether transparent implicit conversions are really such a good idea and whether it might actually be better to use implicits more, um, explicitly. For example, suppose I have a ...
oxbow_lakes's user avatar
23 votes
1 answer
3k views

Scala implicit object vs implicit val

I've seen two ways (one less than the other) of declaring implicit for typeclass pattern in Scala. implicit val instance1 = new Typeclass { def do = ??? } implicit object instance2 extends Typeclass ...
Daniel Shin's user avatar
  • 5,146
22 votes
6 answers
16k views

How make implicit Ordered on java.time.LocalDate

I want to use java.time.LocalDate and java.time.LocalDateTime with an implicit Ordered like: val date1 = java.time.LocalDate.of(2000, 1, 1) val date2 = java.time.LocalDate.of(2010, 10, 10) if (date1 &...
Michael's user avatar
  • 246
22 votes
2 answers
23k views

Implicit return values in Ruby

I am somewhat new to Ruby and although I find it to be a very intuitive language I am having some difficulty understanding how implicit return values behave. I am working on a small program to grep ...
csamuel's user avatar
  • 820
22 votes
3 answers
29k views

How does SQL Server decide format for implicit datetime conversion?

declare @str_datetime varchar(50) set @str_datetime='30-04-2012 19:01:45' -- 30th April 2012 declare @dt_datetime datetime select @dt_datetime=@str_datetime This is giving following error: Msg ...
Shwet Jain's user avatar
22 votes
5 answers
12k views

popen implicitly declared even though #include <stdio.h> is added

This is tiny snippet of my code. #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <time.h> #include <sys/stat.h> #include <sys/...
Chris Allen's user avatar
22 votes
2 answers
3k views

Is there a systematic way to discover which implicit defs are in scope, and which one is bound at a particular point?

Often there's no need to pay any attention to implicit arguments in Scala, but sometimes it's very helpful to understand how the compiler is automatically providing them. Unfortunately, this ...
Scott Morrison's user avatar
22 votes
1 answer
491 views

How does implicit <:< help to find type parameters

A couple of questions arise while I'm reading 7.3.2 Capturing type constraints from Joshua's Scala in Depth. The example excerpted from the book: scala> def peek[C, A](col: C)(implicit ev: C <:&...
cfchou's user avatar
  • 1,239
21 votes
3 answers
8k views

Why is there an implicit type conversion from pointers to bool in C++?

Consider the class foo with two constructors defined like this: class foo { public: foo(const std::string& filename) {std::cout << "ctor 1" << std::endl;} foo(const bool ...
user avatar
20 votes
1 answer
14k views

Not able to import Spark Implicits in ScalaTest

I am writing Test Cases for Spark using ScalaTest. import org.apache.spark.sql.SparkSession import org.scalatest.{BeforeAndAfterAll, FlatSpec} class ClassNameSpec extends FlatSpec with ...
himanshuIIITian's user avatar
20 votes
1 answer
5k views

Is there a way to control which implicit conversion will be the default used?

Suppose I have this: class String2(val x:String) { def *(times:Int) : String = { val builder = new StringBuilder() for( i <- 0 until times) { builder.append(x) ...
Geo's user avatar
  • 94.7k
20 votes
4 answers
5k views

How can I get implicit conversions to work inside collections?

Say I have an implicit conversion: implicit def aToB(a: A):B={ ... } How can I get this implicit conversion to work on the elements of a List? If I have: val listOfA: List[A] ... and I have a ...
Jack's user avatar
  • 16.6k
20 votes
5 answers
8k views

Scala dependency injection: alternatives to implicit parameters

Please pardon the length of this question. I often need to create some contextual information at one layer of my code, and consume that information elsewhere. I generally find myself using implicit ...
Marcus Downing's user avatar
19 votes
5 answers
44k views

Xcode C++ Vectors: Implicit instantiation of undefined template

I ran this code on a different IDE and it was successful. For some reason I get the above error message on Xcode. I assume I'm missing a header of some kind, but I'm not sure which one. #include <...
Nathan Hale's user avatar
19 votes
3 answers
5k views

Best way to handle false unused imports in intellij

Intellij falsely marked some import of Scala implicits as not being use. Is there a way to prevent it from deleting those import when optimized them explicitly for a specific import and not prevent ...
NetanelRabinowitz's user avatar
19 votes
6 answers
1k views

Other programming languages that support implicits "a la Scala"

Scala implicits are very powerfull. I'm curious if they are a new/unique feature of Scala, or the concept already existed in other programming languages. Thanks. EDIT: To clarify my question, yes, ...
gerferra's user avatar
  • 1,519
19 votes
1 answer
2k views

Magnet pattern and overloaded methods

There is a significant difference in how Scala resolves implicit conversions from "Magnet Pattern" for non-overloaded and overloaded methods. Suppose there is a trait Apply (a variation of a "Magnet ...
Vladimir Kostyukov's user avatar
19 votes
4 answers
791 views

Why in C# an explicit decimal => long conversion operator is called implicitly, losing precision? [duplicate]

The following C# program silently and implicitly calls an explicit decimal-to-long conversion operator, losing precision. I don't understand why this happens. As far as I understand, in C# explicit ...
kaalus's user avatar
  • 4,495
19 votes
2 answers
2k views

Using context bounds "negatively" to ensure type class instance is absent from scope

tl;dr: How do I do something like the made up code below: def notFunctor[M[_] : Not[Functor]](m: M[_]) = s"$m is not a functor" The 'Not[Functor]', being the made up part here. I want it to succeed ...
nadavwr's user avatar
  • 1,830
19 votes
1 answer
5k views

Why do we still need a .lib stub file when we've got the actual .dll implementation?

i'm wondering why linkers can not do their job simply by consulting the information in the actual .dll files that got the actual implementation code ? i mean why linkers still need .lib files to do ...
geeko's user avatar
  • 2,770
18 votes
2 answers
6k views

Passing scala.math.Integral as implicit parameter

I have read the answer to my question about scala.math.Integral but I do not understand what happens when Integral[T] is passed as an implicit parameter. (I think I understand the implicit parameters ...
Michael's user avatar
  • 10.2k
18 votes
3 answers
10k views

Why does this explicit call of a Scala method allow it to be implicitly resolved?

Why does this code fail to compile, but compiles successfully when I uncomment the indicated line? (I'm using Scala 2.8 nightly). It seems that explicitly calling string2Wrapper allows it to be used ...
Matt R's user avatar
  • 10.2k

1
2 3 4 5
41