Understanding type declarations

19 views
Skip to first unread message

Miguel Bazdresch

unread,
Apr 13, 2012, 9:41:11 AM4/13/12
to juli...@googlegroups.com
Hi,

I'm working on adding argument validation to Gaston; I want to make it as hard as possible to get gnuplot errors or warnings from Gaston commands. Since I'm learning Julia in the process, I wondered how much the type system could help me to achieve this. Reading the manual, I found this in the "types" section, regarding the :: operator:

    The “declaration” behavior only occurs in specific contexts:

    x::Int8        # a variable by itself
    x::Int8 = 10   # as the left-hand side of an assignment

However, trying this out in Julia:

    julia> x::Int
    x not defined
    julia> x::Int8 = 10
    x not defined
    julia> x = 1
    1
    julia> x::Int
    1
    julia> x::Int = "boo"
    "boo"

However, this seems to work:

    julia> function foo()
           x::Int
           x = "boo"
           end
   
    julia> foo()
    no method convert(Type{Int64},ASCIIString)
     in method_missing at base.jl:60
     in foo at prompt:3
   
Why does :: seem to work for declarations inside functions, but not outside them?

-- mb

Stefan Karpinski

unread,
Apr 13, 2012, 10:16:52 AM4/13/12
to juli...@googlegroups.com
I'm pretty sure the compiler doesn't yet support type declarations on globals. Jeff can elaborate.
Reply all
Reply to author
Forward
0 new messages