In Dart, is it possible to have a configuration fault map?

I created a literal closure map, something like:

Map<String, Function> mapOfFuncs = { 'foo': (a, b, c) => ... , 'bar': (a, b, c) => ... , ... } 

All still. Then I wanted to make this map const , because it is global in my program and should never be changed.

 const Map<String, Function> MAP_OF_FUNCS = const { 'foo': (a, b, c) => ... , 'bar': (a, b, c) => ... , ... } 

Dart pinches this because the literal closures on the map are not const .

On Dartpad: https://dartpad.dartlang.org/817d2cfd141b0a56fc7d

I would think that literal closures are const . Is there any way to make them like that?

+5
source share
2 answers

Closing is not supported in const expressions. There is an open question https://github.com/dart-lang/sdk/issues/4596 and https://github.com/Pajn/dep-const-function-literals/issues/1

If you create static functions instead, you can reference them in const map literals, but you cannot currently define them inline.

+2
source

I suspect this may not be possible, look at this code:

 int test1(int a, int b, int c) { return a; } int test2(final int a, final int b, final int c) { return a; } const Function f1 = test1; const Function f2 = (final a,b,c) => a; const Map<String, Function> MAP_OF_FUNCS = const { 'foo': test1, 'fam': test2, 'bam': f1, 'bar': f2 }; 

Only the first two versions work in this constellation, referring to the links of the static method test1 and test2 . Even f1 generates a UPDATE compilation error, but compiles with dartJS as @irn from the marked comments. Then it is not clear why the version with f2 does not work.

So this is probably an assignment statement that cannot create a statically compiled link for a given constant lambda expression or a link to a static method for an RHS argument (right side).

The documentation pointed me to testing the static const combination, but usually it only works with top-level elements such as class members. Thus, adding a new class allows you to verify this.

 class A { static const Function a1 = test1; static const Function a2 = (final a, final b, final c) => a; } const Map<String, Function> MAP_OF_FUNCS = const { 'foo': A.a1, 'bar': A.a2 }; 

However, these function definitions are valid, but assignment to its map is not performed as before. The map documentation in the built in types section shows how to create a compile-time constant map using the final keyword.

 final constantMap = const { 2: 'helium', 10: 'neon', 18: 'argon', }; 

Unfortunately, this approach has the same drawback. This can lead to a restriction on character symbol restrictions:

A Symbol object is an operator or identifier declared in a Dart program. You may never need to use characters, but they are priceless for APIs that refer to identifiers by name, because minification changes identifier names, but not identifier characters .... Character literals are compile-time constants.

For more on symbols, see dart: mirror - reflection .

Maybe someone has a better idea, but for me it seems impossible at the moment.

+5
source

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


All Articles