julia-0.6中的函数定义中的"where" [英] `where` in function definitions in julia-0.6

查看:108
本文介绍了julia-0.6中的函数定义中的"where"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基于阅读谁能告诉我什么

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 type F with type parameter T.

紧随其后的是括号,而不是方法定义的一部分,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}.

后跟括号和等于,即作为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 of T in the current scope.

为每个参数值TF{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 type F with parameter T.
  • 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆