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

Why does Rust not perform implicit deref coercion in match patterns?

After reading the section in the Rust book on Smart Pointers and Interior mutability, I tried, as a personal exercise, to write a function that would traverse a linked list of smart pointers and return the "last" element in the list: #[derive(Debug,…
Billy
  • 5,179
  • 2
  • 27
  • 53
9
votes
2 answers

Cannot 'coerce' data type with 'Reader' as a field

I have the following Haskell code, that compiles perfectly: import Control.Monad.Reader (Reader (..)) import Data.Coerce (Coercible, coerce) data Flow i o = Flow (i -> o) (o -> i) coerceFlow :: (Coercible i i', Coercible o o') => Flow i o …
Shersh
  • 9,019
  • 3
  • 33
  • 61
9
votes
1 answer

Can you override the truthy coercion of a JavaScript Object?

The string coercion can be overwritten using the toString function. The number coercion can be overwritten using the valueOf function. The boolean coercion can be also overwritten using the valueOf function. var foo = { toString: function() { …
nick zoum
  • 7,216
  • 7
  • 36
  • 80
9
votes
1 answer

drop rows with errors for pandas data coercion

I have a dataframe, for which I need to convert columns to floats and ints, that has bad rows, ie., values that are in a column that should be a float or an integer are instead string values. If I use df.bad.astype(float), I get an error, this is…
Gijs
  • 10,346
  • 5
  • 27
  • 38
8
votes
1 answer

Perl6 string coercion operator ~ doesn't like leading zeros

I'm toying with Rakudo Star 2015.09. If I try to stringify an integer with a leading zero, the compiler issues a warning: > say (~01234).WHAT Potential difficulties: Leading 0 does not indicate octal in Perl 6. Please use 0o123 if you mean…
Zaid
  • 36,680
  • 16
  • 86
  • 155
8
votes
4 answers

Why is it not possible to convert "1.7" to integer directly, without converting to float first?

When I type int("1.7") Python returns error (specifically, ValueError). I know that I can convert it to integer by int(float("1.7")). I would like to know why the first method returns error.
8
votes
1 answer

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: and public Window1() { InitializeComponent(); …
Nitin Chaudhari
  • 1,497
  • 16
  • 39
8
votes
1 answer

Why does 1..99,999 == "1".."99,999" in R, but 100,000 != "100,000"?

In the console, go ahead and try > sum(sapply(1:99999, function(x) { x != as.character(x) })) 0 For all of values 1 through 99999, "1" == 1, "2" == 2, ..., 99999 == "99999" are TRUE. However, > 100000 == "100000" FALSE Why does R have this quirky…
Robert Krzyzanowski
  • 9,294
  • 28
  • 24
8
votes
4 answers

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…
Jean-Denis Muys
  • 6,772
  • 7
  • 45
  • 71
7
votes
1 answer

Why can't hyperfunctions be coerced in GHC?

I have the following type, which is based on the paper Coroutining folds with hyperfunctions: newtype Hyper a b = Hyper { invoke :: Hyper b a -> b } It's contravariant in its first argument and covariant in its second, so it's a…
Zemyla
  • 468
  • 3
  • 7
7
votes
2 answers

Why do I have to coerce this data type by fields, rather than all at once?

I have two types (<->) and (<-->) representing isomorphisms between types: data Iso (m :: k -> k -> *) a b = Iso { to :: m a b, from :: m b a } type (<->) = Iso (->) infix 0 <-> data (<-->) a b = Iso' { to' :: a -> b, from' :: b -> a } infix 0…
rampion
  • 87,131
  • 49
  • 199
  • 315
7
votes
1 answer

Is it possible to remove/override an existing coercion?

I have imported a Coq module which defines a coercion, but it does not fit my needs. Is there any way to remove or (locally) override it? To be specific, say the module I imported defines a coercion Coercion bool_to_nat (b:bool) := match b with…
Tony Beta Lambda
  • 529
  • 3
  • 18
7
votes
1 answer

Converting match object to string in perl6

I was trying to convert a match object to a string in perl6. The method Str on a match object is defined as: method Str(Match:D: --> Str:D) I would think I could use Str($match) to accomplish this. And it seems to convert it to a string, but I'm…
MorayJ
  • 537
  • 2
  • 8
7
votes
2 answers

Type coercion in Perl6

If I have an an object of type Str, and I want to coerce it into an Int, it is my understanding that I can do this by calling the method Int on the Str-object, like so: "100".Int I (think I) know I can do this because the Str-type documentation at…
ozzy
  • 785
  • 3
  • 12
7
votes
1 answer

Why does reference weakening from &mut occur in some trait method calls?

One of the few implicit conversions available in Rust is pointer weakening, which can turn a &mut T into a &T: fn just_foo(_: &T) {} just_foo(&mut vec![1, 2, 3]); However, this doesn't happen when matching traits. For instance, although the +…
E_net4
  • 27,810
  • 13
  • 101
  • 139
1 2
3
26 27