Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between dynamic and static type in Dart

Tags:

dynamic

dart

Two questions. First, Below is strong type.

String msg = "Hello world.";
msg = "Hello world again.";

And, below dynamic

var msg = "Hello world.";
msg = "Hello world again.";

Is there any difference between the two 'msg's above?

Second, if I use 'new' keyword to initiate a variable as below,

Map myMap = new Map;

Why to indicate the variable 'myMap' is a Map instance(Map myMap) as 'new' keyword already include the same meaning? So, isn't it okay just,

myMap = new Map;

Because the 'new' keyword already implies the newly initiated variable is both variable and Map type, I can't understand why normally 'Map' keyword is with the variable name, even 'var' also.

Does anyone have any idea about this (seems a little bit redundant) Dart grammar?

like image 270
Letsgo Avatar asked Sep 12 '14 04:09

Letsgo


People also ask

What is the difference between dynamic and object in Dart?

There is actually no difference between using Object and dynamic in the example you have given here. There is no practical difference, and you can swap the two and the program will run the same. When I refer to "semantic difference", I mean how the code will be understood by other Dart programmers.

What is dynamic data type in Dart?

Dart dynamic type In Dart, when a variable is declared as a dynamic type, it can store any value, such as int and float . The value of a dynamic variable can change over time within the program.

What is the difference between static and dynamic type checking?

Dynamic type checking can find many errors that cannot be identified by static type checking. In most languages, static type checking is not possible for some language constructs in certain cases but the same purpose can be achieved by dynamic type checking.

What does static mean in Dart?

"static" means a member is available on the class itself instead of on instances of the class. That's all it means, and it isn't used for anything else.


3 Answers

In regard to the first question, there will be no difference in what each msg variable contains.

For the Map question, the reason for specifying the type of a variable that is constructed at declaration is to allow some flexibility with subclasses. Take for example the following code:

class SubMap extends Map {
  SubMap() : super();
}

Map map = new SubMap();

Here we have a variable map which contains a SubMap object as its value, however we are allowing it to contain values of type Map (or other types which subclass Map) at later times.

The main thing to remember with Dart is that it is optionally typed. When running your code, none of your type annotiations make any difference at all (unless you run in checked mode). What the type annotations are for is to help your IDE and other tools provide autocomplete help, possible warnings, etc. which other fully dynamic languages like Javascript cannot.

like image 173
Michael Fenwick Avatar answered Nov 03 '22 01:11

Michael Fenwick


String msg = "Hello world.";
msg = "Hello world again.";
msg = 1; // exception in checked mode - int can not be assigned to String.


var msg = "Hello world.";
msg = "Hello world again.";
msg = 1; // ok in checked mode

Checked mode is the developer mode where type annotations are checked and create runtime exceptions when code violates them.

In unchecked (production) mode it makes no difference if you add a type annotation and which one. This is for performance reasons because checked mode is slower.

like image 33
Günter Zöchbauer Avatar answered Nov 02 '22 23:11

Günter Zöchbauer


The declaration Map myMap = new Map() does three things:

  • it declares a variable named myMap with type-annotation Map,
  • it creates a new object using the Map constructor, and
  • it assigns the object to the variable.

The type annotation means that myMap has static type Map. The static type can give you some warnings at compile time, but it doesn't change anything at runtime. The type annotation also adds a type check in checked mode, where any assignment to the variable is checked for being assignable to Map.

The object you create could be any object using any constructor. I regularly use new HashMap() instead of new Map(), and still assign it to a variable typed as Map.

Then the object is assigned to the variable, and the type check succeeds, if in checked mode.

The variable is independent of the value used to initialize it. If you later assign a string to the myMap variable, it will fail in checked mode.

So, the difference between the two msg variables is that one has a static type and a type check in checked mode. For the code you show, there is no difference in behavior.

If the next line was var y = msg + 42;, then the typed version would give a warning (msg has static type String, and String.operator+ has type String->String, so the 42 has an invalid argument type), where the untyped version wouldn't warn you (msg has type dynamic so you probably know what you are doing).

like image 34
lrn Avatar answered Nov 02 '22 23:11

lrn