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 was researching on statically typed and dynamically typed concepts and found out there is another type system called gradually typed.

From wikipedia

Gradual typing is a type system in which variables may be typed either at compile-time (static typing) or at run-time (dynamic typing), allowing software developers to choose either type paradigm as appropriate, from within a single language.

In particular, gradual typing uses a special type named dynamic to represent statically-unknown types and gradual typing replaces the notion of type equality with a new relation called consistency (aka. compatibility) that relates the dynamic type to every other type. The consistency relation is symmetric but not transitive

If I understand those concepts correctly, "Javascript" is a gradually typed language. Am I correct?

share|improve this question
2  
It might help to explain what a gradually typed system is for those of us who don't know. Yes, we can Google it, but it really helps to have pertinent information in your question. –  user3580294 Jun 23 at 5:44
    
I can give you a link to a wiki page. –  Roshan.Ind Jun 23 at 5:45
    
I know. I can find it myself too. I was just suggesting that you put that information in your question. Just like how answers generally should stand on their own, I'd guess questions have the same expectation. –  user3580294 Jun 23 at 5:47
2  
How do you type a variable at compile-time in JavaScript? –  Carson63000 Jun 23 at 7:24
2  
JavaScript is dynamically typed. TypeScript is a gradually typed JavaScript dialect. –  CodesInChaos Jun 23 at 8:26

2 Answers 2

up vote 7 down vote accepted

Mixing static and dynamic typing is still very much an open research issue with several competing approaches, including but not limited to Jeremy Siek's Gradual Typing (which you mentioned), Quasi-Static Typing, Soft Typing, Hybrid Typing (Flanagan), Optional Static Typing (championed for example by Gilad Bracha), Blame Calculus (Wadler and Findler), Coercion Calculus (Henglein), Contracts (Findler and Felleisen), Manifest Contracts (Greenberg, Pierce, and Weirich) and many more.

ECMAScript / JavaScript is not gradually typed. It would be stupid to add something that is still an open research problem to a production language such as ECMAScript. A language design committee is not the place to do cutting-edge research.

Actually, the ECMAScript committee tried to do cutting-edge research by mixing static and dynamic typing in ECMAScript 4 … and that was a spectacular failure.

ECMAScript / JavaScript are dynamically typed. There are no static types, nor is there syntax for type annotations.

share|improve this answer
    
> I think this is the best explained answer. I went through some of the other type systems also. Thank you for sharing those... –  Roshan.Ind Jun 23 at 11:42
    
I would say "dynamically weakly typed" or "loosely typed". Just saying "dynamically" is a (undeserved) compliment. –  Den Jun 23 at 11:42
1  
@Den: Unfortunately, those terms are not well-defined. There are at least a dozen contradictory definitions in the wild. It just doesn't make sense to use those terms. –  Jörg W Mittag Jun 23 at 12:23
    
@JörgWMittag: In a proper weakly-typed language, variables may be of any type, but the meanings of operators won't change with the types of things upon which it is invoked. In Apple's HyperTalk language (1990s), the expression (1 & 2) + 3 would concatenate "1" and "2" as strings, and then add the result as a number to 3, thus yielding 15. In a strongly-dynamically-typed language like Python, using the "+" operator on two numbers will add then, using it on two strings will concatenate them, and using it on one of each is an error. –  supercat Sep 28 at 22:55
    
@JörgWMittag: JavaScript's behavior is worse than that of HyperTalk or Python, since it uses the "+" operator for two different purposes, but doesn't cleanly separate the cases where those purposes apply. This, 12+"X" yields "12X" and "X"+34 yields "X34", but 12+34 yields 46. –  supercat Sep 28 at 22:58

To elaborate on why JavaScript is not gradually typed, let's look in the other direction at C#, which is statically typed with implicit typing.

According to MSDN, C# introduced a var keyword with version 3.0 released in 2007. It is only allowed on "variables that are declared at method scope", what I would call local variables. The var keyword falls short of being a dynamic type, because it allows the compiler to figure out what type the declaration should, but then treats all references to that variable as the given type exactly as if you had declared it statically yourself.

Here are some examples of how it can and cannot be used.

class MyClass {
    // Standard member variable
    public string MyName = "Patrick";

    // Illegal declaration - var cannot be used here even though the type may be inferred
    // from the literal definition; compiler throws an error
    private var LuckyNumber = 7;
    // Have to do it like this:
    private int LuckyNumber = 7;

    private MyClass(int luckyNumber) {
         LuckyNumber = luckyNumber;
    }

    public MyClass MyClassFactory(int newLucky) {
         // Legal - myInstance is in the method scope
         var myInstance = new MyClass(newLucky);
         // Not legal - myInstance is not a dynamic type just because we declared it with var
         myInstance = new MyOtherClass();

         // We can return myInstance because its type matches the method signature
         return myInstance;
     }
}

In other words, C# does not provide a way to declare an variable that can hold an arbitrary type. The closest you can get is with polymorphism, where assignments are restricted to the inheritance tree.

Note that in sharp contrast, JavaScript would allow the illegal line in MyClassFactory. The following is perfectly legal in JS:

var foo = "foo";
foo = 1;
foo = 2.0;
foo = new Date();
foo = [];
foo = {};
foo = function() {};

Every line changes the type of foo. Furthermore, there is no way to declare foo as a type that may not be changed.

Note that there are ways to shoehorn static typing into JavaScript, but they are actually wrapper layers around the actual JS code, which ends up every bit as dynamically typed as if you had written it by hand. There are way more ways than I would have imagined, as it turns out: here is a comprehensive-seeming list of languages (or more properly syntaxes or parsers) that can churn out JavaScript. Of these, CoffeeScript is the most common in my experience.

Finally, a language must provide both dynamic and static declarations in order to be considered dynamically typed. In other words, the following must be possible with some syntax:

// Statically typed declaration
Integer myNumber = 3;
// May be legal if the language provides for auto-conversion or dynamic-casting; may
// throw an error. It depends on the language
myNumber = 7.5;

// Static type declaration of an object class
MyParentType instance = new MyParentType();
// legal if MyChildType inherits/extends MyParentType - illegal otherwise
instance = new MyChildType();

// Illegal always - MUST throw an error
instance = new IncompatibleType();

// Dynamic typed declaration
dynamic otherInstance = new MyParentType();
// Legal for variables declared dynamic - MUST NOT throw an error
otherInstance = new IncompatibleType();

Note that, as demonstrated, neither C# or JS support both types of variable declarations for different reasons.

share|improve this answer
1  
"This is commonly referred to as implicit typing, but as the compiler determines the type, I contend that it is therefore gradual typing." This makes no sense unless you change the definition of gradual typing. There is no additional runtime checks involved in type-inferred variables. –  Doval Jun 24 at 17:52
    
Aw nuts, you're right; I misunderstood. How can I edit it to not make me look stupid... –  Patrick M Jun 24 at 17:59
    
I'd just cut the sentence off at "this is commonly referred to as implicit typing." Actually, I believe the proper term is inferred typing. Implicit typing redirects to latent typing on wikipedia which is pretty much just dynamic typing. –  Doval Jun 24 at 18:03
2  
@Doval Yep, it had to go. And you may be right about inferred being more broadly correct; but C# is MicroSoft's baby and they call it implicit typing, so they'll just have to keep on being wrong. They're pretty good at that... –  Patrick M Jun 24 at 18:20
    
Was not aware MicroSoft adopted that term officially. Good find. –  Doval Jun 24 at 18:23

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.