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

Comparing POSIXlt object with date string the right thing?

I recently came across R code that compared a POSIXlt date object with a date string. as.POSIXlt.date("2007-02-02") >= "2007-02-01" [1] FALSE The result, surprisingly at least for me, was FALSE. I was expecting that the POSIXlt object would be…
farhanhubble
  • 476
  • 3
  • 18
5
votes
1 answer

numpy coercion problem for left-sided binary operator

I am implementing an array-like object that should be interoperable with standard numpy arrays. I just hit an annoying problem that narrows down to the following: class MyArray( object ): def __rmul__( self, other ): return MyArray() # value…
gertjan
  • 843
  • 1
  • 8
  • 16
5
votes
2 answers

Moose deep coercion - ArrayRef of user-defined types

What am I missing in the following subtype and coercion chain? I'd like to be able to coerce an arrayref of validated types or die from the following inputs: Coercible string Valid string Arrayref of mixed coercible and valid strings Assume that…
Oesor
  • 6,632
  • 2
  • 29
  • 56
5
votes
3 answers

Shopify - Convert Int to String in Liquid

How do you change the datatype from a number to a string in liquid? This could be used to create dynamic css classes within blocks. However, if it comes through as an integer it isn't recognized as a class. {{block.id}} // Integer I have answered…
shanestreator
  • 69
  • 1
  • 1
  • 7
5
votes
2 answers

How to set FXML "padding" property in one line

I would like to define a button's padding the same way as in C# XAML files. Instead of writing things like:
0009laH
  • 1,960
  • 13
  • 27
5
votes
0 answers

C++ "coercion constructor" cannot access private members

I'm trying to write some code that uses the "Coercion by Member Template" pattern in order to create a container that can hold pointers to multiple types and treat them polymorphically. I've run into an issue trying to write a "coercion…
tjwrona1992
  • 8,614
  • 8
  • 35
  • 98
5
votes
1 answer

Moose coercion and builders

This follows on from my previous question about Moose structured types. I apologise for the length of the question. I wanted to ensure that I included all the necessary details. MyApp::Type::Field defines a structured type. I use coercion to…
Mike
  • 21,301
  • 2
  • 42
  • 65
5
votes
1 answer

Why does accessing a field on a pointer to a struct work in rust?

I've noticed that, given a P, accessing fields of the SomeStruct directly on the pointer seems to work, and I'm not sure why that is. For example, this code compiles and works as expected (prints…
Marcus Buffett
  • 1,289
  • 1
  • 14
  • 32
5
votes
1 answer

coerce a function call into a string

I am trying to understand what is a call object in R and to coerce it to characters. However my efforts have been vain so far. myFun=function(a=1) { x=sys.call() return(as.character(x)) } x=myFun(a=2) # here I would like to get the string…
RockScience
  • 17,932
  • 26
  • 89
  • 125
5
votes
1 answer

Convert a vector of lists with uneven length to a matrix

I have a vector, well a data frame with only one column, that contains lists of uneven lengths: data = list( c(349, 364, 393, 356, 357, 394, 334, 394, 343, 365, 349), c(390, 336, 752, 377), c(670, 757, 405, 343, 1109, 350, 372), c(0,…
Marius
  • 990
  • 1
  • 14
  • 34
5
votes
3 answers

Unexpected behavior with random array generation

I was trying to generate an array of random numbers between 10 and 1000 in descending ordered. This is the code I wrote: function GenerateRandomArray(){ var array = []; for (var i = 0; i < 10; i++) { array[i] = Math.round(Math.random() *…
Stan Amsellem
  • 503
  • 1
  • 5
  • 16
5
votes
1 answer

Fixed points of representational bifunctors

Edward Kmett's experimental roles package offers various utilities for lifting coercions, some of which I've pasted at the end of this question. The key class in the package is class Representational (t :: k1 -> k2) where -- | An argument is…
dfeuer
  • 48,079
  • 5
  • 63
  • 167
5
votes
1 answer

R: Kaggle Titanic Dataset Random Forest NAs introduced by coercion

Im currently practicing R on the Kaggle using the titanic data set I am using the Random Forest Algorthim Below is the code fit <- randomForest(as.factor(Survived) ~ Pclass + Sex + Age_Bucket + Embarked + Age_Bucket + Fare_Bucket +…
John Smith
  • 2,448
  • 7
  • 54
  • 78
5
votes
1 answer

How do Python's string and unicode coercion/magic functions work?

I'm using Python version: 2.7.3. In Python, we use the magic methods __str__ and __unicode__ to define the behavior of str and unicode on our custom classes: >>> class A(object): def __str__(self): print 'Casting A to str' return…
Snakes and Coffee
  • 8,747
  • 4
  • 40
  • 60
5
votes
2 answers

show returning wrong value when used with unsafeCoerced value

I was experimenting with unsafeCoerce with Int8 and Word8, and I found some surprising behaviour (for me anyway). Word8 is a 8 bit unsigned number that ranges from 0-255. Int8 is a signed 8 bit number that ranges from -128..127. Since they are both…
David Miani
  • 14,518
  • 2
  • 47
  • 66