What are the key aspects of a strongly typed language?
Asked Answered
A

8

18

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.

Astroid answered 23/9, 2008 at 14:34 Comment(1)
possibly the difference gonna answer your question #2691044Bradski
A
30

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
Anyways answered 23/9, 2008 at 18:17 Comment(3)
I'm definitely not trolling, just looking to clarify the term.Astroid
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.Eft
another approach should be; strongly typed = women / weakly typed = men or vice versa :)Scorekeeper
A
11

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

Arise answered 23/9, 2008 at 15:27 Comment(7)
Yours is, by far, the best example of what is and what isn't strongly typed.Haste
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.Ariadne
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.Bendy
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.Arise
Do you imply that Java is not strongly-typed because you can write "Hi" + 5? That sounds problematic. Moreover, that would imply that Scala is also not strongly typed, which is funny since everybody complains that it's so much typed to be scary.Fluoresce
Ouch. My evil plan to get reputation is falling appart.Arise
What's the source? The best I find is en.wikipedia.org/wiki/Talk%3AStrong_typing#Pierce_quotation.Fluoresce
S
7

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
Scraggy answered 23/9, 2008 at 14:45 Comment(1)
C could be said to be statically, weakly typed. It's weird, but possible.Klehm
G
6

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

Girosol answered 23/9, 2008 at 14:48 Comment(0)
G
4

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

Grandam answered 23/9, 2008 at 14:46 Comment(0)
H
1

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
Haste answered 23/9, 2008 at 14:36 Comment(2)
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 artima.com/weblogs/viewpost.jsp?thread=7590Scoutmaster
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.Eft
G
1

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.

German answered 30/3, 2010 at 12:40 Comment(2)
I would have a agreed with you if you actually said 'statically-typed', all code except the weakest scripting languages are strongly typed.Rothstein
It makes sense to discuss static vs dynamic typing, type-safe vs unsafe, explicit vs implicit typing (type inference), and so on. Just "strong typing" is ill-defined.Fluoresce
M
1

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.

Marandamarasca answered 1/1, 2018 at 15:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.