Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between dynamic and Object in dart?

They both seem like they can be used in identical cases. Is there a different representation or different subtleties in type checking, etc?

like image 476
user3761898 Avatar asked Jul 07 '15 00:07

user3761898


People also ask

What does dynamic mean 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 object in Dart?

Objects are basic building blocks of a Dart program. An object is a combination of data and methods. The data and the methods are called members of an object. Objects communicate together through methods. Each object can receive messages, send messages and process data.

What's the difference between VAR and dynamic type in Dart?

dynamic: can change TYPE of the variable, & can change VALUE of the variable later in code. var: can't change TYPE of the variable, but can change the VALUE of the variable later in code. final: can't change TYPE of the variable, & can't change VALUE of the variable later in code.

How do you make a dynamic object in darts?

You can use normal global variables in Dart like explained in Global Variables in Dart. final Map<String,int> myGlobals = <String,int>{}; to create a map that stores integer values with string names. Set values with myGlobals['someName'] = 123; and read them with print(myGlobals['someName']); .


2 Answers

Another perspective on dynamic is that it's not really a type - it's a way to turn off type checking and tell the static type system "trust me, I know what I'm doing". Writing dynamic o; declares a variable that isn't typed - it's instead marked as "not type-checked".

When you write Object o = something; you are telling the system that it can't assume anything about o except that it's an Object. You can call toString and hashCode because those methods are defined on Object, but if you try to do o.foo() you will get a warning - it can't see that you can do that, and so it warns you that your code is probably wrong.

If you write dynamic o = something you are telling the system to not assume anything, and to not check anything. If you write o.foo() then it will not warn you. You've told it that "anything related to o is OK! Trust me, I know what I'm doing", and so it thinks o.foo() is OK.

With great power comes great responsibility - if you disable type-checking for a variable, it falls back on you to make sure you don't do anything wrong.

like image 51
lrn Avatar answered Sep 28 '22 06:09

lrn


The section Type dynamic from the Dart Programming Language Specification, 3rd Edition states :

Type dynamic has methods for every possible identifier and arity, with every possible combination of named parameters. These methods all have dynamic as their return type, and their formal parameters all have type dynamic. Type dynamic has properties for every possible identifier. These properties all have type dynamic.

That means you will not get warnings by calling any method on a dynamic typed variable. That will not be the case with a variable typed as Object. For instance:

dynamic a; Object b;  main() {   a = "";   b = "";   printLengths(); }  printLengths() {   // no warning   print(a.length);    // warning:   // The getter 'length' is not defined for the class 'Object'   print(b.length); } 

At runtime, I think, you shouldn't see any difference.

like image 21
Alexandre Ardhuin Avatar answered Sep 28 '22 05:09

Alexandre Ardhuin