Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When declare an local variable, it's better to declare the type or just use `var`, in Dart?

Tags:

types

var

dart

In dart, there is a var which means dynamic type.

When declare an local variable, I can write:

String name = "Freewind";

or

var name = "Freewind";

At first I thought they are the same, since the editor should be able to infer the type of name is String, but I soon found:

void hello(String name) { print("hello, $name"); }

int n = 123;
hello(n);      // editor will give an warning here

var m = 456;
hello(m);      // but will not here

I tried with DartEditor(based on eclipse) and IDEA, found neither give warning on hello(m). It seems that they treat the m as dynamic, not int, so they don't give warning.

If I understand correct, that we should declare the types as much as possible to gain type-safe checks, right? But I like var since it's shorter, don't need to duplicate the type information.

Or there is no warning just because the editors of Dart is not strong enough, that we will get warnings in the future?

like image 979
Freewind Avatar asked Jul 06 '13 09:07

Freewind


2 Answers

From Dart FAQ:

Does Dart have type inference?

Type inferencing is not something specified by the language specification, but it is something that implementations are free to do. It's important to remember that Dart has a dynamic type system, so type inferencing doesn't play the same role as it does in languages such as Haskell. However, Dart Editor does do some type inferencing, such as when you use var for local variables. We expect that the Dart VM and dart2js will use type inferencing when it's useful for performance or for other reasons.


From Dart Style Guide:

PREFER using var without a type annotation for local variables

Method bodies in modern code tend to be short, and the types of local variables are almost always trivially inferrable from the initializing expression, so explicit type annotations are usually just visual noise. Decent editors can infer the type of local variables and still provide the auto-complete and tooling support you expect.


Q: When declare an local variable, it's better to declare the type or just use var, in Dart?

A: Better way to use 'var'.


Q: Or there is no warning just because the editors of Dart is not strong enough, that we will get warnings in the future?

A: Possible this arrives in near future when new Dart Editor analyzer will be fully implemented and improved.

like image 171
mezoni Avatar answered Nov 11 '22 06:11

mezoni


If I understand correct, that we should declare the types as much as possible to gain type-safe checks, right?

Sort of. By providing type information, you provide information to tools and fellow humans about your intention. It's up to the tools to make use of any type information you provide.

Dart itself runs in two modes - checked or production mode. When you run an app from the editor, by default it runs in checked mode. This effectively performs type assertions while the code is running. The example given above passes the static analyzer without warning, but would fail to run in checked mode. In production mode, it would try to run (and in this case would run successfully).

By run successfully, in production mode your code is treated as though it were

void hello(name) { print("Hello $name"); } // note the lack of type info on the signature

var i = 123;
hello(i); // "Hello 123"

This is effectively the same as JavaScript - it will run until the point of failure. The type information are just annotations, formalized into the language, so when you write

voidhello(Stringname) { ... } the void and String are type annotations that are ignored in production mode but can be used by tools and humans alike.

like image 39
Chris Buckett Avatar answered Nov 11 '22 07:11

Chris Buckett