How to define tuple of types in julia type declaration's

Viewed 269

How do I declare a tuple of specific types for a julia function?

This works:

function f(x, y)::Int8
    x+y
end

julia> f(2, 3)
  5

This works too:

function g(x, y)::Tuple
    x+y, x*y
end

julia> g(2, 3)
  (5, 6)

But I can't figure out how to define the types in the tuple.

For example, this throws an error:

function h(x, y)::Tuple(::Int8, ::Int8)
    x+y, x*y
end
  ERROR: syntax: invalid "::" syntax around REPL[48]:2

An this too:

function k(x, y)::Tuple(Int8, Int8)
    x+y, x*y
end

julia> k(2, 3)
  ERROR: MethodError: no method matching Tuple(::Type{Int8}, ::Type{Int8})
1 Answers

Use curly braces and omit the :: for the tuple's elements' types:

function k(x, y)::Tuple{Int8, Int8}
    x + y, x * y
end

julia> k(2, 3)
(5, 6)
Related