Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
84 views
in Technique[技术] by (71.8m points)

`where` in function definitions in julia-0.6

I am trying to understand the new type system in Julia v0.6, based on reading the release notes.

Can anyone tell me what

inv(M::Matrix{T}) where T <: AbstractFloat

gives me which is different from using the classic

inv{T<:AbstractFloat}(M::Matrix{T})

?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

  • In isolation F{T} means the parametric type F with type parameter 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}.

  • 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:

  • Adding a method to F{T} for the concrete value of T in the current scope.

  • Adding a method to F{T} for each parametric value T.

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:

  • 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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...