关于 Julia 类型语法的问题:为什么 Array{Int32, 1} <: Array{Integer, 1} 为假? [英] Question about Julia type syntax: Why is Array{Int32, 1} <: Array{Integer, 1} false?

查看:18
本文介绍了关于 Julia 类型语法的问题:为什么 Array{Int32, 1} <: Array{Integer, 1} 为假?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在朱莉娅中,

Array{Int32, 1} <: Array{Integer, 1} 

评估为 false,但是

Array{Int32, 1} <: (Array{T, 1} where T <: Integer)

评估为 true 因为 Int32 <: Integer 为真.

evaluates to true because Int32 <: Integer is true.

在我看来,第一个和第二个表达式传达了相同的想法,并且应该等效地评估.此外,第一个表达式不太混乱.为什么 Julia 的语法将前者评估为假,而将后者评估为真,有什么原因吗?这种行为是否有一些深刻而有益的东西,或者这是对数组/类型系统开发方式的疏忽?

In my mind, the first and second expressions communicate the same idea and should evaluate equivalently. Furthermore, the first expression is less cluttered. Is there a reason why Julia's syntax evaluates the former as false but the latter as true? Is there something deep and good about this behavior or is this an oversight in how Arrays/the type system were developed?

推荐答案

Array{Integer, 1}Array{T, 1} 的主要区别 where T <: Integer 是前者是具体类型,后者是抽象类型.之所以会有所不同,是因为您可以创建一个类型为 Array{Integer, 1} 的变量.这是一个潜在的异构数组,因此必须将其实现为指针数组(如此缓慢且堆分配).考虑到这一点,为什么 Array{Int32, 1} <: Array{Integer, 1}=false 就很清楚了.如果我们为特定类型 Array{Integer, 1} 编写一个方法,它不能被特化,因为它已经是一个具体类型,并且当我们在 上运行它时会出现段错误Array{Int32, 1} 具有完全不同的数据格式(内联元素).

The key difference between Array{Integer, 1} and Array{T, 1} where T <: Integer is that the former is a concrete type, while the later is an abstract type. The reason this makes a difference is that you can make a variable with type Array{Integer, 1}. This is a potentially heterogeneous array, so it has to be implemented as an array of pointers (so slow and heap allocated). With this in mind, it is clear why Array{Int32, 1} <: Array{Integer, 1}=false. If we write a method for the specific type Array{Integer, 1}, that can't be specialized since it is already a concrete type, and we would segfault when we run it on an Array{Int32, 1} which has a completely different data format (inline elements).

这篇关于关于 Julia 类型语法的问题:为什么 Array{Int32, 1} &lt;: Array{Integer, 1} 为假?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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