Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@u_str macro hygiene? #272

Closed
goretkin opened this issue Sep 30, 2019 · 1 comment · Fixed by #298
Closed

@u_str macro hygiene? #272

goretkin opened this issue Sep 30, 2019 · 1 comment · Fixed by #298

Comments

@goretkin
Copy link

I'm not sure if this is intentional or not, but I expected it to be sufficient to just import Unitful: @u_str in order to use e.g. u"m".

julia> import Unitful: @u_str

julia> u"m"
ERROR: LoadError: Symbol `m` was found in unit module Unitful, but was not loaded into Main. Consider `using Unitful` within `Main`?
Stacktrace:
 [1] error(::String) at ./error.jl:33
 [2] replace_value(::Module, ::Symbol) at /Users/goretkin/.julia/packages/Unitful/ytsW0/src/user.jl:550
 [3] @u_str(::LineNumberNode, ::Module, ::Any) at /Users/goretkin/.julia/packages/Unitful/ytsW0/src/user.jl:505
in expression starting at REPL[4]:1

julia> import Unitful: m

julia> u"m"
ERROR: LoadError: Symbol `m` was found in unit module Unitful, but was not loaded into Main. Consider `using Unitful` within `Main`?
Stacktrace:
 [1] error(::String) at ./error.jl:33
 [2] replace_value(::Module, ::Symbol) at /Users/goretkin/.julia/packages/Unitful/ytsW0/src/user.jl:550
 [3] @u_str(::LineNumberNode, ::Module, ::Any) at /Users/goretkin/.julia/packages/Unitful/ytsW0/src/user.jl:505
in expression starting at REPL[6]:1

julia> using Unitful

julia> u"m"
m

julia> m = "garbage"
ERROR: cannot assign a value to variable Unitful.m from module Main
Stacktrace:
 [1] top-level scope at REPL[9]:1
@c42f
Copy link
Contributor

c42f commented Dec 9, 2019

This is a consequence of the way that Unitful allows u_str to be dynamically aware of symbols from unit extension modules, but without breaking precompilation; the heuristic is that there must be a binding for the extension module available.

Perhaps we could do better, but for now the workaround for this is to make sure there's a binding for Unitful itself in Main:

using Unitful: Unitful, @u_str

c42f added a commit to c42f/Unitful.jl that referenced this issue Jan 17, 2020
Generalize the handling of `@u_str` and the unit extension module
context to allow the same machinery to be used for a new `parseunit`
function which does the same thing at runtime.

Fixes PainterQubits#272 as a side effect of generalizing `@u_str`.
ajkeller34 pushed a commit that referenced this issue Jan 25, 2020
* Parsing of units from strings

Generalize the handling of `@u_str` and the unit extension module
context to allow the same machinery to be used for a new `parseunit`
function which does the same thing at runtime.

Fixes #272 as a side effect of generalizing `@u_str`.

* Rename parseunits -> uparse

Also add a test for #272 

* Make unit_context a keyword
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants