DEV Community

Cover image for Weird Type Coercions
kamaaaal
kamaaaal

Posted on

Weird Type Coercions

Hey Folks, I've been using multiple programming languages recently and I noticed the different behavior of type coercion in different languages.

JavaScript

We all know how weird is JavaScript so I'm not going to explain JavaScript type coercion lot.
but here is a simple snippet

"5" + 5 // results "55"
5 + []  // results "5"
Enter fullscreen mode Exit fullscreen mode

here the number and Array[] are implicitly converted to a string,
so the behaviour is it does either addition or concatenation at certain operands.

Python and Typescript

Python and Typescript seems to be strict. they don't allow type coercion we need to explicitly convert them to a certain type to do addition or string concatenation.

Python

5 + int("5") # results 10, does addition. 
"5" + str(5) # results "55", does concatenation
5 + "55"     # throws TypeError
Enter fullscreen mode Exit fullscreen mode

Typescript

5 + number("5") // results 10, does addition. 
"5" + string(5) // results "55", does concatenation
Enter fullscreen mode Exit fullscreen mode

The above examples avoid unexpected behavior. but it may feel odd for others at first who had lot of experience with other languages.

CSharp / .NET

I've been learning asp.net Framework (2014) for my college semester. That's a sad story university never minds updating the curriculum.

back to CSharp, when I was writing an if else
statement checking for an empty string gave me an error saying that "Cannot implicitly convert type 'string' to 'bool' "

string emptyStr = "";

if (emptyStr){ //error : Cannot implicitly convert type 'string' to 'bool'  
   Console.WriteLine(emptyStr);
}
Enter fullscreen mode Exit fullscreen mode

and i'm sorry for not including boilerplate "public static void Main" šŸ„ŗ.

I didn't expect this because for a long time JavaScript, typescript and python were evaluating zeros(0) and empty string("") to be falsy. Then I realized that I didn't touch other typed languages for a long time šŸ˜….

after this tried the same example in java got the same error. But the c++ evaluated empty string(char[]) as true because every pointer is evaluated to truthy value I guess. however, std::string throwed an conversion error.

After that I had a strong belief that Csharp is one of a safest strictly-typed language. That didn't last long.

Even though CSharp being strict on implicit conversion from converting string to bool.
It didn't care converting an int to string implicitly when using + operator

 string str = "no.";
 int num = 10;
 Console.WriteLine(num+str); // results no.10
Enter fullscreen mode Exit fullscreen mode

Php

I'm currently doing a project in php for my friend. That's the reason made me to write this post.

Php has a dedicated operator for string concatenation which is ".". having a concatenation operator, it isn't a surprise that it implicitly converts an integer to string.

echo "Hello "."World"; // results "Hello World"
echo "Hello ". 10 ;    // results "Hello 10"
Enter fullscreen mode Exit fullscreen mode

So, because of having a concatenation operator it doesn't allow "+" to do concatenation.

echo 10 + 10 ; // results 20
echo "Hello "+"World"; // Unsupported operand types: string + string
Enter fullscreen mode Exit fullscreen mode

This seems to be somehow same behavior like other languages,but the next example isn't

$str1 = "10";
$str2 = "20";
echo $str1 + $str2; // results 20 
Enter fullscreen mode Exit fullscreen mode

Even though both the operand being string here, both of them are converted to integer because of the operator "+".

Without knowing this behavior, I tried to concatenate two strings containing a number šŸ™‚.

I think I can't say it's a bad behavior , because it has a dedicated operator for concatenation, but still a newbie developer like me who have been practicing other languages for some time it seemed weird.

Thank you for reading this. I apologize for any grammatical errors in my writing.

Top comments (0)