Julia语法-具有where子句的函数的返回类型注释

弹药筒

我是朱莉娅的新手。这可能是一个愚蠢的问题,但是我似乎无法获得正确的语法。

我可以

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

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

但是,如果我想同时包含类型注释和外部where子句,则会不断出现语法错误。以下似乎无效:

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)

这确实有效,但是我认为这不符合我想要的,因为类型参数是从方法主体中隐藏的(假设我想在其中使用它)

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

编写此代码的正确方法是什么?

谢谢

BogumiłKamiński

采用:

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

请注意,::Bool确实转换为Bool例如:

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

julia> check_condition(5,7)
true

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章