Skip to content

Commit 0cd76d6

Browse files
authored
Implicity convert function into FunctionWrappersWrapper (#54)
* Added FunctionWrappersWrapper constructor for the case where type parameters are specified but only the function to be wrapped is provied as an argument. * Changed arguments types in test from Int to Float32 because calculating exp and cos of integer really doesn't make sense. Actually check the value of the wrapped cos. * Added specializations of Base.convert for FunctionWrappersWrapper based on new constructor. * Added a docstring for new constructor * Fixed formatting
1 parent 9c11a85 commit 0cd76d6

2 files changed

Lines changed: 40 additions & 0 deletions

File tree

src/FunctionWrappersWrappers.jl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,25 @@ end
111111

112112
TruncatedStacktraces.@truncate_stacktrace FunctionWrappersWrapper
113113

114+
"""
115+
FunctionWrappersWrapper{FW, P, CS}(f)
116+
117+
Create a `FunctionWrappersWrapper` when the type parameters are specified.
118+
119+
# Arguments
120+
- `f`: The function to wrap
121+
122+
# Type parameters
123+
- `FW`: Tuple type of `FunctionWrapper`s
124+
- `P`: Fallback policy (`Strict`, `AllowAll`, or `AllowNonIsBits`)
125+
- `CS`: Cache storage type (`NoCacheStorage`, `SingleCacheStorage`, `DictCacheStorage`)
126+
"""
127+
function FunctionWrappersWrapper{FW, P, CS}(f) where {K, FW <: NTuple{K, Any}, P, CS}
128+
fw = ntuple(i -> FW.parameters[i](f), Val(K))
129+
cs = CS()
130+
return FunctionWrappersWrapper{FW, P, CS}(fw, cs)
131+
end
132+
114133
"""
115134
FunctionWrappersWrapper(f, argtypes, rettypes; cache=SingleCache(), policy=AllowNonIsBits())
116135
@@ -137,6 +156,8 @@ function FunctionWrappersWrapper(
137156
return FunctionWrappersWrapper{typeof(fwt), typeof(policy), typeof(cs)}(fwt, cs)
138157
end
139158

159+
Base.convert(::Type{T}, obj) where {T <: FunctionWrappersWrapper} = T(obj)
160+
Base.convert(::Type{T}, obj::T) where {T <: FunctionWrappersWrapper} = obj
140161

141162
# ============================================================================
142163
# Call dispatch — entry point

test/basictests.jl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,3 +243,22 @@ end
243243
# Float32 is isbits mismatch → errors
244244
@test_throws FunctionWrappersWrappers.NoFunctionWrapperFoundError fww(4.0f0, 8.0f0)
245245
end
246+
247+
@testset "Conversion" begin
248+
fww_exp = FunctionWrappersWrapper(
249+
exp,
250+
(Tuple{Float64}, Tuple{Float32}),
251+
(Float64, Float32)
252+
)
253+
FWW = typeof(fww_exp)
254+
255+
fww_cos = FWW(cos)
256+
@test typeof(fww_cos) == FWW
257+
@test fww_cos(0.5) == cos(0.5)
258+
259+
fww_vector = FWW[sin, tan, log]
260+
@test eltype(fww_vector) == FWW
261+
fww_vector[1](0.5) == sin(0.5)
262+
fww_vector[2](0.5) == tan(0.5)
263+
fww_vector[3](0.5) == log(0.5)
264+
end

0 commit comments

Comments
 (0)