Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inside type definition is reserved

Tags:

julia

The code that worked in 0.3:

type foo
    bar::Int = 0
end

After migrating to Julia 0.4- produces errors like

julia4 test.jl
ERROR: LoadError: syntax: "bar::Int=0" inside type definition is reserved
 in include at ./boot.jl:254
 in include_from_node1 at loading.jl:133
 in process_options at ./client.jl:306
 in _start at ./client.jl:406

What does the error mean? How to fix it in 0.4-?

NB

I understand that it is a dev version. I also did googled and consulted the manual http://julia.readthedocs.org/en/latest/manual/types/

like image 426
Yauhen Yakimovich Avatar asked Dec 10 '22 22:12

Yauhen Yakimovich


1 Answers

Ok, the thing I come up with is to use the constructor called new() and a function (in type) with default (python/haskell-like) parameter values:

struct Foo
    bar::Int

    function Foo(bar=0)
        new(bar)
    end
end


x = Foo()

A shorter syntactical version is (thnx @ivarne)

struct Foo
    bar::Int

    Foo(bar=0) = new(bar)
end
like image 187
Yauhen Yakimovich Avatar answered Dec 23 '22 23:12

Yauhen Yakimovich