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
95
votes
6 answers

Converting two columns of a data frame to a named vector

I need to convert a multi-row two-column data.frame to a named character vector. My data.frame would be something like: dd = data.frame(crit = c("a","b","c","d"), name = c("Alpha", "Beta", "Caesar", "Doris") ) and…
Stefan F
  • 2,573
  • 1
  • 17
  • 19
67
votes
2 answers

Convert factor to integer

I am manipulating a data frame using the reshape package. When using the melt function, it factorizes my value column, which is a problem because a subset of those values are integers that I want to be able to perform operations on. Does anyone know…
Jeff Erickson
  • 3,783
  • 8
  • 36
  • 43
65
votes
3 answers

How can I convert a LazySeq of Characters to a String in Clojure?

Let's say I have a LazySeq of java.lang.Character like (\b \ \! \/ \b \ \% \1 \9 \/ \. \i \% \$ \i \space \^@) How can I convert this to a String? I've tried the obvious (String. my-char-seq) but it throws java.lang.IllegalArgumentException: No…
Robert Campbell
  • 6,848
  • 12
  • 63
  • 93
62
votes
2 answers

In Ruby, how does coerce() actually work?

It is said that when we have a class Point and knows how to perform point * 3 like the following: class Point def initialize(x,y) @x, @y = x, y end def *(c) Point.new(@x * c, @y * c) end end point = Point.new(1,2) p point p point *…
nonopolarity
  • 146,324
  • 131
  • 460
  • 740
46
votes
3 answers

What is the difference between a slice and an array?

Why are both &[u8] and &[u8; 3] ok in this example? fn main() { let x: &[u8] = &[1u8, 2, 3]; println!("{:?}", x); let y: &[u8; 3] = &[1u8, 2, 3]; println!("{:?}", y); } The fact that &[T; n] can coerce to &[T] is the aspect that…
runrioter
  • 533
  • 1
  • 5
  • 11
40
votes
3 answers

How is 1 == [1] in javascript?

Recently I have been asked this question in an interview. var a = 1; var b = [1]; What will a == b; return. When I checked that on my chrome browser console I got this. var a = 1; var b = [1]; a == b; true I have also checked var a = 1; var b…
hmachahary
  • 474
  • 4
  • 12
37
votes
4 answers

Is there a JavaScript idiom to change "undefined" to "null"?

There's quite a few JavaScript idioms that coerce between types and similar things. ! can convert anything falsey to boolean true, !! can convert anything falsey to actual boolean false, + can convert true, false, or a string representing a number…
hippietrail
  • 15,848
  • 18
  • 99
  • 158
36
votes
8 answers

Are there cases where a typedef is absolutely necessary?

Consider the following excerpt from the safe bool idiom: typedef void (Testable::*bool_type)() const; operator bool_type() const; Is it possible to declare the conversion function without the typedef? The following does not compile: operator (void…
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
35
votes
2 answers

What's the difference between casting and coercion in Python?

In the Python documentation and on mailing lists I see that values are sometimes "cast", and sometimes "coerced".
Justin R.
  • 23,435
  • 23
  • 108
  • 157
25
votes
3 answers

Scala 2.10, its impact on JSON libraries and case class validation/creation

In Scala 2.10 apparently we're getting improved reflection. How will this impact lift-json, jerkson, sjson and friends? Furthermore, can we expect in the not too distant future a built-in JSON language feature a la Groovy's excellent GSON in…
virtualeyes
  • 11,147
  • 6
  • 56
  • 91
20
votes
3 answers

Why does the Linq Cast<> helper not work with the implicit cast operator?

Please read to the end before deciding of voting as duplicate... I have a type that implements an implicit cast operator to another type: class A { private B b; public static implicit operator B(A a) { return a.b; } } class B { } Now,…
Cristian Diaconescu
  • 34,633
  • 32
  • 143
  • 233
19
votes
1 answer

What does it mean to rely on the consistency of a coercion language?

From https://ghc.haskell.org/trac/ghc/wiki/DependentHaskell, unlike Coq and Agda, Haskell relies on the consistency of a coercion language, which is not threatened by * :: *. See the paper for more details. The "paper" cited is a broken link, but,…
MaiaVictor
  • 51,090
  • 44
  • 144
  • 286
19
votes
1 answer

sprintf invalid format '%d'

This works: > sprintf('%d', c(1, 1.5)) [1] "1" "1" and this doesn't: > sprintf('%d', c(1.5, 1)) Error in sprintf("%d", c(1.5, 1)) : invalid format '%d'; use format %f, %e, %g or %a for numeric objects Why?
pomber
  • 23,132
  • 10
  • 81
  • 94
17
votes
5 answers

Prevent coercion of pandas data frames while indexing and inserting rows

I'm working with individual rows of pandas data frames, but I'm stumbling over coercion issues while indexing and inserting rows. Pandas seems to always want to coerce from a mixed int/float to all-float types, and I can't see any obvious controls…
Mike T
  • 41,085
  • 18
  • 152
  • 203
17
votes
2 answers

Array Types In Powershell - System.Object[] vs. arrays with specific types

Why does caling GetType().Name on an array of strings return Object[] and not String[]? This seems to happen with any element type, for example Import-Csv will give you an Object[] but each element is a PSCustomObject. Here's an example with an…
David Klempfner
  • 8,700
  • 20
  • 73
  • 153
1
2 3
26 27