Skip to content

Commit edbcb79

Browse files
committed
some type safety for Fix
Disallows any type `Fix{x}` where `!(x isa Int) || (x < 0)`. Follows up on PR JuliaLang#54653. The goals are similar as in the closed PR JuliaLang#56518, but less ambitious. I hope it's not too late in the release cycle to consider this change as it seems not to be invasive or disruptive.
1 parent baf8124 commit edbcb79

File tree

2 files changed

+9
-10
lines changed

2 files changed

+9
-10
lines changed

base/operators.jl

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1170,17 +1170,15 @@ A type representing a partially-applied version of a function `f`, with the argu
11701170
`Fix{1}(Fix{2}(f, 4), 4)` fixes the first and second arg, while `Fix{2}(Fix{1}(f, 4), 4)`
11711171
fixes the first and third arg.
11721172
"""
1173-
struct Fix{N,F,T} <: Function
1173+
struct Fix{N,F,T,NTuple{N,Nothing}<:NTup<:NTuple{N,Nothing}} <: Function
11741174
f::F
11751175
x::T
11761176

11771177
function Fix{N}(f::F, x) where {N,F}
1178-
if !(N isa Int)
1179-
throw(ArgumentError(LazyString("expected type parameter in `Fix` to be `Int`, but got `", N, "::", typeof(N), "`")))
1180-
elseif N < 1
1181-
throw(ArgumentError(LazyString("expected `N` in `Fix{N}` to be integer greater than 0, but got ", N)))
1178+
if N < 1
1179+
throw(ArgumentError("expected `N` in `Fix{N}` to be integer greater than 0, but got 0"))
11821180
end
1183-
new{N,_stable_typeof(f),_stable_typeof(x)}(f, x)
1181+
new{N,_stable_typeof(f),_stable_typeof(x),NTuple{N,Nothing}}(f, x)
11841182
end
11851183
end
11861184

@@ -1196,12 +1194,12 @@ end
11961194
"""
11971195
Alias for `Fix{1}`. See [`Fix`](@ref Base.Fix).
11981196
"""
1199-
const Fix1{F,T} = Fix{1,F,T}
1197+
const Fix1{F,T} = Fix{1,F,T,NTuple{1,Nothing}}
12001198

12011199
"""
12021200
Alias for `Fix{2}`. See [`Fix`](@ref Base.Fix).
12031201
"""
1204-
const Fix2{F,T} = Fix{2,F,T}
1202+
const Fix2{F,T} = Fix{2,F,T,NTuple{2,Nothing}}
12051203

12061204

12071205
"""

test/functional.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,9 +348,10 @@ end
348348
@test f() == 'a'
349349
end
350350
@testset "Dummy-proofing" begin
351+
@test_throws Exception Fix{-1}
351352
@test_throws ArgumentError("expected `N` in `Fix{N}` to be integer greater than 0, but got 0") Fix{0}(>, 1)
352-
@test_throws ArgumentError("expected type parameter in `Fix` to be `Int`, but got `0.5::Float64`") Fix{0.5}(>, 1)
353-
@test_throws ArgumentError("expected type parameter in `Fix` to be `Int`, but got `1::UInt64`") Fix{UInt64(1)}(>, 1)
353+
@test_throws TypeError Fix{0.5}
354+
@test_throws TypeError Fix{UInt64(1)}
354355
end
355356
@testset "Specialize to structs not in `Base`" begin
356357
struct MyStruct

0 commit comments

Comments
 (0)