site-www icon indicating copy to clipboard operation
site-www copied to clipboard

Improve Lang-tour const constructors explanation

Open Rodsevich opened this issue 6 years ago • 3 comments

There was a doubt in this repo's Gitter about const constructors. @zoechi answered very well here:

a const constructor has constraints, like it can't have a body. A class can have const constructors and non-const constructors. Only const constructors can be used to create constant values. You still can have runtime-created instances of the same class, for example if parameter values can't be provided as constants.

It would be nice to have that answer included in https://www.dartlang.org/guides/language/language-tour#using-constructors

Rodsevich avatar Oct 04 '18 11:10 Rodsevich

It could make sense to point out how const can be used and what the benefits are.

In that discussion we figured out, that you can use const at constructors, but maybe there are more ways to use const differently in dart, which is worth to take a deep look, or better explanation.

mader102 avatar Oct 04 '18 11:10 mader102

See also

  • https://stackoverflow.com/questions/21744677/how-does-the-const-constructor-actually-work
  • https://stackoverflow.com/questions/12418819/why-does-dart-have-compile-time-constants

zoechi avatar Oct 04 '18 11:10 zoechi

cc @kwalrath

mit-mit avatar Oct 04 '18 13:10 mit-mit

I'm going to close this in favor of https://github.com/dart-lang/site-www/issues/4350 to consolidate issues related to const in the language tour. Thanks!

parlough avatar Nov 07 '22 22:11 parlough