Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is "int + string" possible in statically-typed C# but not in dynamically-typed Python?

Tags:

python

c#

types

While studying C# I found it really strange, that dynamically typed Python will rise an error in the following code:

i = 5
print i + " "

whereas statically typed C# will normally proceed the similar code:

int i = 5;
Console.Write(i + " ");

I would expect other way around (in python I would be able to do this without any casting, but C# would require me to cast int to string or string to int).

Just to highlight, I am not asking what language is better, I am curious what was the reason behind implementing the language this way.

like image 378
Salvador Dali Avatar asked Oct 23 '13 21:10

Salvador Dali


People also ask

Why C is called statically typed language?

A statically-typed language is a language (such as Java, C, or C++) where variable types are known at compile time. In most of these languages, types must be expressly indicated by the programmer; in other cases (such as OCaml), type inference allows the programmer to not indicate their variable types.

Is C statically typed or dynamically typed?

Statically Typed Languages Some common examples of programming languages that belong to this category are Java, Haskell, C, C++, C#, Scala, Kotlin, Fortran, Go, Pascal, and Swift.

Why is Julia dynamically typed?

Julia is squarely in the dynamic camp: types are a property of values not expressions. The result type of code is determined by how values flow through it when it executes; the language does not include any rules for assigning types to expressions before executing them.

Which is better statically typed or dynamically typed?

Statically typed languages have better performance at run-time intrinsically due to not needing to check types dynamically while executing (it checks before running). Similarly, compiled languages are faster at run time as the code has already been translated instead of needing to "interpret"/translate it on the fly.


2 Answers

You are right that something is odd here. What is odd is that + means string concatenation in either language! Whether + can concatenate an int onto a string or not is a small point compared with the oddity that is + meaning "concatenate" at all.

I do not know which language first allowed + to be used to mean string concatenation, but that misfeature has been repeated over and over again so often that now we typically don't even notice how truly strange it is. Let's list some of the properties of normal addition and see if strings measure up.

  • Addition is commutative, but string concatenation is not. x + y != y + x
  • Addition is associative, but string concatenation in C# is not. (x + y) + z need not equal x + (y + z) if, say, y and z are integers.
  • Addition is paired with an inverse operation, subtraction, with the property that if x + y is z then z - y is x. There is no such operation for strings.
  • Addition has a left and right identity, and so does string concatenation. (The empty string).

One out of four ain't good. String concatenation is very unlike addition, so why should they share an operator?

I don't say this often but C got it right. The string concatenation operator should be just that: concatenation. In C "X" "Y" means "XY". If you put two string literals beside each other, they concatenate into a third.

like image 154
Eric Lippert Avatar answered Sep 20 '22 23:09

Eric Lippert


This isn't really a static/dynamic question but rather a matter of language design philosophy.

In Python, + is defined to be addition for numbers and concatenation for strings. This is perfectly reasonable behavior if you have even a little programming experience.

But when you have one of each, what happens? Does it try to convert the string to a number, or does it convert the number to a string? Either is, again, perfectly reasonable behavior to anyone who has done any programming at all, but since different languages have different rules about how this happens, you may make assumptions different from the ones someone else would make depending on which languages you have experience with already.

Python has as one of its guiding principles "explicit is better than implicit" (import this) and so it makes you explicitly state which behavior you want. How? By converting either the string or the number to the desired type, of course. Then they are both strings or both numbers and the behavior is obvious.

The resulting code is easier to read (even if you don't know much Python), because you don't have to guess what it's going to do.

like image 27
kindall Avatar answered Sep 20 '22 23:09

kindall