Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I wonder why are weakly-typed languages still being actively developed. For example, what benefit can one draw from being able to write

$someVar = 1;
(...)  // Some piece of code
$someVar = 'SomeText';

instead of using the much different, stongly-typed version

int someInt = 1;
(...)
string SomeString = 'SomeText';

It is true that you need to declare an aditional variable in the second example, but does that really hurt? Shouldn't all languages strive to be strongly-typed since it enforces type-safety at compile time, thus avoiding some pitfalls in type-casting?

share|improve this question
9  
"Strongly typed" is not a well defined term. Mostly it means "you cannot subvert the type system". It's orthogonal to what you describe above which might be latent versus manifest typing, or static versus dynamic typing. –  Frank Shearar Oct 22 '11 at 8:46
10  
What am I missing that moves this from flame bait with several closely related, arguably even duplicate, questions (just search SackOverflow for questions mentioning static and dynamic typing in their tags) to legitimate question? –  delnan Oct 22 '11 at 10:03
1  
There are advantages and drawbacks to both statically typed and dynamically typed languages. Dynamically typed languages lend themselves nicely to rapid development or prototyping (hence the reason "scripting languages" are usually dynamically typed), whereas statically-typed languages (arguably) are easier to maintain and extend as they grow into large, complicated projects. –  Charles Salvia Oct 22 '11 at 10:15
6  
The first example is a bit like Python where variables have no declared type. However, Python is a very strongly typed language because the objects -- themselves -- have a type which is almost impossible to change or coerce. I think the misuse of the terminology makes this question very hard to answer. –  S.Lott Oct 22 '11 at 11:08
1  
@delnan The fact that this question go two reasonable answers and didn't devolve into a flame war helps. –  Anna Lear Oct 22 '11 at 12:54
show 1 more comment

4 Answers

up vote 21 down vote accepted

Strong / weak typing and static / dynamic typing are orthogonal.

Strong / weak is about whether the type of a value matters, functionally speaking. In a weakly-typed language, you can take two strings that happen to be filled with digits and perform integer addition on them; in a strongly-typed language, this is an error (unless you cast or convert the values to the correct types first). Strong / weak typing is not a black-and-white thing; most languages are neither 100% strict nor 100% weak.

Static / dynamic typing is about whether types bind to values or to identifiers. In a dynamically-typed language, you can assign any value to any variable, regardless of type; static typing defines a type for every identifier, and assigning from a different type is either an error, or it results in an implicit cast. Some languages take a hybrid approach, allowing for statically declared types as well as untyped identifiers ('variant'). There is also type inference, a mechanism where static typing is possible without explicitly declaring the type of everything, by having the compiler figure out the types (Haskell uses this extensively, C# exposes it through the var keyword).

Weak dynamic programming allows for a pragmatic approach; the language doesn't get in your way most of the time, but it won't step in when you're shooting yourself in the foot either. Strong static typing, by contrast, pushes the programmer to express certain expectations about values explicitly in the code, in a way that allows the compiler or interpreter to detect a class of errors. With a good type system, a programmer can define exactly what can and cannot be done to a value, and if, by accident, someone tries somethine undesired, the type system can often prevent it and show exactly where and why things go wrong.

share|improve this answer
add comment

Weak typing is more along the lines of 1 == "TRUE". This section on the wikipedia nicely illustrates the difference.

Please note that neither example from the wikipedia is statically typed, which is what you refer to in your second example.

So if the question is, why people use dynamically typed languages, then the answer is: static type systems put limitations on you. Many people simply have never worked with an expressive static type system, which leads them to the conclusion that the disadvantages of static typing outweigh the benefits.

share|improve this answer
add comment

Weakly-types languages are still being developed because people use them and like them. If you don't like weak typing, don't use weakly-typed languages. Declaring that something is The One True Way and that Everybody Should Do It The One True Way ignores the complicatedness of the world.

share|improve this answer
add comment

Shouldn't all languages strive to be strongly-typed since it enforces type-safety at compile time, thus avoiding some pitfalls in type-casting?

Not necessarily. Learning Objective-C: A Primer addresses that question directly in the context of Objective-C:

Weakly typed variables are used frequently for things such as collection classes, where the exact type of the objects in a collection may be unknown. If you are used to using strongly typed languages, you might think that the use of weakly typed variables would cause problems, but they actually provide tremendous flexibility and allow for much greater dynamism in Objective-C programs.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.