6

In the following the full context form of Test is not required as "Aaa`" is added to $ContextPath

Aaa`Test[x_] := x + 1;
context = "Aaa`";
PrependTo[$ContextPath, context];
Context@Test

(* "Aaa`" *)

The same however, doesn't seem to apply within scoping constructs.

Quit
Aaa`Test[x_] := x + 1;
With[{context = "Aaa`"},
 PrependTo[$ContextPath, context];
 Context@Test
 ]

(* "Global`" *)

Note that Quit is needed as the expected behaviour happens if evaluated twice. I find this surprising as it indicates an a priori parsing of the body within scoping constructs (the same applies if With is replaced by Block or Module) whereas I would have anticipated that this would be changeable along with normal control-flow expectations. Is this a bug or a feature?

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
Ronald Monson
  • 6,076
  • 26
  • 46

1 Answers1

2

The scoping construct is only relevant in so far as it groups the code into a singly parsed block. Hence, the same "unexpected" behaviour results from any type of grouping

(Aaa`Test[x_] := x + 1;
 context = "Aaa`";
 PrependTo[$ContextPath, context];
 Context@Test
 )

(* "Global`" *)

Hence, the WL possesses a parsing/evaluation sequence from input to input (line to line) giving a hybrid parsing/run-time evaluation sequence (n.b. loading the first snippet in a .m/.wl file produces identical output). If all needs to be contained in a given scope one workaround as per this answer is to delay the parsing with strings/boxes although probably easier is just to set up desired contexts in a previous input.

Ronald Monson
  • 6,076
  • 26
  • 46