How to specify multiple return types of a function

Hi,

Specifying the return type of a function in Julia does not improve the code speed, but it can improve the clarity of the code.

In Julia documentation it is not explain how to give more than one return type to a function.

Is it possible ?

Thanks for your comments !

julia> function test(str::String)::String, ::Int
           r = split(str, r",")
           pat = r[1]
           nb = parse(Int,r[2])
           return pat, nb
       end
ERROR: syntax: unexpected ","
Stacktrace:
 [1] top-level scope
   @ none:1

julia> function test(str::String)::String ::Int
           r = split(str, r",")
           pat = r[1]
           nb = parse(Int,r[2])
           return pat, nb
       end
ERROR: syntax: invalid "::" syntax around REPL[10]:1
Stacktrace:
 [1] top-level scope
   @ REPL[10]:1

julia> function test(str::String)(::String ::Int)
           r = split(str, r",")
           pat = r[1]
           nb = parse(Int,r[2])
           return pat, nb
       end
ERROR: syntax: "::String" is not a valid function argument name around REPL[11]:1
Stacktrace:
 [1] top-level scope
   @ REPL[11]:1

julia> function test(str::String)
           r = split(str, r",")
           pat = r[1]
           nb = parse(Int,r[2])
           return pat, nb
       end
test (generic function with 1 method)

julia> test("A,12")
("A", 12)

Maybe

::Tuple{String,Int}

I think return values are just tuples

1 Like

Thanks ! it works !

julia> function test(str::String)::Tuple{String,Int}
           r = split(str, r",")
           pat = r[1]
           nb = parse(Int,r[2])
           return pat, nb
       end
test (generic function with 1 method)

julia> test("A,12")
("A", 12)
1 Like