Questions tagged [implicit]
An implicit in Scala is a function applied or a parameter provided without explicitly appearing in the source code.
implicit
344
questions
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 ...
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 = ?
...
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!
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?
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 ...
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 ...
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 ...
2
votes
1
answer
1k
views
In scala 2 or 3, is it possible to debug implicit resolution process in runtime?
In scala language, implicit resolution is often done in compile-time and sometimes throws obfuscating error information, one famous example of such error is when shapeless Generic throws error ...
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 {
| ...
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 ...
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?
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, ...
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 ...
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 ...
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)
...
17
votes
3
answers
12k
views
Scala: Implicit parameter resolution precedence
Suppose we have implicit parameter lookup concerning only local scopes:
trait CanFoo[A] {
def foos(x: A): String
}
object Def {
implicit object ImportIntFoo extends CanFoo[Int] {
def foos(x: ...
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 ...
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]
}
...
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....
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 ...
12
votes
5
answers
3k
views
Why does the compiler choose bool over string for implicit typecast of L""?
Having recently introduced an overload of a method the application started to fail.
Finally tracking it down, the new method is being called where I did not expect it to be.
We had
setValue( const ...
5
votes
1
answer
554
views
Why is this implicit ambiguity behaviour happening?
I have a typeclass Search, which has an instance Search[A] if we have a TypeClass1[A] or a TypeClass2[A] instance. With preference given to the 1 instance.
The following compiles:
trait TypeClass1[A]...
0
votes
2
answers
514
views
Type class instance for case objects defined in sealed trait
In Scala 2.13 I have a case where I define some operation for all of types extending some sealed trait EnumType. I made it working but I'd like getTypeClass function not to be dependant on concrete ...
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 ...
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 ...
4
votes
1
answer
322
views
How to wrap a method having implicits with another method in Scala?
I have a method with implicits:
def f(x: String)(implicit dispatcher: ExecutionContextExecutor, mat: ActorMaterializer) = ???
And I want to create a helper method like:
def g1(y: String) = f("uri1" ...
0
votes
2
answers
139
views
When doing implicit resolution with type parameters, why does val placement matter?
In one file, I have:
trait JsonSchema[T] {
val propertyType: String
override def toString: String = propertyType
}
object JsonSchema {
implicit def stringSchema: JsonSchema[String] = new ...
0
votes
2
answers
129
views
In scala 3, is it possible to make covariant/contravariant type constructor to honour coercive subtyping?
This is a simple example:
object CoerciveCovariance {
trait Cov[+T]
def cast[A, B](v: Cov[A])(
implicit
ev: A <:< B
) = {
v: Cov[B]
}
}
It doesn't compile:
...
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: ...
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 ...
11
votes
2
answers
966
views
create an ambiguous low priority implicit
Consider the default codec as offered in the io package.
implicitly[io.Codec].name //res0: String = UTF-8
It's a "low priority" implicit so it's easy to override without ambiguity.
implicit val ...
5
votes
3
answers
2k
views
Is a constructor a function and is it possible to call a constructor
I came across this where one of the user comment says:
A constructor cannot be called, it is not a function. It is invoked automatically when a new object is created.
My question is that is the ...
1
vote
1
answer
465
views
In scala, are there any condition where implicit view won't be able to propagate to other implicit function?
Assuming that A class called 'summoner' was defined, that is capable of summoning implicit views from the scope:
case class Summoner[R]() {
def summon[T](v: T)(implicit ev: T => R): R = ev(v)...
1
vote
1
answer
454
views
Implicit view not working - is my implicit def to blame?
I'm having some trouble with an implicit view. I suspect this is quite trivial and may have some embarassingly easy answer. I have a situation like this, along with (obviously, unsuccessful) attempts ...
1
vote
1
answer
357
views
In scala 2.13, how to use implicitly[value singleton type]?
The following simple code:
implicit val a: String = "abc"
implicitly[a.type]
fails to compile despite that a is totally in the scope and consistent in type:
Error:(9, 13) could not ...
0
votes
1
answer
630
views
Pass implicit parameter through multiple objects
I wonder is it possible to pass implicit params through singletons like that
case class Greet(g: String)
object Foo {
def greet(name: String)(implicit greet: Greet = Greet("Hello")) = ...
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 ...
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/...
13
votes
2
answers
1k
views
"can't existentially abstract over parameterized type..."
I was messing around with Scala 2.8 for fun and trying to define a pimp which adds an "as" method to type constructors, allowing to convert from one functor to another (please overlook the fact that I'...
11
votes
2
answers
2k
views
How can implicits with multiple inputs be used in Scala?
For example, how can I write an expression where the following is implicitly applied:
implicit def intsToString(x: Int, y: Int) = "test"
val s: String = ... //?
Thanks
8
votes
2
answers
2k
views
Scala: generic weighted average function
I want to implement a generic weighted average function which relaxes the requirement on the values and the weights being of the same type. ie, I want to support sequences of say: (value:Float,weight:...
3
votes
0
answers
137
views
What are the hidden rules regarding the type inference in resolution of implicit conversions?
I thought that if the following compiles:
implicitly[X => Y]
than so will this:
(??? :X) :Y
It turns out I was wrong. Backstory: I toyed with an implementation of type unions:
private[this] val ...
3
votes
1
answer
178
views
Finding the second matching implicit
Consider the following setup:
trait Foo[A]
object Foo extends Priority2
trait Priority0 {
implicit def foo1: Foo[Int] = new Foo[Int] {}
}
trait Priority1 extends Priority0 {
implicit def foo2: ...
2
votes
1
answer
149
views
Scala Kleisli throws an error in IntelliJ
trying to implement Kleisli category for a made-up Partial type in Scala (reading Bartosz Milewski's "category theory for programmers", that's exersize for chapter 4)
object Kleisli {
type ...
2
votes
1
answer
356
views
Shapeless3 and annotations
This is a followup on Shapeless and annotations. The original question was asked in the context of Scala 2 and Shapeless2.
Some features from Shapeless2 were migrated to Shapeless3, such as ...
1
vote
1
answer
1k
views
Why can't Scala find my typeclass instance defined implicitly in the companion object, when the typeclass is not in a dedicated source file?
Please refer to the source code below. All source code is defined in the same package. When I define all of the code within a single source file ShowMain.scala, I get a compile error, however when ...
1
vote
1
answer
385
views
NullPointerException on implicit resolution
This code results in NullPointerException:
import anotherpackage.MyContext
import anotherpackage.builders.aMyContext
case class Context(id: String)
object Context {
implicit def `ContextHolder to ...
1
vote
1
answer
6k
views
Implicit wait Command Not Working-selenium webdriver C#
Guys, I have started to work on selenium web driver. You can assume I am a beginner. At the moment I am having difficulties in implementing the implicit wait command in my code (C#). it is not working ...
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> ...
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 ...