Reifying local scopes is a feature that dynamic languages often allow as an
artifact of the most obvious implementation of scopes in low-performance
interpreters: as dictionaries from symbols to values. Exposing this,
however, effectively prevents any other possible implementation. In
particular, compilers don't construct dictionaries like this – because it
would be ridiculously slow. Instead, they translate the data flow expressed
by the programmer into machine instructions at a level where variables
don't exist any more. This is one of those dynamic features whose absence
allows Julia to be fast.

On Thu, Jan 28, 2016 at 10:28 AM, Fady Shoukry <[email protected]>
wrote:

>
>
> On Thursday, 28 January 2016 08:12:06 UTC-5, Stefan Karpinski wrote:
>>
>> Please try to avoid this business of creating the same conversation in
>> three places.
>>
>> On Thu, Jan 28, 2016 at 8:10 AM, Stefan Karpinski <[email protected]>
>> wrote:
>>
>>>
>>> http://stackoverflow.com/questions/35051773/create-local-variables-programmatically-from-a-dictionary-in-julia
>>>
>>> On Thu, Jan 28, 2016 at 8:10 AM, Stefan Karpinski <[email protected]>
>>> wrote:
>>>
>>>> https://groups.google.com/forum/#!topic/julia-users/axwQqFeIlCQ
>>>>
>>>> On Wed, Jan 27, 2016 at 11:05 PM, Yichao Yu <[email protected]> wrote:
>>>>
>>>> On Wed, Jan 27, 2016 at 9:47 PM, Fady Shoukry <[email protected]>
>>>>> wrote:
>>>>> >
>>>>> >>
>>>>> >> Depending on what API JuMP provide and what API you want to
>>>>> provide, I
>>>>> >> think you should either keep the dict (if JuMP can handle it) or use
>>>>> >> meta programing to construct an AST/function based on the user input
>>>>> >> and evaluate that.
>>>>> >
>>>>> >
>>>>> > Thanks for the quick reply. Could you elaborate on that last part
>>>>> regarding
>>>>> > constructing an AST/function (with an example if possible)?
>>>>> >
>>>>>
>>>>> A simple example that hopefully shows what I meant,
>>>>>
>>>>>
>>>>> ```
>>>>> julia> vars = [:a, :b]
>>>>> 2-element Array{Symbol,1}:
>>>>>  :a
>>>>>  :b
>>>>>
>>>>> julia> expr = :(a + b)
>>>>> :(a + b)
>>>>>
>>>>> julia> function make_function(name, vars, expr)
>>>>>            :(function $name()
>>>>>                $([:($v = 1) for v in vars]...)
>>>>>                $expr
>>>>>              end)
>>>>>        end
>>>>> make_function (generic function with 1 method)
>>>>>
>>>>> julia> func_ast = make_function(:new_function, vars, expr)
>>>>> :(function new_function() # none, line 3:
>>>>>         a = 1
>>>>>         b = 1 # none, line 4:
>>>>>         a + b
>>>>>     end)
>>>>>
>>>>> julia> eval(func_ast)
>>>>> new_function (generic function with 1 method)
>>>>>
>>>>> julia> new_function()
>>>>> 2
>>>>>
>>>>> julia> @code_warntype new_function()
>>>>> Variables:
>>>>>   a::Int64
>>>>>   b::Int64
>>>>>
>>>>> Body:
>>>>>   begin  # none, line 3:
>>>>>       a = 1
>>>>>       b = 1 # none, line 4:
>>>>>       return (Base.box)(Int64,(Base.add_int)(a::Int64,b::Int64))
>>>>>   end::Int64
>>>>> ```
>>>>>
>>>>
>>>>
>>>
>>
> Apologies, the second Google groups post was posted by mistake and is
> incomplete and the stackoverflow question (by a colleague of mine) was in
> hopes to get as many opinions as possible, everyone is very responsive and
> see I see that was unnecessary, my bad.
>
> Thanks for the valuable input, I will look further into the JuMP API as
> per your comment. Otherwise, it saves time to know that this is not doable
> by design. Just out of curiosity, is there a specific reason why this is
> not a feature you would want to allow?
>

Reply via email to