coercion

Coercing template class with operator T* when passing as T* argument of a function template

Assume I have a function template like this: template<class T> inline void doStuff(T* arr) { // stuff that needs to use sizeof(T) } Then in another .h filee I have a template class Foo that has: public: operator T*() const; Now, I realize that those are different Ts. But If I have a variable Foo<Bar> f on the stack, the only way ...

How does JavaScript treat the ++ operator?

JavaScript does funky automatic conversions with objects: var o = {toString: function() {return "40"; }}; print(o + o); print((o+1)+o); print((o*2) + (+o)); will print: 4040 40140 120 This is because +, if any of the arguments are objects/strings, will try to convert all the arguments to strings then concatenate them. If all argume...

How to encourage implementation of TDD?

In a consulting company where the general thought of TDD and automated testing as a whole is appreciated by the programmers but thought of as "been there, done that, doesn't work" by management, how would you convince the powers that be to approve the investment of time and resources to implement such an approach? So, to be specific, th...

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 object. The property value on the control is coerced correctly. How do I get the Binding to ...

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 printPie() { System.out.println(_PIE); } } public class SampleContArg { String...

Nested Class Coercion Fail Between CFC and Flex

I have two AS3 classes, A and B. Class A contains an ArrayCollection that will hold multiple instances of class B. In ColdFusion, I have matching CFCs, with same-named variables in the same order, as well as cfproperty tags for each variable. The AS3 classes both use the RemoteClass metadata to associate them with their CFC counterpa...

Flash AS3: Getting this error: Access of undefined property - basic scope help needed :(

I'm still used to the AS2 style of all code on 1 frame, I'm trying to code AS3 in class files and I'm having a problem with a basic package setup. Scope issues are killing me with trying to learn AS3. Below is my package code, I don't have any other class files, just trying to return a simple trace. The error I'm getting after I run the...

Casting vs. coercion in Python

In the Python documentation and on mailing lists I see that values are sometimes "cast", and sometimes "coerced". What is the difference? ...

C++ user-defined conversion operators without classes?

In C++ is it possible to define conversion operators which are not class members? I know how to do that for regular operators (such as +), but not for conversion operators. Here is my use case: I work with a C Library which hands me out a PA_Unichar *, where the library defines PA_Unichar to be a 16-bit int. It is actually a string code...

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 matching ctor found for class java.lang.String (NO_SOURCE_FILE:0) [Thrown class clojure...

Python: Identifying a numeric string?

I tried a couple of approaches, I am really only concerned with performance, not correctness. I noticed that the regex based implementation is about 3-4x slower than the one that uses type coercion. Is there another, more efficient way of doing this? def IsNumber(x): try: _ = float(x) except ValueError: return Fa...

Bitstream to Float Type Coercion

I'm having trouble getting the following code to work correctly. Using an online IEEE-754 converter, I wrote out (by hand) to the testData.txt file that is read with the bit string that should signify the floating point number 75.5; the actual cout.write does show that the bit string is as I expect as well. However, when I try to coerc...

coercing nil into a number

What is happening here?? irb(main):001:0> a= nil => nil irb(main):002:0> b = nil => nil irb(main):003:0> a => nil irb(main):004:0> a+b NoMethodError: undefined method `+' for nil:NilClass from (irb):4 from :0 irb(main):005:0> if a.nil? or b.nil?; a,b=0;end; irb(main):006:0* c = a+b TypeError: nil can't be coerced into Fixnum ...

Is it possible to coerce string values in xml to bool?

Let's suppose I have xml like this one: <Server Active="No"> <Url>http://some.url&lt;/Url&gt; </Server> C# class looks like this: public class Server { [XmlAttribute()] public string Active { get; set; } public string Url { get; set; } } Is it possible to change Active property to type bool and have XmlSerializer coer...

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, so I have cobbled together something using other methods in that library: use strict; use...

F# return type coercion

Hi, In F# I have a function that returns System.Linq.Expression instances: and System.Object with member this.ToExpression() = match this with | :? System.Int32 -> Expression.Constant(this) :> Expression | :? System.Boolean -> Expression.Constant(this) :> Expression | :? Tml.Runtime.Seq as s -> s.ToExpression() |...

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 * 3 Output: #<Point:0x336094 @x=1, @y=2> #<Point:0x335fa4 @x=3, @y=6> but then, 3...

In Ruby, can the coerce() method know what operator it is that requires the help to coerce?

In Ruby, it seems that a lot of coerce() help can be done by def coerce(something) [self, something] end that's is, when 3 + rational is needed, Fixnum 3 doesn't know how to handle adding a Rational, so it asks Rational#coerce for help by calling rational.coerce(3), and this coerce instance method will tell the caller: # I know ...

A simple question about type coercion in C++

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 prototy...

WPF Dependency Property Coercion binding issues

I have both VS2008 and VS2010 installed, and I see a very strange behavior In VS2008, I have a simple WPF app: <TextBox x:Name="textbox" Text="{Binding Path=MyProperty,Mode=TwoWay}"></TextBox> and public Window1() { InitializeComponent(); DataContext = this; } public string MyProperty { get { return (string)GetValue(MyP...