views:

242

answers:

4

With Java on one side and Ruby/Groovy on the other, I know that in the second camp I'm free to make typos which will not get caught until run-time. Is this true of all dynamically-typed languages?

Edit: I've been asked to elaborate on the type of typo. In Ruby and in Groovy, you can assign to a variable with an accidental name that is never read. You can call methods that don't exist (obviously your tests should catch this, it's been said). You can refer to classes that don't exist, etc. etc. Basically any valid syntax, even with typographical errors, is valid in both Ruby and Groovy.

+3  A: 

In Perl, if you declare use strict in your code, then you must declare your variables with my. Typos in variable names will then be caught at compile-time. This is one of the biggest things I miss when coding in Python.

ire_and_curses
By the way, I think `use strict` makes Perl's usability far far better than otherwise. :)
Paul Nathan
The strangest thing about all this is, wasn't there an option to do that in QuickBasic or maybe the early Visual Basics? Insist that you wanted to use types?
Yar
@yar: You're not insisting on using types. You're insisting on using variable declaration. The type of the variable has nothing to do with whether you mis-spell its name. Fortran is a strongly-typed language that allows you to avoid declaring variables. You need `IMPLICIT NONE` at the top, just like Perl's `use strict`. If you don't use `IMPLICIT NONE`, you can make typos with your variable names just as easily as in a dynamic language.
ire_and_curses
Okay, that's clear, thanks for that.
Yar
+2  A: 

For the most part, yes. Dynamic typing and not requiring declaration of variables are language properties that are frequently found together.

However, these are not inherently related. A language can easily have dynamic typing while requiring variable names to be declared before use. As ire_and_curses mentions, this can be achieved in Perl via the "use strict" directive.

Andrew Medico
The other way around happens, too - Objective C is dynamic re: method names, but variable declaration rules are those of C.
Seva Alekseyev
Great answer. I think it's true for other things, and not just variable declaration. My question is, aside from SmallTalk, what is the biggest dynamic language rulebreaker? Scala?
Yar
I'm not a Scala programmer but my understanding is that Scala is not a dynamic language by most definitions.
Alexandre Jasmin
+2  A: 

Here's what happens when I try to get into the pitfalls you mentioned in Squeak and Dolphin two implementations of the dynamic language Smalltalk 80.

You can assign to a variable with an accidental name that is never read

The Smalltalk language requires temp and instance variables to be declared. If I try to compile a method containing an undefined variable I get a compile-time error.

| anArray |
anArrray := Array with: 2 with: 1. "Unknown variable anArrray"

Creating variables dynamically is not something dynamic languages have to allow. There's a difference between typeless decelerations and no deceleration at all.


You can call methods that don't exist

The compiler issue a warning if you use a selector (i.e. method name) that is entirely unknown.

The compiler won't bother if I call the method paint on an array because there's another class in the system implementing paint. That error will only be caught at runtime.

If however I call the method sortt (while I intend to call sort) the compiler generates a warning. When developing top-down you can proceed pass these warnings.

| anArray |
anArray := Array with: 2 with: 1.
anArray paint. "Runtime error. You can't paint an array but perhaps a Shape"
anArray sortt. "Compile-time warning"

You can refer to classes that don't exist

This is not allowed. Though in Squeak you can quickly create a new class from the error dialog if needed.

Alexandre Jasmin
Fascinating. I don't know if Smalltalk is gaining in popularity, but I do hear about it on all of my dynamic-language questions :)
Yar
+3  A: 

Python is typo-friendly in the way you described in your question.

But this does not mean that these 'typos' can only be caught @ runtime. When using a code analyzer like pylint (ideally integrated into your development environment) you'll catch 'most' of these consistently before hitting 'run'.

ChristopheD
Really? That's neat, I didn't know that. In Ruby there's no such thing, I think. Netbeans catches a few things, but it gets confused quite quickly
Yar
Some tools exist according to this question (http://stackoverflow.com/questions/286564/can-anyone-recommend-a-ruby-source-code-analyzer-something-like-pylint) although none of the mentioned tools seems as fully-featured as pylint at the moment.
ChristopheD