coercion

Rust Trait object conversion

拥有回忆 提交于 2019-11-28 13:59:33
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 operation does not require knowledge of the size of the structure that implements the trait, it only requires knowledge of the pointer itself and the type it is converting from and to, which it will have because &self is generic when implemented inside a trait. I think this may be an instance of the compiler enforcing unnecessary constraints and I've

Coercion in JavaScript

不打扰是莪最后的温柔 提交于 2019-11-28 09:08:39
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 convert it ? By instance: 1) String(undefined) == String(null) // false 2) Number(undefined) == Number(null) // false 3) Boolean(undefined) == Boolean(null) // true Does it first try to coerce the left side operand ? then the right ? then both ? EDIT: As explained in the comments: "not a duplicate. While both questions are about type coercion, this

Converting two columns of a data frame to a named vector

你离开我真会死。 提交于 2019-11-28 08:00:32
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 what I actually need would be: whatiwant = c("a" = "Alpha", "b" = "Beta", "c" = "Caesar", "d" = "Doris") Use the names function: whatyouwant <- as.character(dd$name) names(whatyouwant) <- dd$crit as.character is necessary, because data.frame and read.table turn characters into factors with default settings. If you want a one-liner: whatyouwant <- setNames(as.character(dd$name), dd

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

瘦欲@ 提交于 2019-11-28 07:34:49
问题 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 y) } prints number For the record: $ tsc --version Version 1.8.10 回答1: Type compatibility in TypeScript is based on structural subtyping, not nominal typing. That said, consider the two following interface definitions: interface IFoo { X: number } interface IBar {

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

ⅰ亾dé卋堺 提交于 2019-11-27 21:22:55
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, implicit and explicit casting work just fine: B b = a; B b2 = (B)a; ...so how come Linq's .Cast<> doesn't? A[] aa = new A[]{...}; var bb = aa.Cast<B>(); //throws InvalidCastException Looking at the source code for .Cast<> , there's not much magic going on: a few special cases if the parameter really is a IEnumerable<B> , and then: foreach (object obj in source)

Forced conversion of non-numeric numpy arrays with NAN replacement

你说的曾经没有我的故事 提交于 2019-11-27 15:11:55
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, replacing non-numeric values with something like NAN? Alternatively, is there an efficient numpy function equivalent to np.isnan , but which also tests for non-numeric elements like letters? You can convert an array of strings into an array of floats (with NaNs) using np.genfromtxt : In [83]: np.set_printoptions(precision=3, suppress=True) In [84]: np

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

☆樱花仙子☆ 提交于 2019-11-27 08:57:27
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 various types returns an error: > as.numeric(l) Error: (list) object cannot be coerced to type 'double' > as.logical(l) Error: (list) object cannot be coerced to type 'logical' However, I'm apparently allowed to coerce a list to character, I just wasn't expecting this result: > as.character(l) [1] "1:3" [2] "c(\"a\", \"b\", \"c\")" [3] "c(0

What is the difference between a slice and an array?

笑着哭i 提交于 2019-11-27 08:02:31
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 makes them tolerable. — Chris Morgan Why can &[T; n] coerce to &[T] ? In what other conditions does this coercion happen? Chris Morgan [T; n] is an array of length n , represented as n adjacent T instances. &[T; n] is purely a reference to that array, represented as a thin pointer to the data. [T] is a slice, an unsized type; it can only be used through some form of

Why does “one” < 2 equal FALSE in R?

馋奶兔 提交于 2019-11-27 05:24:47
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 an error. This is especially puzzling to me since -1 < "one" # TRUE So my question is two-fold: first, why this answer, and second, is there a way of seeing how R converts the individual elements within a logical vector like these examples? jdharrison From help("<") : If the two arguments are atomic vectors of different types, one is coerced to the

Coercion in JavaScript

让人想犯罪 __ 提交于 2019-11-27 02:45:51
问题 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 convert it ? By instance: 1) String(undefined) == String(null) // false 2) Number(undefined) == Number(null) // false 3) Boolean(undefined) == Boolean(null) // true Does it first try to coerce the left side operand ? then the right ? then both ? EDIT