this form implicitly makes f a const, generic function
function f(i); i; end
this form is exactly equivalent to the compiler:
f(i) = i
this form explicitly makes f a const, anonymous function:
const f = function(i); i; end
this form is exactly equivalent to the compiler:
const f = (i) -> i
I don't recommend writing code like this. You will confuse yourself
and make your boss angry:
julia> f = function(i)
global f = function(j)
j/2
end
i
end
julia> [f(i) for i = 1:3]
{1 1.0 1.5}
(the additional assumption required is that all code and code paths
are currently visible to the compiler and that nothing new will be
added and that furthermore, you will refrain from using certain OO
features like setfield on a Module -- e.g. that f is provably
constant)
On Sun, Feb 16, 2014 at 10:52 PM, Jake Bolewski <[email protected]> wrote:
> You are pointing out differences in the parsed AST. As Jameson said the two
> forms are equivent and will get lowered by the compiler to the same code. I
> suggest browsing though Julia's type inference / compiler code to get a
> better overview of how things work under the hood.
>
> Best,
> Jake
>
> On Sunday, February 16, 2014 10:16:28 PM UTC-5, Fil Mackay wrote:
>>
>> On Mon, Feb 17, 2014 at 1:02 PM, Jameson Nash <[email protected]> wrote:
>>>
>>> Fil, your second form of writing the function is functionally
>>> identical to the first -- it is only a difference in syntax and makes
>>> absolutely no difference to the compiler.
>>
>>
>> There are differences to the compiler:
>>
>> julia> dump(:(x->x))
>> Expr
>> head: Symbol ->
>> args: Array(Any,(2,))
>> 1: Symbol x
>> 2: Expr
>> head: Symbol block
>> args: Array(Any,(2,))
>> 1: Expr
>> head: Symbol line
>> args: Array(Any,(2,))
>> typ: Any
>> 2: Symbol x
>> typ: Any
>> typ: Any
>>
>> julia> dump(:(function (x); x; end))
>> Expr
>> head: Symbol function
>> args: Array(Any,(2,))
>> 1: Expr
>> head: Symbol tuple
>> args: Array(Any,(1,))
>> 1: Symbol x
>> typ: Any
>> 2: Expr
>> head: Symbol block
>> args: Array(Any,(2,))
>> 1: Expr
>> head: Symbol line
>> args: Array(Any,(2,))
>> typ: Any
>> 2: Symbol x
>> typ: Any
>> typ: Any
>>
>> These differences (-> / function) could make a real difference to type
>> inference, and seem to from the local/const examples.