Skip to content

Commit 09e2d62

Browse files
authored
breaking: make the typed_callable arguments order follow the Manual (#13)
Fixes #9
1 parent 28b1e28 commit 09e2d62

File tree

3 files changed

+30
-30
lines changed

3 files changed

+30
-30
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,17 @@ The package exports the following bindings:
5252
```julia-repl
5353
julia> using EnforcedTypeSignatureCallables
5454
55-
julia> typed_callable(Float32, sin)(0.3f0)
55+
julia> typed_callable(sin, Float32)(0.3f0)
5656
0.29552022f0
5757
58-
julia> typed_callable(Float32, sin)(0.3)
58+
julia> typed_callable(sin, Float32)(0.3)
5959
ERROR: TypeError: in typeassert, expected Float32, got a value of type Float64
6060
[...]
6161
62-
julia> typed_callable(Float64, Tuple{Int, Int}, hypot)(3, 4)
62+
julia> typed_callable(hypot, Float64, Tuple{Int, Int})(3, 4)
6363
5.0
6464
65-
julia> typed_callable(Float64, Tuple{Int, Int}, hypot)(3, 4.0)
65+
julia> typed_callable(hypot, Float64, Tuple{Int, Int})(3, 4.0)
6666
ERROR: TypeError: in typeassert, expected Tuple{Int64, Int64}, got a value of type Tuple{Int64, Float64}
6767
[...]
6868
```
@@ -133,7 +133,7 @@ suffices to call `typed_callable` once:
133133

134134
```julia
135135
function accepts_a_function_from_the_user(func, other_arguments...)
136-
func = typed_callable(Float64, func)
136+
func = typed_callable(func, Float64)
137137
# any call of `func` is now guaranteed not to return anything other than `Float64`
138138
end
139139
```
@@ -146,7 +146,7 @@ function accepts_a_function_from_the_user_type_safe(func::CallableWithReturnType
146146
end
147147
148148
function accepts_a_function_from_the_user(func, other_arguments...)
149-
func = typed_callable(Float64, func)
149+
func = typed_callable(func, Float64)
150150
accepts_a_function_from_the_user_type_safe(func, other_arguments...)
151151
end
152152
```

src/EnforcedTypeSignatureCallables.jl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ function return_type_enforcer(::Type{Return}) where {Return}
9393
end
9494

9595
"""
96-
typed_callable(return_type::Type, argument_types::Type{<:Tuple}, callable)::CallableWithTypeSignature{return_type, argument_types}
96+
typed_callable(callable, return_type::Type, argument_types::Type{<:Tuple})::CallableWithTypeSignature{return_type, argument_types}
9797
9898
Creates a callable from `callable` with:
9999
@@ -108,14 +108,14 @@ Examples:
108108
```julia-repl
109109
julia> using EnforcedTypeSignatureCallables
110110
111-
julia> typed_callable(Float32, Tuple{Float32, Float32}, hypot)(3.1f0, 3.0f0)
111+
julia> typed_callable(hypot, Float32, Tuple{Float32, Float32})(3.1f0, 3.0f0)
112112
4.313931f0
113113
114-
julia> typed_callable(Float32, Tuple{Float32, Float32}, hypot)(3.1f0, 3.0)
114+
julia> typed_callable(hypot, Float32, Tuple{Float32, Float32})(3.1f0, 3.0)
115115
ERROR: TypeError: in typeassert, expected Tuple{Float32, Float32}, got a value of type Tuple{Float32, Float64}
116116
```
117117
"""
118-
function typed_callable(::Type{Return}, ::Type{Arguments}, callable::Callable) where {
118+
function typed_callable(callable::Callable, ::Type{Return}, ::Type{Arguments}) where {
119119
Return, Arguments <: Tuple, Callable,
120120
}
121121
ret = return_type_enforcer(Return)
@@ -124,7 +124,7 @@ function typed_callable(::Type{Return}, ::Type{Arguments}, callable::Callable) w
124124
end
125125

126126
"""
127-
typed_callable(return_type::Type, callable)::CallableWithReturnType{return_type}
127+
typed_callable(callable, return_type::Type)::CallableWithReturnType{return_type}
128128
129129
Creates a callable from `callable` with guaranteed return type `return_type`
130130
@@ -141,14 +141,14 @@ julia> typed_callable(Int, Int)(3)
141141
julia> typed_callable(Int, Int) isa CallableWithReturnType{Int}
142142
true
143143
144-
julia> typed_callable(Float64, cos)(3)
144+
julia> typed_callable(cos, Float64)(3)
145145
-0.9899924966004454
146146
147-
julia> typed_callable(Float32, cos)(3.0)
147+
julia> typed_callable(cos, Float32)(3.0)
148148
ERROR: TypeError: in typeassert, expected Float32, got a value of type Float64
149149
```
150150
"""
151-
function typed_callable(::Type{Return}, callable::Callable) where {
151+
function typed_callable(callable::Callable, ::Type{Return}) where {
152152
Return, Callable,
153153
}
154154
ret = return_type_enforcer(Return)

test/runtests.jl

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ using Aqua: Aqua
1313
@test CallableWithReturnType{Float32} == ComposedFunction{Base.Fix2{typeof(typeassert), Type{Float32}}}
1414
end
1515
@testset "construction" begin
16-
@test (@inferred typed_callable(Float64, cos)) isa CallableWithReturnType
17-
@test (@inferred typed_callable(Float64, cos)) isa CallableWithReturnType{Float64}
18-
@test (@inferred typed_callable(Float64, cos)) isa CallableWithReturnType{Float64, typeof(cos)}
16+
@test (@inferred typed_callable(cos, Float64)) isa CallableWithReturnType
17+
@test (@inferred typed_callable(cos, Float64)) isa CallableWithReturnType{Float64}
18+
@test (@inferred typed_callable(cos, Float64)) isa CallableWithReturnType{Float64, typeof(cos)}
1919
end
2020
@testset "return type enforcement" begin
21-
f = typed_callable(Int, only)::CallableWithReturnType
21+
f = typed_callable(only, Int)::CallableWithReturnType
2222
x_int = Any[3]
2323
x_f64 = Any[3.0]
2424
@test 3 === @inferred f(x_int)
@@ -33,24 +33,24 @@ using Aqua: Aqua
3333
@test_throws TypeError CallableWithTypeSignature{<:Any, Int}
3434
end
3535
@testset "construction" begin
36-
@test (@inferred typed_callable(Float64, Tuple{Int, Int}, hypot)) isa CallableWithTypeSignature
37-
@test (@inferred typed_callable(Float64, Tuple{Int, Int}, hypot)) isa CallableWithTypeSignature{Float64}
38-
@test (@inferred typed_callable(Float64, Tuple{Int, Int}, hypot)) isa CallableWithTypeSignature{Float64, Tuple{Int, Int}}
39-
@test (@inferred typed_callable(Float64, Tuple{Int, Int}, hypot)) isa CallableWithTypeSignature{Float64, Tuple{Int, Int}, typeof(hypot)}
40-
@test (@inferred typed_callable(Int, Tuple{Float32}, Int)) isa CallableWithTypeSignature
41-
@test (@inferred typed_callable(Int, Tuple{Float32}, Int)) isa CallableWithTypeSignature{Int}
42-
@test (@inferred typed_callable(Int, Tuple{Float32}, Int)) isa CallableWithTypeSignature{Int, Tuple{Float32}}
43-
@test (@inferred typed_callable(Int, Tuple{Float32}, Int)) isa CallableWithTypeSignature{Int, Tuple{Float32}, Type{Int}}
36+
@test (@inferred typed_callable(hypot, Float64, Tuple{Int, Int})) isa CallableWithTypeSignature
37+
@test (@inferred typed_callable(hypot, Float64, Tuple{Int, Int})) isa CallableWithTypeSignature{Float64}
38+
@test (@inferred typed_callable(hypot, Float64, Tuple{Int, Int})) isa CallableWithTypeSignature{Float64, Tuple{Int, Int}}
39+
@test (@inferred typed_callable(hypot, Float64, Tuple{Int, Int})) isa CallableWithTypeSignature{Float64, Tuple{Int, Int}, typeof(hypot)}
40+
@test (@inferred typed_callable(Int, Int, Tuple{Float32})) isa CallableWithTypeSignature
41+
@test (@inferred typed_callable(Int, Int, Tuple{Float32})) isa CallableWithTypeSignature{Int}
42+
@test (@inferred typed_callable(Int, Int, Tuple{Float32})) isa CallableWithTypeSignature{Int, Tuple{Float32}}
43+
@test (@inferred typed_callable(Int, Int, Tuple{Float32})) isa CallableWithTypeSignature{Int, Tuple{Float32}, Type{Int}}
4444
@test_throws MethodError typed_callable(Int, Int, Int) # arguments type must subtype `Tuple`
4545
end
4646
@testset "arguments type enforcement" begin
4747
@testset "non-`Type`" begin
48-
f = typed_callable(Any, Tuple{Int}, -)::CallableWithTypeSignature
48+
f = typed_callable(-, Any, Tuple{Int})::CallableWithTypeSignature
4949
@test -3 === @inferred f(3)
5050
@test_throws TypeError f(3.0)
5151
end
5252
@testset "`Type`" begin
53-
f = typed_callable(Any, Tuple{Int}, Int8)::CallableWithTypeSignature
53+
f = typed_callable(Int8, Any, Tuple{Int})::CallableWithTypeSignature
5454
@test Int8(3) === @inferred f(3)
5555
@test_throws TypeError f(3.0)
5656
end
@@ -59,12 +59,12 @@ using Aqua: Aqua
5959
x_int = Any[3]
6060
x_f64 = Any[3.0]
6161
@testset "non-`Type`" begin
62-
f = typed_callable(Int, Tuple, only)::CallableWithTypeSignature
62+
f = typed_callable(only, Int, Tuple)::CallableWithTypeSignature
6363
@test 3 === @inferred f(x_int)
6464
@test_throws TypeError f(x_f64)
6565
end
6666
@testset "`Type`" begin
67-
f = typed_callable(Int, Tuple, Int)::CallableWithTypeSignature
67+
f = typed_callable(Int, Int, Tuple)::CallableWithTypeSignature
6868
@test 3 === @inferred f(3)
6969
@test 3 === @inferred f(3.0)
7070
end

0 commit comments

Comments
 (0)