yjbanov / dart-const-functions

[WORK IN PROGRESS] DEP: const functions and methods for Dart
Apache License 2.0
2 stars 0 forks source link

relax transform-to-existing syntax requirement to accommodate proper visibility #3

Open yjbanov opened 9 years ago

yjbanov commented 9 years ago

from @munificent on misc:

Doing this correctly requires more than a simple textual substitution. The namespace of the code where a const function is declared may be different from where it's invoked. Consider:

// fn.dart
const name = "I'm from fn.dart";

const fn() => name;

// main.dart
import 'fn.dart';

const name = "Oops, I'm from main.dart";

@SomeAnnotation(fn())
main() { ... }

If you just expand the fn() invocation to its body, it's not going to do the right thing. Scope is hard. Of course, this can be implemented correctly, but doing so starts to look a lot more like a little term rewriting language interpreter than like the simple text preprocessor you're going for.

yjbanov commented 9 years ago

from @lrhn from the same thread:

True, you can't just move the const expression - it might depend on private const variables from its original scope, nothing will make that work. However, it's just a matter of how deeply you need to expand the const expression before all the variables are either basic values or visible from where it's expanded. Worst case, it will expand to a complete const expression with no variables.

Internally in a compiler, scope should not be a problem, you can probably just refer to the const value.