-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
LinRange{Int}(...)
allows creating a LinRange that doesn't really work
#35219
Comments
This is probably not fixable with the current type system. The issue is you need julia> struct MyLinRange{T} <: AbstractRange{typeof(zero(T)/1)}
start::T
stop::T
len::Int
lendiv::Int
end
ERROR: MethodError: no method matching zero(::TypeVar)
Closest candidates are:
zero(::Type{Missing}) at missing.jl:103
zero(::Type{LibGit2.GitHash}) at /home/tim/src/julia-1/usr/share/julia/stdlib/v1.4/LibGit2/src/oid.jl:220
zero(::Type{Pkg.Resolve.VersionWeight}) at /home/tim/src/julia-1/usr/share/julia/stdlib/v1.4/Pkg/src/Resolve/versionweights.jl:15
...
Stacktrace:
[1] top-level scope at REPL[1]:1 Alternatives include adding a second type parameter, julia> struct MyLinRange{Item,T} <: AbstractRange{T}
start::Item
stop::Item
len::Int
lendiv::Int
function MyLinRange{Item,T}(start,stop,len) where {Item,T}
len >= 0 || throw(ArgumentError("range($start, stop=$stop, length=$len): negative length"))
if len == 1
start == stop || throw(ArgumentError("range($start, stop=$stop, length=$len): endpoints differ"))
return new(start, stop, 1, 1)
end
new(start,stop,len,max(len-1,1))
end
end
julia> MyLinRange{Item}(start, stop, len) where Item = MyLinRange{Item,typeof(start/1)}(start, stop, len) but I am not sure whether this would be regarded as too breaking. Note that this issue doesn't come up with |
The elements of
|
Oh, right, I had forgotten about that. That's a relief. So we should just fix the printing. |
In addition to improving the printing, one could also throw an error when creating a |
Yes, I think that would be the right way to do it. (The type parameter can be set by a constructor, and invalid constructor calls can be errors.) |
The misleading printing is fixed by #35267. |
LinRange{Int}(...)
allows creating a LinRange that doesn't really work
Triage thinks this is fixed now. If you explicitly ask for |
eltype(LinRange{Int}(1, 4, 5))
isInt
, whereas the elements are actually of typeFloat64
, sinceLinRange
uses/
for division. Furthermore,LinRange{Int}(1, 4, 5) isa AbstractRange{Int}
is also technically wrong, since the docstring ofAbstractRange
clearly states:Thanks to @asinghvi17 for bringing this up on Slack!
The text was updated successfully, but these errors were encountered: