Would javascript and similar scripting languages benefit from being strongly typed? [closed]
Asked Answered
R

7

11

Just had my mind going today. I spent some time in IE debug mode, browsing the web as usual, and oh boy do I see many errors :) Most of these errors are because some value are of a different type than expected (at least as far as I interpret the error messages).

What are the reasons JavaScript and similar scripting languages aren't strongly typed? Is it just to make the languages "easier" to understand and more accessable, or is the lack of a "compile-time" the real problem?

Recrudesce answered 11/8, 2009 at 23:33 Comment(3)
You should probably post your opinion as an answer - to gauge how strongly people (dis)agreePropriety
Clarification: There is nothing stopping you from parsing javascript or any other dynamic language. What you're talking about is semantically evaluating the parsed text.Burgess
Added my own oppinion as an answer. And ye, Jared, but assuming a browser environment, none I know of does that (at least not to my knowledge?). If you had a strongly typed scripting language, you'd be kindof forced to it I think? :)Recrudesce
T
15

It should definitely have strong typing available. Actionscript 3 is strongly typed, but still has prototype inheritance and a wildcard type if you need dynamic objects.

There are no downsides to having that feature available, and I have to say, for a project of moderate to large size, strong typing prevents A TON of problems. To get the most out of it you need IDE support so it can report errors and provide autocomplete options, but Javascript would be in a whole new world if it had real classes and strong typing.

Tungusic answered 11/8, 2009 at 23:33 Comment(2)
+1 for "What's the harm in adding it"; people who don't want it don't have to use it.Woolley
After 6 years, this is very close to happening - Javascript having real classes. Though, I'm not too sure about strong typing.Dowlen
A
2

Would javascript and similar scripting languages benefit from being strongly typed?

Yes, they would, JavaScript 2.0 introduces a type system:

Type System JavaScript 2.0 supports the notion of a type, which can be thought of as a subset of all possible values. There are some built-in types such as Object, Number, and String; each user-defined class (section 6) is also a type.

Also see: http://timkadlec.com/2008/04/an-objective-look-at-javascript-2-0-strong-typing/

In general, support for strong typing provides many interesting opportunities for the compilation and optimization passes.

Appendage answered 11/8, 2009 at 23:33 Comment(0)
D
2

It gains flexibility from not being typed. I personally enjoy the weakly typed languages.

So the answer is there'd be benefits and drawbacks.

For people who want a strongly-typed language in the browser, GWT and Script# are available.

Disconnected answered 11/8, 2009 at 23:33 Comment(1)
I wouldn't say they're flexible so much as they introduce a whole class of errors that could be prevented automatically, and they do it while offering no extra additional functionality. You could have a guarantee that that object you passed that function will support the method you need, but you'll never know unless that line of code gets executed...Gogetter
A
1

I like the weak typed aspects of most scripting languages for the most part. The only reason that I would want strongly typed, besides for performance, is that it is easier for tools to refactor strongly typed languages than weak.

Antivenin answered 11/8, 2009 at 23:33 Comment(1)
Also, strong typing seems to eliminate entire classes of errors, and it makes it easy to remember what methods your object supports. With Javascript, two objects of the same type can have completely different methods. Yippee!Gogetter
R
1

My own oppinion: You could parse scripts before executing them. This would catch most type-errors, and mean that the user doesn't have to see a partly-executed-then-terminated scriptresult. Even better, it would be a lot easier to debug the thing, if it had a parser :)

Recrudesce answered 11/8, 2009 at 23:33 Comment(0)
G
0

Microsoft has gone a long way to solving strong typing the problem in the interim with typescript. Have a look:

http://www.typescriptlang.org/

Gyatt answered 11/8, 2009 at 23:33 Comment(0)
S
0

I build a rapid prototype framework for eLearning in ActionScript 2 way back when. My biggest gripe was AS2 was not strongly typed and it causes me many a headache when debugging. I think that strongly typing things makes code easier to read. I think a weakly typed language offers more flexibility.

I lean more towards readability when i have to go back and figure out what the heck is going on in code i wrote 6 months ago.

Scuttlebutt answered 11/8, 2009 at 23:33 Comment(1)
AS2 is strongly typed. Just specify the type after the variable. eg: var vector1:VectorPatsy

© 2022 - 2024 — McMap. All rights reserved.