{} in function names

I see many functions or constructors with {} in their names.

julia> NTuple{4,Int64}((1,2,3,4))
(1, 2, 3, 4)

julia> function myfun{2}(x,y)
       return x + y
ERROR: UndefVarError: `myfun` not defined
 [1] top-level scope
   @ REPL[62]:1

But as I tested, {} are not allowed in function names?

Hi there!
These are not really standard functions, they are parametric constructors for parametric types.


Another way to look at constructors is as function-like objects: Methods · The Julia Language

That is, any object may be made callable in Julia by giving it a method; giving a type a method creates a constructor. For example, these two define the same method (the second line overwrites the method defined in the first line):

NTuple{4,Int64}(x::X) = ...
(::Type{NTuple{4,Int64}})(x::X) = ...

PR to mention this on the Manual page for constructors: doc: manual: constructors: note the correspondence with callable objs by nsajko · Pull Request #53051 · JuliaLang/julia · GitHub

1 Like