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
17
votes
1 answer

recommended way to convert Double -> Float in Haskell

What is the idiomatic way to go Double -> Float? Is it uncurry encodeFloat . decodeFloat ? (I am using gloss, this requires Floats) And what is the recommended way to find an answer to such questions? I was trying this hoogle query but the answers…
d8d0d65b3f7cf42
  • 2,597
  • 15
  • 28
17
votes
2 answers

Why does "one" < 2 equal FALSE in R?

I'm reading Hadley Wickham's Advanced R section on coercion, and I can't understand the result of this comparison: "one" < 2 # [1] FALSE I'm assuming that R coerces 2 to a character, but I don't understand why R returns FALSE instead of returning…
JoeF
  • 733
  • 1
  • 7
  • 21
15
votes
1 answer

Coercion in JavaScript

I was wondering a few things about coercion. When you do: 1 == true // true Which one is coerced into which one ? is it the left one or the right one ? When you do undefined == null // true How does it work exactly ? In which order does it try to…
Scipion
  • 11,449
  • 19
  • 74
  • 139
15
votes
2 answers

Exclude a value for a path parameter in React Router by type

I'm a bit stuck with the route component. Imagine I have this two Routes with their own path: /person/add should show a form where I…
Wannes
  • 1,019
  • 2
  • 11
  • 18
12
votes
1 answer

Force a narrow implicit coercion at compile time

I'm trying to define a struct which uses a variable with a restricted range of numbers, and implicit coercion from ints. I'd like to be able to force build errors if any constants or other hardcoded values are used with this struct. Here is an…
user3657661
  • 306
  • 3
  • 13
12
votes
2 answers

Exception handling and coercion

try { throw Derived(); } catch (Base&) { std::cout << "subtyping\n"; } try { throw "lol"; } catch (std::string) { std::cout << "coercion\n"; } Output: subtyping terminate called after throwing an instance of 'char const*' Why does…
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
11
votes
2 answers

Why GHC.Types.Any here?

I was doing some code golf in Haskell just now, and I ran into an error that didn't make much sense to me at the time. Decided to check it out in GHCi, and now I'm truly baffled. λ> :t replicate <$> readLn replicate <$> readLn :: IO (a -> [a]) λ> f…
11
votes
1 answer

Can I use map coercion in groovy to mock a class with a constructor that has parameters?

Java example classes under test public class Sample { public void printPie() { System.out.println("Pie."); } } public class SampleCont { String _PIE; public SampleCont() { _PIE = "pie"; } public void…
PopsiclePolly
11
votes
0 answers

Why does this deref coercion fail when the expression is wrapped in a block?

String implements Deref, which means that the following code compiles: fn save(who: &str) { println!("I'll save you, {}!", who); } save(&String::from("Madoka")); If I create a custom type that also implements Deref,…
Lambda Fairy
  • 13,814
  • 7
  • 42
  • 68
11
votes
3 answers

How do I make Binding respect DependencyProperty value coercion?

I have a control with a DependencyProperty with a CoerceValueCallback. This property is bound to a property on a model object. When setting the control property to a value that causes coercion the Binding pushes the uncoerced value to the model…
Jesper Larsen-Ledet
  • 6,625
  • 3
  • 30
  • 42
11
votes
3 answers

Coercing float into unsigned char on ARM vs. Intel

When I run the following C code on an Intel machine... float f = -512; unsigned char c; while ( f < 513 ) { c = f; printf( "%f -> %d\n", f, c ); f += 64; } ...the output is as follows: -512.000000 -> 0 -448.000000 -> 64 -384.000000 ->…
zmippie
  • 1,509
  • 2
  • 17
  • 22
11
votes
1 answer

Compare strings as numbers in SQLite3

I have the following query in SQLite: SELECT * FROM t1 ORDER BY t1.field Where t1.field is a text column containing numbers. Is it posible to force SQLite to consider the values of t1.field as numbers instead of strings (whithout doing ALTER…
diegogs
  • 2,036
  • 2
  • 16
  • 20
10
votes
2 answers

Why this behavior when coercing a list to character via as.character()?

In the process of (mostly) answering this question, I stumbled across something that I feel like I really should already have seen before. Let's say you've got a list: l <- list(a = 1:3, b = letters[1:3], c = runif(3)) Attempting to coerce l to…
joran
  • 169,992
  • 32
  • 429
  • 468
10
votes
3 answers

What is happening in this loose equality comparison of 2 empty arrays

I am struggling with understanding how this snippet works on a basic level if([] == ![]){ console.log("this evaluates to true"); } Please help me understand where I got it wrong. My thinking: First there is operator precedence so ! evaluates…
Konrad Albrecht
  • 1,701
  • 1
  • 9
  • 20
10
votes
2 answers

Forced conversion of non-numeric numpy arrays with NAN replacement

Consider the array x = np.array(['1', '2', 'a']) Tying to convert to a float array raises an exception x.astype(np.float) ValueError: could not convert string to float: a Does numpy provide any efficient way to coerce this into a numeric array,…
ChrisB
  • 4,628
  • 7
  • 29
  • 41
1
2
3
26 27