Questions tagged [implicit-conversion]

Converting an object, variable or value from one type to another to satisfy a type restriction, without specifically requesting that conversion through language syntax.

Converting an object, variable or value from one type to another to satisfy a type restriction, without specifically requesting that conversion through language syntax.

Implicit conversion is a subset of .

2401 questions
1
vote
1 answer

How to implement automatic conversion from type class to interface syntax (Cats example)

I am working through the Scala with Cats book and I am wondering how the library implements some functionality that is described in an example. Specifically it is about automatically generating an implicit class from an existing type class…
Allen Han
  • 1,163
  • 7
  • 16
1
vote
1 answer

Two implicit definitions with same name for a method

I have two implicit declarations that "redefine" x as an operator: import scala.io.StdIn._ import util._ import scala.language.postfixOps case class Rectangle(width: Int, height: Int) case class Circle(ratio: Integer) case class Cylinder[T](ratio:…
Jose Cabrera Zuniga
  • 2,348
  • 3
  • 31
  • 56
1
vote
1 answer

Implicit conversion is applied when supposedly equivalent implicit class is not

I have this fairly simple example where one function foo(), which takes an implicit argument of type A, tries to call another function bar(), which takes an implicit argument of type B. In the project where I ran into this question, B contains a…
Feuermurmel
  • 9,490
  • 10
  • 60
  • 90
1
vote
1 answer

Unexpected Result for Implicit Type Conversion

I am trying to cause an implicit type conversion between an int and unsigned int in some code that I wrote. The compiler gives an unexpected result. According to the rules for implicit conversion between signed and unsigned integers (of equal rank),…
Lane Surface
  • 56
  • 1
  • 6
1
vote
1 answer

Can the argument of a converting constructor be implicitly converted?

Right now I'm working on a project where convertibility between two classes (A and B) is highly desirable. I've created a function myFunc() that takes an object of class A as its argument, and I've passed it an object of class B. The reason I…
ggrajeda
  • 13
  • 3
1
vote
1 answer

Cannot resolves symbol X, when defining multiple implicit vals

I am testing out some code shown below that basically defines multiple implicit vals taking a string as input and converting it to corresponding types. The problem I have is that the conversions like toLong, toDouble and toInt become unresolved for…
d-_-b
  • 4,142
  • 6
  • 28
  • 43
1
vote
2 answers

How to convert a string type to an array of chars

I'm trying to translate some C code to D, and I've come across this: char[] welcome = "\t\tWelcome to the strange land of protected mode!\r\n"; It gives this warning: main.d:5:18: error: cannot implicitly convert expression ("\x09\x09Welcome to the…
S.S. Anne
  • 15,171
  • 8
  • 38
  • 76
1
vote
2 answers

How to define partially parameterize generic implicit class?

Is it possible to define partially parameterize generic implicit class ? For instance assume I have following class implicit class IoExt[L, R](val io: IO[R]) { def wrapped(errorCode: String): IO[Either[ProcessingResult[L], R]] = ??? } How…
expert
  • 29,290
  • 30
  • 110
  • 214
1
vote
2 answers

How do I fix implicit conversion errors in my c# quicksort algorithm?

I'm trying to implement a quicksort algorithm to sort an array of floats. Whenever I reference an index in the array, i get this error: Cannot implicitly convert type 'float' to 'int'. An explicit conversion exists (are you missing a…
Dylan
  • 21
  • 3
1
vote
1 answer

When does implicit type conversion occur in C++?

Let's say we have the following code, meant to shift the bits of a to the left by i and replace the i LSBs of a with the i MSBs of b unsigned short a; unsigned short b; auto i = 4; a = (a << i) | (b >> (16 - i)); This works as expected. Now,…
Throckmorton
  • 564
  • 4
  • 17
1
vote
1 answer

Is there any reason not to put Scala implicits into a trait?

I started adding some implicit conversions to my code base. I didn't really research how this was done in Scala or look at many examples, so I implemented these as traits. For example, this snippet lets you test the schema of a Spark…
kingledion
  • 2,263
  • 3
  • 25
  • 39
1
vote
3 answers

Similar conversion in overloading wstring and wchar_t *

I have following code: inline bool match(const std::wstring & text1, const std::wstring & text2) { return match(text1.c_str(), text2.c_str()); } inline bool match(const std::wstring & text1, const wchar_t * text2) { return…
relaxxx
  • 7,566
  • 8
  • 37
  • 64
1
vote
1 answer

How do static and const resolve ambigous function calls?

I am currently trying to understand the concepts of functions calls and noticed you can write horrible code, but still can make it work by abusing implicit conversion, const and static. Now I would like to understand why and especially how this…
Imago
  • 521
  • 6
  • 29
1
vote
1 answer

Should Non-Copyable class have user conversion

My question is, should non-copyable objects have implicit/explicit user conversion? At least from my example below, conversions look very much like copies. PS: I know that is recomended here to "Avoid implicit conversion operators"…
1
vote
1 answer

Can't convert template of subclass to another template of base class even though conversion exists

In the following code: class Class { }; class Subclass : public Class { }; template class Template1 { public: }; template class Template2 { public: template Template2(const Template1& t1) { …