julia-0.6 函数定义中的“where" [英] `where` in function definitions in julia-0.6
问题描述
我正在尝试了解 Julia v0.6 中的新类型系统,基于阅读 发行说明.
I am trying to understand the new type system in Julia v0.6, based on reading the release notes.
谁能告诉我
inv(M::Matrix{T}) where T <: AbstractFloat
给了我与使用经典不同的地方
gives me which is different from using the classic
inv{T<:AbstractFloat}(M::Matrix{T})
?
推荐答案
新语法的含义相同,但可以在更多情况下使用,可以表达更多的构造,并消除许多概念上的歧义,尤其是围绕参数构造函数.旧语法将在 0.6 中弃用,一些旧语法将在 1.0 中以不同的含义回收.从根本上说,F{T}(args...)
的问题在于 F{T}
部分在概念上是模棱两可的——解析器知道它的含义,但它是经常让人困惑:
The new syntax means the same thing but can be used in more circumstances and can express more constructs and eliminates a number of conceptual ambiguities, especially surrounding parametric constructors. The old syntax will be deprecated in 0.6 and some of the old syntax will be reclaimed with different meaning in 1.0. Fundamentally, the problem with F{T}(args...)
is that the F{T}
part is conceptually ambiguous – the parser knows what it means, but it's often confusing to humans:
单独
F{T}
表示带类型参数T
的参数类型F
.
In isolation
F{T}
means the parametric typeF
with type parameterT
.
后跟括号,不作为方法定义的一部分,F{T}(args...)
表示应用类型 F{T}
将参数 args...
作为一个函数,通常构造一个 F{T}
类型的实例.
Followed by parens, not as part of a method definition, F{T}(args...)
means to apply the type F{T}
to the arguments args...
as a function, typically constructing an instance of the type F{T}
.
后跟parens和equals,即作为F{T}(args...) = expr
中方法定义的一部分,意思是为F
作为函数,带有类型参数 T
形式参数 args...
和定义 expr
.
Followed by parens and equals, i.e. as part of a method definition as in F{T}(args...) = expr
, it means to define a method for F
as a function, with type parameters T
formal arguments args...
and definition expr
.
特别是,其中任何一个都没有语法:
In particular, there is no syntax for either of these:
为
F{T}
添加一个方法,用于当前范围内T
的具体值.
Adding a method to
F{T}
for the concrete value ofT
in the current scope.
为每个参数值T
添加一个方法到F{T}
.
Adding a method to F{T}
for each parametric value T
.
这种情况导致 Julia 0.5 和之前的构造函数语法比必要的更加混乱和不直观.在 Julia 1.0 中,类型参数和构造函数将更加直观和一致,遵循以下原则:
This situation causes constructor syntax in Julia 0.5 and prior to be more confusing and unintuitive than necessary. In Julia 1.0 type parameters and constructors will be both more intuitive and consistent, following these principles:
- 用于定义方法的语法始终与用于调用它的语法相匹配.
F{T}
语法总是引用带有参数T
的类型F
.- 类型参数总是由
where
子句引入.
- The syntax used to define a method always matches the syntax used to call it.
- The
F{T}
syntax always refers to the typeF
with parameterT
. - Type parameters are always introduced by
where
clauses.
当 0.6 发布时,将会有更详细的更改解释,可能在一篇关于 0.6 版本亮点的博客文章中.
There will be a more detailed explanation of the changes when 0.6 comes out, probably in a blog post on highlights of the 0.6 release.
这篇关于julia-0.6 函数定义中的“where"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!