Questions tagged [type-coercion]

In weakly typed languages, type coercion is where a data type is converted by the interpreter from one type to another (such as a string to an integer). This most often occurs in comparing two values. Use for questions where type coercion is an issue with your code.

Type coercion occurs in weakly typed languages (like or ). Consider this PHP code

$x = '1'; // string
$y = $x + 1;
echo $y;

$x is a string, but we call it like an integer when setting $y. So the PHP engine coerces '1' (which is a string data type) into an integer for the purposes of math. As such, the script echoes 2 because the string 1 can be coerced neatly into integer 1. But not everything is so neatly handled

$x = 'a';
$y = $x + 1;
var_dump($y);

Now, 'a' is not readily coerced into a number. As such, we get back integer(1) because PHP coerces strings to 0 (modern PHP emits a E_WARNING that this is happening).

This most often rears itself in doing comparisons. Consider this code

$x = 'Something';
echo (strpos($x, 'Some') != false) ? 'Found it!' : 'Did not find it';

This script doesn't run like we expect. It tells us it didn't find it when we know it should have found it. The problem is that strpos can return a boolean false or the position of where it found the match. In this case, the match was at position 0. Our test then becomes

if( 0 == false )

Type coercion nails us here because 0 is coerced into false in PHP. PHP and Javascript both have a workaround for this, however. We can use === and !== to tell the interpreter to not coerce before comparing. If we tell PHP not to coerce the 0, we get back the results we expect

echo (strpos($x, 'Some') !== false) ? 'Found it!' : 'Did not find it';
108 questions
61
votes
2 answers

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 *…
nonopolarity
  • 130,775
  • 117
  • 415
  • 675
54
votes
7 answers

How to flatten a list to a list without coercion?

I am trying to achieve the functionality similar to unlist, with the exception that types are not coerced to a vector, but the list with preserved types is returned instead. For instance: flatten(list(NA, list("TRUE", list(FALSE), 0L)) should…
eold
  • 5,596
  • 11
  • 51
  • 72
13
votes
2 answers

Why does (int 10) produce a Long instance?

Why does (int 10) not produce an instance of type java.lang.Integer? ; why Long here? => (type (int 10)) ; java.lang.Long ; this one is also Long, why not java.lang.Number? => (type (num 10)) ; java.lang.Long => (type (double 10)) ;…
Eric Schoonover
  • 44,080
  • 43
  • 148
  • 200
13
votes
4 answers

How does PHP compare strings with comparison operators?

I'm comparing strings with comparison operators. I needs some short of explanations for the below two comparisons and their result. if('ai' > 'i') { echo 'Yes'; } else { echo 'No'; } output: No Why do these output this way? if('ia' >…
Shakti Singh
  • 77,873
  • 18
  • 129
  • 147
9
votes
3 answers

Create String list in Groovy

The following code in Groovy adds GStrings to the list: List args = [ 'cmd', "-Dopt=${value}" ] When I create a ProcessBuilder with this list, I get a ClassCastException. What's a groovy way to coerce the list elements to the correct type?
Aaron Digulla
  • 297,790
  • 101
  • 558
  • 777
8
votes
2 answers

Equality comparison between Date and number doesn't work

According to the ECMA script standard, the following code should return true, but it doesn't: d = new Date() ; d.setTime(1436497200000) ; alert( d == 1436497200000 ) ; Section 11.9.3 says: If Type(x) is either String or Number and Type(y) is…
GetFree
  • 34,030
  • 17
  • 70
  • 101
6
votes
2 answers

Understanding JavaScript hoisting and truthy & falsy

I've been reading about JavaScript hoisting sometime back. JavaScript Scoping and Hoisting by Ben Cherry Two words about “hoisting” by Dmitry Soshnikov and, some more about JavaScript type-coercion, truth & false test: Truth, Equality and…
manikanta
  • 7,066
  • 5
  • 52
  • 61
5
votes
4 answers

Why is `'\t\n ' == false` in JavaScript?

In JavaScript... '\t\n ' == false // true I can assume that any string that consists solely of whitespace characters is considered equal to false in JavaScript. According to this article, I figured that false would be converted to 0, but was unable…
alex
  • 438,662
  • 188
  • 837
  • 957
5
votes
5 answers

Understanding type coercion in JavaScript

I know == operator performs the type coercion. But I can't understand the below behaviour. const x = new Boolean(false); if (x) { console.log("if(x) is true"); } if (x == false) { console.log("if(x == false) is…
Varinder Singh
  • 1,530
  • 1
  • 12
  • 23
5
votes
1 answer

Is there any way to disable automatic type coercion in SQL Server 2005/2008, from varchar to int (as an example)?

If I have the following SQL: INSERT INTO sometable (someintcolumn) VALUES ('1') This will succeed, inserting the value 1. Is there any way I can ask SQL Server to please don't convert the types for me, permanently or temporarily, and instead fail…
Lasse V. Karlsen
  • 350,178
  • 94
  • 582
  • 779
5
votes
1 answer

Why do some non-empty strings evaluate to "false" in JavaScript?

According to this table in the ECMAScript standard, string values that have length 0 should be evaluated as boolean false. How come, then, these statements evaluate to true? "\t" == false " " == false "\n" == false " " == false All those…
Matt
  • 19,570
  • 12
  • 62
  • 104
5
votes
3 answers

How to coerce type of ActiveRecord attribute returned by :select phrase on joined table?

Having trouble with AR 2.3.5, e.g.: users = User.all( :select => "u.id, c.user_id", :from => "users u, connections c", :conditions => ... ) Returns, e.g.: => [#] >> users.first.attributes => {"id"=>1000,…
tribalvibes
  • 2,027
  • 3
  • 25
  • 29
4
votes
1 answer

Is it true that for all types `T`, `U` if `T` is coerced to `U` then `&T` is coerced to `&U`?

It's well documented that [T; n] can coerce to [T]. The following code is also well-formed: fn test(){ let _a: &[i32] = &[1, 2, 3]; } Here we have that &[T; n] is coerced to &[T]. Is it true that for all types T, U if T is coerced to U then &T…
Some Name
  • 6,872
  • 4
  • 9
  • 32
4
votes
3 answers

implicit coercion in javascript

I am revisiting implicit coercion in javascript and realized that I have overlooked something and need clarification on it. if var a = "5"; var b = 5; and a==b will return true. But there are two possible ways a==b could give true coercion right?…
tnkh
  • 1,142
  • 1
  • 7
  • 23
4
votes
4 answers

If [] is [] and Array.prototype is [] why doesn't ([] == Array.prototype)

I'm messing around in console and saw the following: >>> [] [] >>> Array.prototype [] >>> [] == Array.prototype false >>> [] === Array.prototype false Can anyone explain this behavior? (Sounds like a good candidate for wtfjs)
qwertymk
  • 30,576
  • 24
  • 107
  • 179
1
2 3 4 5 6 7 8