开发者

Are All Dynamic Languages Typo-friendly?

With Java on one side and Ruby/Groovy on the other, I know that 开发者_如何学Cin 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.


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.


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'.


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.


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 declarations and no declaration 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜