Chris Sutton
Chris Sutton

Reputation: 2781

What are the key aspects of a strongly typed language?

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.

Upvotes: 17

Views: 13190

Answers (8)

SimpleGuy
SimpleGuy

Reputation: 2894

Statically typed language is one where the variables need to be declared before they can be used. While a Dynamically typed language is one where the variables can be used anytime, even if they are not declared. The only condition is that they must be initialized before they can be used.

Now, let us come to Strongly typed language. In such a language the variables have a type, and they will always be that type. They cannot be assigned to a value of another type. While a Weakly typed language is one where variables don't have a type. One can assign value of any type to them.

Example: Java is a statically typed as well as strongly typed language. It is statically typed, because one has to declare the variables before they can be used. It is strongly typed, because a variable of particular type int will always hold integer values. You can't assign boolean to them.

Powershell is a dynamically typed as well as weakly typed language. It is dynamically typed as variables need not be declared before using them. it is weakly typed as variable may hold value of one type at certain point while a value of another type at different point of time.

Upvotes: 1

Bite code
Bite code

Reputation: 596713

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.

EDIT: as stated in comments, Java just does that :-(

Upvotes: 11

JF.
JF.

Reputation: 21

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.

Upvotes: 1

Jörg W Mittag
Jörg W Mittag

Reputation: 369458

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

Upvotes: 29

MattC
MattC

Reputation: 12327

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 :)

Upvotes: 6

Mendelt
Mendelt

Reputation: 37483

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

Upvotes: 4

Mike Deck
Mike Deck

Reputation: 18397

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

Upvotes: 7

Hans Sjunnesson
Hans Sjunnesson

Reputation: 22299

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

Upvotes: 1

Related Questions