Hacker News new | ask | show | jobs
by Sukera 1403 days ago
> I have to annotate all structs and function input types.

Can you elaborate on that? Argument types in function definitions do not help the compiler infer types - you can leave them completely untyped. It's really only required in struct fields, so that the compiler can know the size of your struct and doesn't have to box everything.

2 comments

It's not even needed in struct fields. If Python, Matlab and R are viable alternatives, then surely OP does not care about performance and can choose a completely dynamic style of Julia.

(I don't recommend it though, having some idea of type safety is in general a good idea)

My bad, I didn't express myself properly. The Julia language requires structs to be annotated and only those, you are right. Then if you want to reach a primitive level of type safety, function input types have to be annotated.
No, it does not:

    julia> struct Foo
               a
           end
    
    julia> Foo(17)
    Foo(17)
    
    julia> Foo("bar")
    Foo("bar")
People should absolutely criticise Julia for its faults, but I would expect them to at least learn and understand the very basics of the language before doing so.

Structs and functions do not need to have type annotations. They are completely optional and learning when to annotate is one of the first learning challenges for newcomers to the language.

There are code generation advantages to type annotations for structs, but they are not required. As for functions, this will only ever be the case if type inference fails, which should be so rare that you hardly even need to consider it.

This doesn't match with my experience. Julia is really excellent at infering complicated types. What you might be running into is type ambiguity though where Julia has to resort to unions because that's what the loops needs to work. Have a look at @code_warntype of some function call to see if that is the problem.
Ah, I see, you're referring to linting capabilities. Yes, that's unfortunately a current limitation of LanguageServer.jl right now. I don't think it interfaces extensively with the compiler for type inference in a way that it crossreferences callers in the current code base, though it certainly could. That capability just hasn't been implemented yet I think.

Regardless, that's orthogonal to being type safe.