Static fields const

I read this answer in SO, and I was wondering why fields are explicitly declared both static and const. Are constant fields a compilation of time constants in Darth? and if this does not mean that they are implicitly static?

+2
source share
2 answers

You could theoretically modify Dart so that the const modifier introduces static . This is a valid proposal and has been actively discussed.

There are two reasons why we prefer to use explicit static :

  • This makes access to these variables (like any other static) more understandable.
  • We can use an instance of const for a different value. Currently the const instance fields are strictly equivalent to the final fields. However, they do not have to be. For example, you can modify the Dart specification to allow access to the fields of a const instance as part of a constant expression. (Currently, access to the fields on the right side of const fields is not allowed.)
+4
source

Constant in class:

 class Consts { static const int maxLength = 50; } 

Using:

 ... int length = Consts.maxLength; ... 
0
source

Source: https://habr.com/ru/post/1485882/


All Articles