Closed mitchellwrosen closed 3 months ago
Very cool! Can you have multiple namespace directives in a file?
@hojberg You cannot – zero or one
@pchiusano I looked into this briefly, it really isn't more complicated than we're now parsing namespace
as a Reserved
lexeme rather than an Identifier
lexeme, and go down different error paths from there. This could/should be cleaned up for sure, but I'm inclined to leave it alone for now as it also affects other reserved keywords in the same way.
@mitchellwrosen cool. Yeah I wouldn’t sweat it for this PR then.
Overview
This PR implements the "namespace directive" feature: a Unison file can contain an optional
namespace foo
line that affects parsing as follows:That is,
is equivalent to
Note that the existence of a namespace directive therefore prevents one from referring to a name outside of the file whose suffix matches something in the file.
For example, without a namespace directive, I could write
and refer to both my locally-bound
foo.factorial
as well as a term literally namedfactorial
(no other prefix) in the namespace. (N.B that's not true in trunk today, but it will be fixed soon: https://github.com/unisonweb/unison/issues/5268).However, with a namespace directive...
there's no way to write the same function (because
factorial
resolves to the locally-boundfactorial
, which will be expanded tofoo.factorial
during parsing).For this reason, it would be unsafe to implement
edit.namespace foo
naively by putting anamespace foo
directive at the top of the block.Test coverage
I've added a transcript to demonstrate the feature.