Discussion:
[dart-misc] Newbie question about const
Kovács Attila
2018-10-13 01:49:19 UTC
Permalink
Hi

Could somebody tell me what is the difference between them :


1. const List<String> li = ["a","b"];

vs.

2. List<String> li = const ["a","b"];

vs.

3. const List<String> li = const ["a","b"];

thank you in advance
--
For more ways to connect visit https://www.dartlang.org/community
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
To view this discussion on the web visit https://groups.google.com/a/dartlang.org/d/msgid/misc/4fa1dd92-76ba-4794-a935-e06a595cce7f%40dartlang.org.
Tom M
2018-10-13 04:46:44 UTC
Permalink
Hi Atilla,

1 and 3 are the same. Since the left-hand side is a const declaration, the
right-hand side *must be* a transitively constant value. This const
keyword can be inferred, and Dart style prefers
<https://www.dartlang.org/guides/language/effective-dart/usage#dont-use-const-redundantly>
to omit it.

In 2, the value is assigned to an ordinary mutable variable. Note Dart
constants are canonicalized: each variable points to the same object, so
they are all == and identical.

If you write List<String> li = ['a', 'b'] instead, a constant value is *not*
inferred. A new list is allocated.

https://dartpad.dartlang.org/acb3d4f93aaab8fdd6b03fa7d5e4b2eb

Hope it helps!

PS. You can omit types too. Dart can infer from the assignment that li is a
List<String>.

On Fri, Oct 12, 2018 at 9:49 PM Kovács Attila ***@gmail.com
<http://mailto:***@gmail.com> wrote:

Hi
Post by Kovács Attila
1. const List<String> li = ["a","b"];
vs.
2. List<String> li = const ["a","b"];
vs.
3. const List<String> li = const ["a","b"];
thank you in advance
--
For more ways to connect visit https://www.dartlang.org/community
---
You received this message because you are subscribed to the Google Groups
"Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an
To view this discussion on the web visit
https://groups.google.com/a/dartlang.org/d/msgid/misc/4fa1dd92-76ba-4794-a935-e06a595cce7f%40dartlang.org
<https://groups.google.com/a/dartlang.org/d/msgid/misc/4fa1dd92-76ba-4794-a935-e06a595cce7f%40dartlang.org?utm_medium=email&utm_source=footer>
.
--
For more ways to connect visit https://www.dartlang.org/community
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
To view this discussion on the web visit https://groups.google.com/a/dartlang.org/d/msgid/misc/CAAHYbeZWHBF4ixOby5YXk1JdMz%2B1jiWNT4PAu73QL%3DdOsCOkJg%40mail.gmail.com.
Kovács Attila
2018-10-13 09:37:58 UTC
Permalink
Thank you for your detailed explanation. It was very useful.
Attila
Post by Kovács Attila
Hi
1. const List<String> li = ["a","b"];
vs.
2. List<String> li = const ["a","b"];
vs.
3. const List<String> li = const ["a","b"];
thank you in advance
--
For more ways to connect visit https://www.dartlang.org/community
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
To view this discussion on the web visit https://groups.google.com/a/dartlang.org/d/msgid/misc/364aa40a-1ecb-4d50-9ea2-9f5e57258a9e%40dartlang.org.
Continue reading on narkive:
Loading...