JuliaLang / JuliaSyntax.jl

The Julia compiler frontend
Other
267 stars 32 forks source link

`import as if` #384

Open ParadaCarleton opened 8 months ago

ParadaCarleton commented 8 months ago

Not sure if this is supposed to be allowed, or if it's supposed to be an error (in which case I think it would be better to error when importing).

julia> import InverseFunctions as if

julia> if.square(sqrt(2))
ERROR: ParseError:
# Error @ REPL[26]:1:3
if.square(sqrt(2))
# ╙ ── invalid identifier
Stacktrace:
 [1] top-level scope
   @ none:1
c42f commented 8 months ago

This was allowed by the previous parser so removing it would be technically breaking:

julia> dump(JuliaSyntax.fl_parse("import InverseFunctions as if"))
Expr
  head: Symbol import
  args: Array{Any}((1,))
    1: Expr
      head: Symbol as
      args: Array{Any}((2,))
        1: Expr
          head: Symbol .
          args: Array{Any}((1,))
            1: Symbol InverseFunctions
        2: Symbol if

The import currently does work "as expected" in some technical sense. It's just that if is not a valid identifier and must be written as var"if" when it's used later in the code. For example:

julia> import Unicode as if

julia> var"if".graphemes
graphemes (generic function with 2 methods)
c42f commented 7 months ago

On further consideration, I do agree that this is confusing, and ideally could be deprecated in future as a warning, then an error. The amount of breaking would need to be assessed, but would probably be low.

For readability, it would be better to force the user to write the following if they really want bizarre identifier names:

import Unicode as var"if"
pfitzseb commented 7 months ago

My two cents are that we should treat this as a bug/unexepected behaviour in the flisp parser.