`where {t :< 是什么意思?Integer}` 在 Julia 函数声明的上下文中是什么意思?

编程入门 行业动态 更新时间:2024-10-28 08:19:39
本文介绍了`where {t :< 是什么意思?Integer}` 在 Julia 函数声明的上下文中是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在其他人的代码中看到了以下内容,但我从未用自己的代码编写过,因为我不明白发生了什么.

I have seen the following in other people's code, but I've never written it in my own since I didn't understand what was happening.

举个例子:

函数 add(x::T, y::T) 其中 {T :<整数}

我猜测 T 被转换为整数,然后用于显式键入 x 和 y.但是为什么不直接做 x::Int64 呢?在哪里 {T :<Integer} 允许任何 Int 类型,例如 Int32 和 Int64?

I am guessing that T is being cast as an Integer and then used to explicitly type x and y. But why not just do x::Int64? Does where {T :< Integer} allow for any Int type like Int32 and Int64?

推荐答案

稍微扩展一下 Oscar 的回答:

To expand a little on Oscar's answer:

使用 function add(x::T, y::T) where {T :<Integer} 允许您将 参数方法 添加到函数 add(x, y).您可以在 参数方法.

Using function add(x::T, y::T) where {T :< Integer} allows you to add a parametric method to the function add(x, y). You can read up on this in more detail in the Julia documentation under Parametric Methods.

这有两大优势:它允许您定义合理的通用方法(因为在许多情况下,整数的确切类型不会真正影响函数定义).同时它允许您将调用限制为相同类型的 x, y 对,这可以提高类型稳定性并导致更高效的编译代码.

This comes with two big advantages: It allows you to define reasonably general methods (since in many cases the exact type of integer would not really affect the function definition). Simultaneously it allows you to restrict the call to x, y pairs of the same type, which can improve type stability and lead to more efficient compiled code.

更多推荐

`where {t :&lt; 是什么意思?Integer}` 在 Julia 函数声明的上下文中是什么意思?

本文发布于:2023-10-08 14:14:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1472867.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文中   函数   声明   lt   amp

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!