Is there a language that enables variable types to be changed?

239 Views Asked by At

I'm a pretty junior level developer (first year CS student) and I've been learning about the differences between static typed and dynamically typed languages. Correct me if I'm wrong, but it's my understanding that a dynamically typed language allows the programmer to initialize a variable without giving it a type, then give that variable a type later in the program. Just for the sake of curiosity, is there any languages out there that allow you to change the type/class of the object without initializing a brand new variable?

4

There are 4 best solutions below

0
On

I think that what you're looking for is weak typing. Note that weak vs. strong typing is not the same as static vs. dynamic typing.

0
On

It all depends on what you call a brand new variable. For example, in PHP:

<?php
$var = NULL; // $var is now of type null
$var = 1; // $var is now of type integer
?>

And so on. However, there is no guarantee that the space previously used for storing the NULL value is now used for storing the 1, so you could say that you just got yourself a brand new variable with the same name.

0
On

It depends on how you define types, but JavasScript doesn't have "classes" and allows you to easily change the interface to an object.

I don't know of any language with a strong OO basis that allows you to do something like:

typeof dog // Dog
dog.turnIntoCat()
typeof dog // Cat

However almost all OO languages support something like:

typeof dog // Dog
cat = dog.turnIntoCat()
typeof cat // Cat

And I think all dynamically typed languages (at least all that I know of) allow this:

typeof dog // Dog
dog = new Cat() 
typeof dog // Cat
0
On

There are a lot of definitions of static/dynamic typing and strong/weak typing, so it's hard to answer any general question very concretely. That being said, the (very high level) definition I use for them tends to convey the general idea fairly well (at least, I think so).

Static vs Dynamic Typing

A statically typed language applies types to variables. The variable count can be defined as an integer. It can only hold integer values.

A dynamically typed language applies types to values, but not variables. The value 123 is an integer and "abc" is a string, but the variable result could be assigned to either or both at different points in time.

Strong vs Weak Typing

In a strongly typed language, a value has a type and it is only that type. For example, "123" is a string where 123 is an integer. You can't treat the string as an integer and vice versa. You can convert between them (ie "123".toint() or such), but you can't just treat one type as another (ie. the following wouldn't be valid: "123" + 456 == 579)

In a weakly typed language, a value is just a value and you can treat it as various types depending on it's use. For example, you CAN say "123" + 234 and get a useful result (357 or 123234 depending on the language).

There are a LOT of grey areas between static and dynamic, and between strong and weak, but the definitions above give a general idea.

On a related topic, there's also explicit vs implicit typing (programmer designates types vs compiler figures out types), which is a really interesting topic all on it's own.