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 convert a Java Stream to Scala Array?

Given new Scanner(is) .tokens() .map(_.toInt) .toArray[Int]((_: Int) => Array.ofDim[Int](100000)) I get a compile error Error:(40, 12) no type parameters for method map: (x$1: java.util.function.Function[_ >: String, _ <:…
Abhijit Sarkar
  • 21,927
  • 20
  • 110
  • 219
1
vote
1 answer

How to use SFINAE to enable implicitness of explicitness of the conversion operator?

Consider the following code: // Preamble #include #include // Wrapper template struct wrapper { // Implicit conversion template
Vincent
  • 57,703
  • 61
  • 205
  • 388
1
vote
1 answer

Issue with Scala Implicit Conversions

In the below code excerpt, the last statement does not compile. However, the statement immediately before that one does in fact compile. This second-to-last statement is what I would expect the compiler would convert the last statement to. I do not…
N W
  • 13
  • 1
  • 2
1
vote
3 answers

Simplifying std::transform for single vector

I want to write a simple function to simplify std::transform for a transformation on a single vector. What I've got so far: template void transform(std::vector &vec, const std::function &fun) { …
1
vote
1 answer

Assigning primitive datatype directly to Custom Object using indirect cast in C#

I created a Class called Parameter, which can hold a generic value as an object. The parameter object also has a Unit object associated with it, that can provide information about the unit of measure for conversion purposes elsewhere in my program.…
Jason O
  • 753
  • 9
  • 28
1
vote
2 answers

Implicit Conversion Not Working for Dynamic Type

I am running into a problem when trying to implicitly convert one of my dynamic types. There are two assemblies with definitions similar to the following: Configuration.dll: public class ConfigurationValue : DynamicObject { public…
Max Ehrlich
  • 2,479
  • 1
  • 32
  • 44
1
vote
3 answers

Cannot convert null to struct when defining two nullable implicit operators

The following does not compile: public struct Foo { public static implicit operator Foo(string bar) { return new Foo(); } public static implicit operator Foo(long? bar) { return new Foo(); } public…
Michael Olsen
  • 404
  • 3
  • 14
1
vote
2 answers

Implicit conversion from null

Looking Zoran Horvats courses at PluralSight, I'm currently implementing a Maybe type, a bit like Zoran has on its GitHub account: https://github.com/zoran-horvat/option Generally, a Maybe is a wrapper around objects, which are either set or have a…
Matthias Müller
  • 3,336
  • 3
  • 33
  • 65
1
vote
3 answers

How does Swift's int literal to float inference work?

Swift can infer int literals to be doubles or floats let x: Float = 3 It even works with arithmetic. It will convert everything before doing the math, so this is also 3: let y: Float = 5/2 + 0.5 But what are the actual rules for this? There are…
Max
  • 21,123
  • 5
  • 49
  • 71
1
vote
1 answer

Struct to class and back implicit conversion

Suppose I have a struct and a class with the same members: using System; class app { static void Main() { foo f = new foo() { a = 4, b = 7 }; bar b = f; Console.WriteLine(b.a); Console.ReadKey(); } …
nurchi
  • 770
  • 11
  • 24
1
vote
1 answer

Returning a reference to a const stuct's (pointer-type) member: apparent lvalue to rvalue conversion

Given the following code, GCC gives some unexpected errors & warnings. I'm trying to return a member of a struct by reference, and it's saying that I'm returning a temporary! Also, when attempting to fix this function, it complains of a value…
golvok
  • 1,015
  • 12
  • 25
1
vote
1 answer

Establishing a known 1:1 relationship between a type and a unique value

Given that i have a kind of enumeration using a sealed trait and case objects representing the values, is it possible to enforce a mechanism to retrieve the single unique value for a given type, without requiring an implicit argument? with implicits…
0__
  • 66,707
  • 21
  • 171
  • 266
1
vote
3 answers

Integer overflow vs implicit conversion from long long to int

Take for example int a=INT_MAX-1; and int b=INT_MAX-1; and assume that int is 32-bit and a function int product(int a,int b) { return a*b; } Now here the product a*b overflows resulting in undefined behavior from the standard: If an…
kingW3
  • 439
  • 10
  • 20
1
vote
0 answers

Where to put implicits and helper functions, traits vs companion objects

I first describe way of thinking, and then my questions. I use Functor definition as an example. It is defined in scalaz, so I use it as an example with small changes to make it less complex. Here is a Functor module definition, that I put in…
Alexandr
  • 9,213
  • 12
  • 62
  • 102
1
vote
2 answers

scala implicit conversion doesn't work

I discover a strange phenomenon: class A { val dual: A = this object T { implicit def doubleDual(t: T): dual.dual.T = t.asInstanceOf[dual.dual.T] implicit def justForTest(t: T): Int = 777 } class T } val a = new A val t = new a.T //…
蘇哲聖
  • 735
  • 9
  • 17