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)

写这个的正确方法是什么?

谢谢

回答

用:

(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


以上是Julia语法-带有where子句的函数的返回类型注释的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>