我是朱莉娅的新手。这可能是一个愚蠢的问题,但我似乎无法正确理解此问题的语法。
我可以
check_condition(func::F, arg::Int) where {F} = func(arg)
Run Code Online (Sandbox Code Playgroud)
和
check_condition(func::Function, arg::Int)::Bool = func(arg)
Run Code Online (Sandbox Code Playgroud)
但是如果我想同时包含类型注释和外部 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)
Run Code Online (Sandbox Code Playgroud)
这确实有效,但我相信它不等于我想要的,因为类型参数从方法体中隐藏(假设我想在某个地方使用它)
check_condition(func::F where {F}, arg::Int)::Bool = func(arg)
Run Code Online (Sandbox Code Playgroud)
写这个的正确方法是什么?
谢谢
用:
(check_condition(func::F, arg::Int)::Bool) where {F} = ...
Run Code Online (Sandbox Code Playgroud)
请注意,::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
Run Code Online (Sandbox Code Playgroud)