Julia syntax - Return type annotation for a function with a where clause

Viewed 158

I'm new to Julia. This may be a stupid question, but I can't seem to get the syntax for this right.

I can do

check_condition(func::F, arg::Int) where {F} = func(arg)

and

check_condition(func::Function, arg::Int)::Bool = func(arg)

But if I want to include both the type annotation and the external where clause I keep getting syntax errors. The following don't seem to work:

check_condition(func::F, arg::Int) where {F}::Bool = func(arg)
(check_condition(func::F, arg::Int) where {F})::Bool = func(arg)
check_condition(func::F, arg::Int)::Bool where {F} = func(arg)
check_condition::Bool(func::F, arg::Int) where {F} = func(arg)

This does work, but I believe it is not equivalent to what I want, since the type parameter is hidden from the method body (assume I want to use it somewhere within)

check_condition(func::F where {F}, arg::Int)::Bool = func(arg)

What is the proper way of writing this?

Thanks

1 Answers

use:

(check_condition(func::F, arg::Int)::Bool) where {F} = ...

Note that ::Bool does conversion to Bool. E.g.:

julia> (check_condition(func::F, arg::Int)::Bool) where {F}= 1
check_condition (generic function with 1 method)

julia> check_condition(5,7)
true
Related