Last active
March 20, 2017 03:25
-
-
Save fcard/8a2ad09318b90eeb2e82ecff435ace43 to your computer and use it in GitHub Desktop.
C11's _Generic-like expressions in julia with ccall and an application
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @generated function _Generic(t1::Type{T1}, args...) where T1 | |
| for i in 1:2:length(args) | |
| T2 = args[i].parameters[1] | |
| Ex = args[i+1].parameters[1].parameters[1] | |
| isa(T2,Type) || throw(ArgumentError("$T2 is not a type")) | |
| if Ex == :_ | |
| T1 <: T2 && return T1 | |
| else | |
| Ex = isa(Ex, Symbol) ? QuoteNode(Expr(:quote, Ex)) : Ex | |
| T1 <: T2 && return Ex | |
| end | |
| end | |
| throw(ArgumentError("No matching type found")) | |
| end | |
| macro generic(t, pairs...) | |
| args = [map(pair->[pair.args[2], :($Val{$(pair.args[3])})], pairs)...;] | |
| esc(:($_Generic($t, $(args...)))) | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function tryparse(::Type{T}, as::AbstractString) where T<:Union{Float32, Float64} | |
| s = String(as) | |
| s[1] == '_' && return Nullable{T}() | |
| s = replace(s, "_", "") | |
| ccall(@generic(T, | |
| Float32 => :jl_try_substrtof, | |
| Float64 => :jl_try_substrtod), | |
| Nullable{T}, (Ptr{UInt8}, Csize_t, Csize_t), s, 0, sizeof(s)) | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment