Skip to content

Commit

Permalink
Merge pull request #58 from liuq/master
Browse files Browse the repository at this point in the history
Added the possibility to have Type parameters for smart type dispatching
  • Loading branch information
BenLauwens authored May 4, 2023
2 parents d0d4394 + 2a1c335 commit 96e46a6
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ function get_args(func_def::Dict)
kwarg_list = Vector{Symbol}()
for arg in (func_def[:args]...,)
arg_def = splitarg(arg)
push!(arg_list, arg_def[1])
arg_dict[arg_def[1]] = arg_def[3] ? Any : arg_dict[arg_def[1]] = arg_def[2]
if arg_def[1] != nothing
push!(arg_list, arg_def[1])
arg_dict[arg_def[1]] = arg_def[3] ? Any : arg_dict[arg_def[1]] = arg_def[2]
end
end
for arg in (func_def[:kwargs]...,)
arg_def = splitarg(arg)
Expand Down
44 changes: 43 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,46 @@ end
@testset "test_continue" begin
@test collect(test_continue()) == [1, 3, 4, 5, 6, 7, 8, 9, 10]
@test collect(test_continue_double()) == [1, 3, 1, 3]
end
end

@resumable function test_type_parameters(::Type{Int64}, start::Int64)
for i in start:start+3
@yield i
end
end

@resumable function test_type_parameters(::Type{Float64}, start::Int64)
for i in start:start+3
@yield float(i)
end
end

@testset "test_type_parameters" begin
@test collect(test_type_parameters(Int64, 1)) == [1, 2, 3, 4]
@test collect(test_type_parameters(Float64, 1)) == [1.0, 2.0, 3.0, 4.0]
end

@resumable function test_type_parameters_order(start::Int64, ::Type{T})::T where {T}
for i in start:start+3
@yield T(i)
end
end

@testset "test_type_parameters_order" begin
@test collect(test_type_parameters_order(1, Int64)) == [1, 2, 3, 4]
@test collect(test_type_parameters_order(1, Float64)) == [1.0, 2.0, 3.0, 4.0]
@test typeof(first(test_type_parameters_order(1, ComplexF32))) == ComplexF32
end

@resumable function test_type_parameters_optional(start::Int64; t::Type{T}=Type{Int64})::T where {T}
for i in start:start+3
@yield T(i)
end
end


@testset "test_type_parameters_optional" begin
@test collect(test_type_parameters_optional(1, t=Int64)) == [1, 2, 3, 4]
@test collect(test_type_parameters_optional(1, t=Float64)) == [1.0, 2.0, 3.0, 4.0]
@test typeof(first(test_type_parameters_optional(1, t=ComplexF32))) == ComplexF32
end

0 comments on commit 96e46a6

Please sign in to comment.