views:

541

answers:

7

What makes a language strongly typed? I'm looking for the most important aspects of a strongly typed language.

Yesterday I asked if PowerShell was strongly typed, but no one could agree on the definition of "strongly-typed", so I'm looking to clarify the definition.

Feel free to link to wikipedia or other sources, but don't just cut and paste for your answer.

+1  A: 

Strongly typed means you declare your variables of a certain type, and your compiler will throw a hissy fit if you try to convert that variable to another type without casting it.

Example (in java mind you):

int i = 4;
char s = i; // Type mismatch: cannot convert from int to char
Hans Sjunnesson
It we be the "compiler" only in case of static typing. In case of strong dynamic typing it will be the runtime who will throw the exception/error/whatever. See also http://www.artima.com/weblogs/viewpost.jsp?thread=7590
Suma
This is wrong. Whether you declare a type or not has nothing to do with strong typing, that is a question of explicit vs. implicit typing. Also, whether the compiler or the runtime catches type errors is a question of static vs. dynamic typing ans has again nothing to do with strong typing.
Jörg W Mittag
This is static typing.
leppie
+6  A: 

The key is to remember that there is a distinction between statically typed and strongly typed. A strongly typed language simply means that once assigned, a given variable will always behave as a certain type until it is reassigned. By definition statically typed languages like Java and C# are strongly typed, but so are many popular dynamic languages like Ruby and Python.

So in a strongly typed language

x = "5"

x will always be a string and will never be an integer.

In certain weakly typed languages you could do something like

x = "5"
y = x + 3
// y is now 8
Mike Deck
C could be said to be statically, weakly typed. It's weird, but possible.
troelskn
In C everything is a number.
leppie
+4  A: 

I heard someone say in an interview (I think it was Anders Hejlsberg of C# and turbo pascal fame) that strong typing is not something that's on or off, some languages have a stronger type system than others.

There's also a lot of confusion between strongly, weakly, static and dynamic typing where staticly typed languages assign types to variables and dynamic languages give types to the objects stored in variables.

Try wikipedia for more info but don't expect a conclusive answer: http://en.wikipedia.org/wiki/Strongly_typed_language

Mendelt
+4  A: 

People are confusing statically typed with strongly typed. Statically typed means "A string is a string is a string". Strongly typed means "Once you make this a string it will be treated as a string until it is reassigned as something different."

edit: I see someone else did point this out after all :)

MattC
+6  A: 

According to B.C. Pierce, the guy who wrote "Types and Programming Languages and Advanced Types and Programming Languages" :

I spent a few weeks trying to sort out the terminology of "strongly typed," "statically typed," "safe," etc., and found it amazingly difficult... The usage of these terms is so various as to render them almost useless.

So no wonder why your collegues disagree.

I'd go with the simplest answer : if you can concatenate a string and an int without casting, then it's not strongly typed.

e-satis
Yours is, by far, the best example of what is and what isn't strongly typed.
Hans Sjunnesson
Do you mean "then it's **not** strongly typed"? Because automatic variable coercion is not a feature I've seen in any strongly typed language.
John Millikin
yep, typo. My mistake. Will be punished.
e-satis
The point is that the term is useless, which you make well. But then you go on to afford some measure of definition. Needless to say, we can find *some* definition for which your example is incorrect. Here is one: a strongly-typed language has no undefined conversions. When there is an implicit conversion like you describe, the conversion *is* well defined (at least in Perl or PHP, which I presume you are referring to). Your answer is pretty good otherwise, so I would just remove your "simplest answer". Its not simple at all.
Paul Biggar
LOL. I guess you are right. I can't beat Pierce on that field :-) But I'm sure I cook better Tiramisus than he does.
e-satis
+7  A: 

The term "strongly typed" has no agreed-upon definition.

It makes a "great" argument in a flamewar, because whenever someone is proven wrong, they can just redefine it to mean whatever they want it to mean. Other than that, the term serves no real purpose.

It is best to just not use the term, or, if you use it, rigorously define it first. If you see someone else use it, ask him to define the term.

Everybody has their own definition. Some that I have seen are:

  • strongly typed = statically typed
  • strongly typed = explicitly typed
  • strongly typed = nominally typed
  • strongly typed = typed
  • strongly typed = has no implicit typecasts, only explicit
  • strongly typed = has no typecasts at all
  • strongly typed = what I understand / weakly typed = what I don't understand
  • strongly typed = C++ / weakly typed = everything else
  • strongly typed = Java / weakly typed = everything else
  • strongly typed = .NET / weakly typed = everything else
  • strongly typed = my programming language / weakly typed = your programming language

In Type Theory, there exists the notion of one type system being stronger than another. In particular, if there exists an expression e1 such that it is accepted by a type system T1, but rejected by a type system T2, then T2 is said to be stronger than T1. There are two important things to note here:

  1. this a comparative, not an absolute: there is no strong or weak, only stronger and weaker
  2. there is no value implied by the term; stronger does not mean better
Jörg W Mittag
I'm definitely not trolling, just looking to clarify the term.
Chris Sutton
Sorry, I didn't mean to imply that. I meant "you" as in "some undefined group". I'm going to edit that portion of my answer. Again, please excuse that misunderstanding, English is not my first language.
Jörg W Mittag
A: 

The term 'strongly typed' is completely and utterly nonsensical. It has no meaning, and never did. Even if some of the claimed definitions were accurate, I see no purpose as to the reason for distinction; Why is it important to know, discuss or debate whether a language is strongly typed (whatever that may mean) or not?

This is very similar to the terms 'Web 2.0' or 'OEM', which also have no real meaning.

What is interesting to consider, is how these phrases begin, and take root in everyday communication.

JF
I would have a agreed with you if you actually said 'statically-typed', all code except the weakest scripting languages are strongly typed.
leppie