Home Are variable types permanent in Statically Typed Languages?
Reply: 1

Are variable types permanent in Statically Typed Languages?

JBallin
1#
JBallin Published in 2017-12-05 00:00:38Z

My understanding is that variable types are "checked" before run-time for statically typed languages.

I take this to mean that a var of type int can't ever be type string? Does this mean variable type can't change (within the same scope) throughout the program (in a statically typed language)?

Somebody mentioned "variable shadowing" but I'm pretty sure that only applies in different scopes.

var i = 'hi';
function foo() {
    var i = 1;
}

My understanding of var shadowing is that i in the global scope is a different variable than i in the foo function scope and therefore their types are permanent and unrelated (in a static language, which JS is not). Is that right?

Oliver Charlesworth
2#
Oliver Charlesworth Reply to 2017-12-05 00:10:16Z

Somebody mentioned "variable shadowing" but I'm pretty sure that only applies in different scopes.

It depends on your definition of "scope", Rust, for example, allows the kind of shadowing that you're talking about, even within a single block:

fn main() {
    let a: str = "hello";
    let a: i32 = 3;
}

It could be argued that the declaration of a shadow variable implicitly ends the scope of the previous variable. But to quote from the Rust book:

Note that shadowing a name does not alter or destroy the value it was bound to, and the value will continue to exist until it goes out of scope, even if it is no longer accessible by any means.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.347341 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO