Julia: Terse and comprehensible variable init

Created on 8 Sep 2017  路  7Comments  路  Source: JuliaLang/julia

Lets say I have a function that returns a tuple of two values

function foo()::Tuple{Int64, Float64}
 0, 1.0
end

I would like to be able to assign variables with type directly from this tuple. E. g.

local a::Int64, local b::Float64 = foo()

This is easier to understand, read and write than

local someTempVariable = foo()
local a::Int64 = someTempVariable[1]
local b::Float64 = someTempVariable[2]

Secondly. I would like to be able to declare my variables const, local and add a type e. g.

 const local a::Int64 = 100

This results in some arcane error telling me that a is already declared and defined and that I would redefine it. Which is obviously wrong.

error handling lowering

Most helpful comment

The syntax local (a::Int64, b::Float64) = foo() works.

All 7 comments

The syntax local (a::Int64, b::Float64) = foo() works.

Works for me with a single local and no parens as well: local x::Int, y::Float64 = (1, 2.5).

Thanks @JeffBezanson works perfectly. Did not know that syntax exists.

I still have the issue that const local a::Int64 = 100 throws an error ERROR: syntax: multiple type declarations for a

I also think this should not be an error.

function foo(bar::Int64)
    if 0 == bar
        local baaz::Int64 = 3
    elseif 1 == bar
        local baaz::Int64 = 4
    else
        local foo2::String = ""
    end
end

foo(0)

This tells me LoadError: syntax: local "baaz" declared twice, while it is clear that two baaz are distinct and should never leave their respective if-scope.

if doesn't begin a scope though, so the warning is correct.

Is there anything actionable here?

const local needs a different error message. For now it should just give the same "const not supported on locals" message that we use for other const declarations on locals.

Was this page helpful?
0 / 5 - 0 ratings