Jonas
Jonas

Reputation: 1529

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

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})

Upvotes: 3

Views: 920

Answers (1)

Djaouad
Djaouad

Reputation: 22766

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)

Upvotes: 7

Related Questions