kjh
kjh

Reputation: 3411

How does dart implement library privacy under the hood?

When a library name is prefixed with a '_' it becomes private to its library. How does the dart vm actually implement this privacy?

Upvotes: 2

Views: 102

Answers (2)

lrn
lrn

Reputation: 71813

Internally, the VM suffixes the variable name with a string that is unique to the library, so _foo might become _foo@1238fa12. The @ ensures that it can't collide with a user variable because @ isn't valid in a normal identifier. After appending the string, the VM can treat the variable as a completely normal variable, and because there is no other library that can possibly have a reference to the variable, it is effectively library private (although there are probably a few corner cases where it needs to be more clever about private names).

That's not the only possible implementation strategy, but private variables were designed from the beginning to make this strategy possible.

Upvotes: 5

Günter Zöchbauer
Günter Zöchbauer

Reputation: 657909

As far as I know are the names prefixed with a random string which is unique per library.

Upvotes: 0

Related Questions