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