Questions tagged [coercion]

Coercion, type conversion and typecasting are different ways of, implicitly or explicitly, changing an entity of one data type into another.

Coercion, type conversion and typecasting are different ways of, implicitly or explicitly, changing an entity of one data type into another.

398 questions
7
votes
1 answer

How does Scala convert Int to Double?

val d: Double = 42 When I try to find implicit conversion via intellij, nothing interesting comes up. Also, Int isn't a subtype of Double. So how does Scala do it?
allidoiswin
  • 2,543
  • 1
  • 20
  • 23
7
votes
1 answer

Why doesn't a nested reference to an array coerce to a slice?

I read What are Rust's exact auto-dereferencing rules? from beginning to end, but I still have a question about the coercion from array to slice. Let us think about the following code: let arr: &[i32; 5] = &&&[1, 2, 3, 4, 5]; // let arr: &[i32] =…
7
votes
1 answer

When is double coercion useful?

I stumbled upon the following compilation message in OCaml: This simple coercion was not fully general. Consider using a double coercion. It happened in a fairly complicated source code, but here is a MNWE: open Eliom_content.Html.D let f_link s…
Fabian Pijcke
  • 2,920
  • 25
  • 29
7
votes
3 answers

What is going on with R coercing "TRUE" string to TRUE logical?

So, I discovered this: > TRUE == "TRUE" [1] TRUE > TRUE == "BOO" [1] FALSE > TRUE == "true" [1] FALSE > TRUE == "T" [1] FALSE > FALSE == "FALSE" [1] TRUE > FALSE == "F" [1] FALSE According to the R documentation for logical {base}: as.logical…
turtlegraphics
  • 296
  • 3
  • 8
7
votes
1 answer

TypeScript: why is a number assignable to a reference of type Object?

Why is this legal TypeScript? var x: number = 5 var y: Object = x Surely a number is not an Object. One might suspect that x is implicitly coerced (auto-boxed) to an object, but no: if (!(y instanceof Object)) { console.log(typeof…
Roly
  • 2,126
  • 2
  • 20
  • 34
7
votes
1 answer

Unboxing uint/int without knowing what's inside the box

I have an object o that is known to be a boxed int or uint: object o = int.MinValue object o = (uint)int.MinValue // same bytes as above I don't know what's in the box, all I care about is that there's 4 bytes in there that I want to coerce to an…
Johannes Rudolph
  • 35,298
  • 14
  • 114
  • 172
7
votes
2 answers

Why does Pandas coerce my numpy float32 to float64?

Why does Pandas coerce my numpy float32 to float64 in this piece of code: >>> import pandas as pd >>> import numpy as np >>> df = pd.DataFrame([[1, 2, 'a'], [3, 4, 'b']], dtype=np.float32) >>> A = df.ix[:, 0:1].values >>> df.ix[:, 0:1] = A >>>…
Finn Årup Nielsen
  • 6,130
  • 1
  • 33
  • 43
6
votes
1 answer

Why is GHC contradicting itself when using a Coercible constraint?

Why is GHC inferring unification from coercibility of associated data, and why is it contradicting its own checked type signature to do so? The problem {-# LANGUAGE ExplicitForAll #-} {-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE ConstraintKinds…
Isaac van Bakel
  • 1,772
  • 10
  • 22
6
votes
3 answers

Why Rust can't coerce mutable reference to immutable reference in a type constructor?

It is possible to coerce &mut T into &T but it doesn't work if the type mismatch happens within a type constructor. playground use ndarray::*; // 0.13.0 fn print(a: &ArrayView1) { println!("{:?}", a); } pub fn test() { let mut x =…
pkubik
  • 780
  • 6
  • 19
6
votes
1 answer

What are the rules involved in coercing dates with the c() function

As I understand when the objects being concatenated with the c(...) function are of different types they are coerced into a single type which is the type of the output object According the the R documentation The output type is determined from the…
6
votes
1 answer

Coercible and existential

data T t where A :: Show (t a) => t a -> T t B :: Coercible Int (t a) => t a -> T t f :: T t -> String f (A t) = show t g :: T t -> Int g (B t) = coerce t Why does f compile but g generate an error like follows? I'm using GHC 8.4. • Couldn't…
NioBium
  • 583
  • 3
  • 10
6
votes
1 answer

Why can I pass a value-accepting callable to a reference-accepting std::function?

When I declare a variable function, the compiler still lets me assign a lambda that accepts a value: function handler; handler = [](Foo f){}; (cfr. also http://cpp.sh/5dsp) So when the handler is called, a copy…
xtofl
  • 40,723
  • 12
  • 105
  • 192
6
votes
2 answers

Rust Trait object conversion

The following code won't compile due to two instances of this error: error[E0277]: the trait bound Self: std::marker::Sized is not satisfied I don't understand why Sized is required in this instance as both &self and &Any are pointers and the…
Jake Kiesel
  • 341
  • 4
  • 10
6
votes
1 answer

Disable Haskell type coercion

I have example based on hyperloglog. I'm trying to parametrize my Container with size, and use reflection to use this parameter in functions on containers. import Data.Proxy import Data.Reflection newtype Container p = Container…
Eugene Zhulenev
  • 9,714
  • 2
  • 30
  • 40
6
votes
1 answer

How can I determine if an object or reference has a valid string coercion?

I've run into a situation (while logging various data changes) where I need to determine if a reference has a valid string coercion (e.g. can properly be printed into a log or stored in a database). There isn't anything in Scalar::Util to do this,…
Ether
  • 53,118
  • 13
  • 86
  • 159