Are there any constants in Dart?

815 views
Skip to first unread message

Pedro Luz

unread,
Jul 30, 2012, 9:23:13 AM7/30/12
to mi...@dartlang.org
Are there any constants in Dart?

what i mean by constant, is a variable that i define in one place and i can call it in any place, any lib, any class, any source ??

something like constants in PHP, http://php.net/manual/en/language.constants.php

thanks

Gen

unread,
Jul 30, 2012, 9:36:42 AM7/30/12
to mi...@dartlang.org
I advise to have a look on the language tour.
http://www.dartlang.org/docs/language-tour/#variables

I have no more closely tracked Dart for the last months, but I think that:
1)
Any variable defined at the root level of a library is global.
But you must import explicitly that library to have access to that variable.
2)
Search for the keywords final, static and const in the documentation or the forum.

Isolates, the thread or process replacement in Dart, are special and might not allow any sharing of data.


Ladislav Thon

unread,
Jul 30, 2012, 9:37:43 AM7/30/12
to Pedro Luz, mi...@dartlang.org
Are there any constants in Dart?

Sure there are. Actually, there are two types of "constants":

1. final variables -- must be initialized to an arbitrary expression.

2. const variables -- must be initialized to a compile-time constant expression.

Note that this this distinction between final and const variables is relatively new and isn't yet implemented. Currently, static final variables or top-level final variables must be initialized to a compile-time expression and the const modifier isn't yet supported (except for the VM, which recognizes it since few days ago).

But still -- if you declare a "constant" variable in library A and want to use it in library B, you have to import A into B -- but this should be expected.

LT

Bob Nystrom

unread,
Jul 30, 2012, 4:12:50 PM7/30/12
to Ladislav Thon, Pedro Luz, mi...@dartlang.org
On Mon, Jul 30, 2012 at 6:37 AM, Ladislav Thon <lad...@gmail.com> wrote:
Are there any constants in Dart?

Sure there are. Actually, there are two types of "constants":

1. final variables -- must be initialized to an arbitrary expression.

2. const variables -- must be initialized to a compile-time constant expression.

Note that this this distinction between final and const variables is relatively new and isn't yet implemented. Currently, static final variables or top-level final variables must be initialized to a compile-time expression and the const modifier isn't yet supported (except for the VM, which recognizes it since few days ago).

Exactly right, as usual. :)

With the current semantics, any top-level final variable is essentially a constant.


But still -- if you declare a "constant" variable in library A and want to use it in library B, you have to import A into B -- but this should be expected.

Also exactly right. Dart doesn't have a single monolithic global scope. It does have a top-level library (i.e. module) scope which you can then import into your own libraries/modules.

- bob

Reply all
Reply to author
Forward
0 new messages