>
> ​I want the behavior seen in t with the macro. t2 with `isdefined` has the
> "wrong" and counterintuitive behavior.​


Please see the documentation for `isdefined`, which mentions that
`isdefined` only works at module scope:

http://docs.julialang.org/en/release-0.4/stdlib/base/#Base.isdefined

As far as why `isdefined` and `eval` don't do what you want (work in local
scope): this is an explicit design decision. See explanation here:

https://github.com/JuliaLang/julia/issues/2386#issuecomment-13966397

On Wed, Jul 20, 2016 at 9:10 AM, Christopher Rinderspacher <
[email protected]> wrote:

> Here's a session demonstrating what I see:
>
> julia> macro isDefined(x)
>           return :(defined = true; try eval($x); catch e; defined =
> !isa(e,UndefVarError); end; defined)
>        end
>
> julia> function t()
>           if !@isDefined(x)
>                println("x is not defined")
>           end
>           x = 1
>           if !@isDefined(x)
>               println("x should be defined.")
>           end
>        end
> t (generic function with 1 method)
>
> julia> t()
> x is not defined
>
> julia> function t2()
>           if !isdefined(:x)
>              println("x is not defined")
>           end
>           x = 1
>           if !isdefined(:x)
>              println("x is defined, but `isdefined` doesn't recognize it.")
>           end
>        end
> t2 (generic function with 1 method)
>
> julia> t2()
> x is not defined
> x is defined, but `isdefined` doesn't recognize it.
>
> julia>
>
> ​I want the behavior seen in t with the macro. t2 with `isdefined` has the
> "wrong" and counterintuitive behavior.​
> Since the macro is a product of trial and error (no pun intended), I can
> only rationalize its behavior.
>
>
>
> On Wed, Jul 20, 2016 at 8:37 AM, Yichao Yu <[email protected]> wrote:
>
>> On Wed, Jul 20, 2016 at 8:32 AM, Christopher Rinderspacher
>> <[email protected]> wrote:
>> > My solution right now is a macro
>> >
>> > macro isDefined(x)
>> >    return :(defined = true; try eval($x); catch e; defined =
>> > isa(e,UndefVarError); end; defined)
>>
>> It's unclear what you are trying to do here. The eval is likely a
>> no-op or not doing what you want. `$x` should probably be escaped.
>>
>> > end
>> >
>> > I find this to be very clunky, but it works for my limited test cases.
>> >
>> >> All compile-time undefined bindings become a runtime lookup from the
>> >> enclosing (i.e. module) scope. The undefined error means that this
>> lookup
>> >> failed.
>> >
>> > If that is so, what is the function I can call to determine whether my
>> > variable is in scope? `isdefined` is definitely _not_ that function.
>> How do
>> > I need to call that function from Julia? Your explanation is not a
>> solution
>> > to my problem.
>> >
>> >
>> >
>> > On Wed, Jul 20, 2016 at 12:01 AM, Yichao Yu <[email protected]> wrote:
>> >>
>> >> On Tue, Jul 19, 2016 at 11:54 PM, Isaiah Norton <
>> [email protected]>
>> >> wrote:
>> >> > Creating variables dynamically at local scope is not possible (if you
>> >> > are
>> >> > calling `eval` inside a function: it does not work like that!)
>> >> >
>> >> >> Since the runtime can determine that the local variable is
>> undefined as
>> >> >> evidenced by an appropriate error
>> >> >
>> >> >
>> >> > All compile-time undefined bindings become a runtime lookup from the
>> >> > enclosing (i.e. module) scope. The undefined error means that this
>> >> > lookup
>> >> > failed.
>> >>
>> >> We do have local only variables that can be undefined.
>> >>
>> >> >
>> >> >
>> >> > On Tue, Jul 19, 2016 at 10:43 PM, Christopher Rinderspacher
>> >> > <[email protected]> wrote:
>> >> >>
>> >> >> I, too, would like to be able to check whether local variables are
>> >> >> defined. In my case, I am parsing a file that doesn't allow
>> >> >> redefinition of
>> >> >> a field. The natural way to do this is to check whether I've
>> previously
>> >> >> assigned a variable with the content.
>> >> >>
>> >> >> The fact that isdefined only looks at the module scope is really
>> >> >> irrelevant to my use case. Since the runtime can determine that the
>> >> >> local
>> >> >> variable is undefined as evidenced by an appropriate error, I wish I
>> >> >> could
>> >> >> do the same within my function without some clunky try ... catch
>> >> >> mehcanism.
>> >>
>> >> A separate bool flag or a Nullable should work equally well.
>> >>
>> >> >>
>> >> >>
>> >> >> On Monday, May 11, 2015 at 7:31:06 PM UTC-4, Juha Heiskala wrote:
>> >> >>>
>> >> >>>  Hello,
>> >> >>>
>> >> >>> Am I missing something or doesn't isdefined detect local variables
>> of
>> >> >>> a
>> >> >>> function?
>> >> >>>
>> >> >>>
>> >> >>> julia> foo()= begin bar=1; isdefined(current_module(), :bar); end
>> >> >>> foo (generic function with 1 method)
>> >> >>>
>> >> >>> julia> foo()
>> >> >>> false
>> >> >>>
>> >> >>> Best Regards,
>> >> >>>
>> >> >>> Juha
>> >> >>>
>> >> >>> julia version 0.3.5
>> >> >>>
>> >> >>> julia> versioninfo()
>> >> >>> Julia Version 0.3.5
>> >> >>> Commit a05f87b* (2015-01-08 22:33 UTC)
>> >> >>> Platform Info:
>> >> >>>   System: Linux (x86_64-linux-gnu)
>> >> >>>   CPU: Intel(R) Core(TM) i7-3667U CPU @ 2.00GHz
>> >> >>>   WORD_SIZE: 64
>> >> >>>   BLAS: libopenblas (NO_LAPACK NO_LAPACKE DYNAMIC_ARCH NO_AFFINITY)
>> >> >>>   LAPACK: liblapack.so.3
>> >> >>>   LIBM: libopenlibm
>> >> >>>   LLVM: libLLVM-3.3
>> >> >
>> >> >
>> >
>> >
>>
>
>

Reply via email to