coercion

In Ruby, how does coerce() actually work?

∥☆過路亽.° 提交于 2019-12-28 02:27:06
问题 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 * 3 Output: #<Point:0x336094 @x=1, @y=2> #<Point:0x335fa4 @x=3, @y=6> but then, 3 * point is not understood: Point can't be coerced into Fixnum ( TypeError ) So we need to further define an instance method coerce : class Point def coerce(something) [self, something]

How to get Deref coercion when using impl Trait

我与影子孤独终老i 提交于 2019-12-24 10:23:37
问题 This function returns the first element of a list-like collection. It works for a variety of different list-like types: fn first<T: Copy>(x: impl Deref<Target=[T]>) -> T { x[0] } For example, this compiles and runs: let data: Vec<usize> = vec![3, 4]; assert_eq!(first(data), 3); let data: &[usize] = &[3, 4]; assert_eq!(first(data), 3); let data: Rc<[usize]> = Rc::new([3, 4]); assert_eq!(first(data), 3); This also compiles and runs: fn stub(x: &[usize]) -> usize { first(x) } let data: &[usize;

Symbol string coercion

懵懂的女人 提交于 2019-12-22 15:02:08
问题 Symbol() + '' throws TypeError: Cannot convert a Symbol value to a string While a known workaround is to use String(Symbol()) . This looks inconsistent with other primitives, including the ones that should almost never be coerced ( undefined and null ). How exactly does String differ from + '' (except it works)? Do specs explicitly specify that String should accept symbols? What were the motives to allow it in one way and disallow it in another? 回答1: How exactly does String differ from + ''

Can canonical structure resolution be interleaved with coercion insertion?

最后都变了- 提交于 2019-12-13 13:58:23
问题 In trying to solve (How) can I define partial coercions in Coq?, I discovered that canonical structure resolution is not interleaved with coercion insertion: Structure foo := { ty1 : Type ; ty2 : Type }. Canonical Structure default_foo ty := {| ty1 := option ty ; ty2 := ty |}. Definition Some_nat := @Some nat. Coercion Some_nat : nat >-> option. Check Some 0 : ty1 _. Check 0 : ty1 _. (* fails *) Is there a different way to invoke canonical structures or coercions such that they are

Parametrizing type with another type using Type::Tiny

自作多情 提交于 2019-12-12 14:24:39
问题 I want to create a type, based on the string, which will have upper length limit, and - optionally - lower length limit. I.e., parameterized type, where length range would be a parameter. What I want in my implementation: A separate type for string length range. Not using MooseX::Types::Parameterizable A sugar of parametrizing the type straight with arrayref, NOT hashref: This: isa=>Varchar[1, 15] Not this: isa=>Varchar[{min=>1, max=>15,}] That's what I have so far: File MyTypesTiny.pm

no method for coercing this S4 class to a vector for utilization of mclust

爷,独闯天下 提交于 2019-12-12 01:52:40
问题 I'm trying to use the mclust method on an .FCS format file (which is a flow cytometry format file) and I read this file into R as flowFrame object. install.packages("openCyto") # since the old version sefaulted my R session library( openCyto ) library( flowCore) library( mclust) trial1=read.FCS("export_Alcina TregMAIT_AV 10-1974 P1_CD4.fcs") a=as.matrix(trial1) Editors note: some of these are Bioconductor packages and you should install according to the help pages for that environment.

What is the difference between autoboxing and coercion? [closed]

拟墨画扇 提交于 2019-12-11 16:25:12
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 7 years ago . I program in Java, C and Python. The rule for automatic coercions among arithmetic types have been augmented to handle the richer set of types Source: "The C Programming Language" But what does "coercion" mean?

Why does implicit coercion for addition always produce a string?

霸气de小男生 提交于 2019-12-11 12:06:08
问题 If only the second operand in addition is a string then the output is a string: let a = 1 + '2'; console.log(typeof a); // string And if, instead, only the first operand is a string, then the output is still a string: let b = '1' + 2; console.log(typeof b); // string I was guessing that there would be some kind of argument precedence. Is there a reason why this mathematical function defaults to a non-numerical output with mixed-type arguments? 回答1: As it is often the case, the answer is

A simple question about type coercion in C++

扶醉桌前 提交于 2019-12-11 04:36:44
问题 Given a function prototype, and a type definition: int my_function(unsigned short x); typedef unsigned short blatherskite; Is the following situation defined by standard: int main(int argc, char** argv) { int result; blatherskite b; b=3; result = my_function(b); } Do I get type coercion predictably via the function prototype? 回答1: If your question is really about whether the types of the argument and the parameter match, then the answer is yes. typedef does not introduce a new type, it only

How to get Deref coercion when using impl Trait (take 2)

心已入冬 提交于 2019-12-11 03:36:14
问题 Here is a trait (simplified for the question) which I'd like to implement for every type that behaves like a slice: trait SliceLike { type Item; /// Computes and returns (owned) the first item in a collection. fn first_item(&self) -> Self::Item; } Note that the Item type is an associated type; I want each type that is SliceLike to have a unique element type. Here is an attempt at a blanket implementation: use std::ops::Deref; impl<T: Clone, U: Deref<Target = [T]>> SliceLike for U { type Item