我是朱莉娅的新手。这可能是一个愚蠢的问题,但是我似乎无法获得正确的语法。
我可以
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
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句