-
Notifications
You must be signed in to change notification settings - Fork 68
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement variadic function #315
Conversation
By |
|
Substitute the @generated function scanf(s, va_list...)
va_list = map(enumerate(to_c_type.(va_list))) do (ind, type)
:(va_list[$ind]::$type)
end
:(@ccall(scanf(s::Ptr{Cchar}; $(va_list...))::Cint))
end
a = Ref(0)
scanf("%d", a)
# Enter 123
println(a[]) # => 123 |
Thanks! I'll check it out. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's succinct to give users full control of the types of varargs
, just like what they need to do when using @ccall
. But to_c_type
also has some use cases where users might want to hack into the @generated function
and insert their own implementation of to_c_type
.
@generated function scanf(s, args::Vararg{Any,N}) where {N}
varargs = (:(args[$i]::$(args[i])) for i = 1:N)
:(@ccall(scanf(s::Ptr{Cchar}; $(varargs...))::Cint))
end
Unfortunately both CimGui.jl and LibCimGui.jl have few tests, so I forked LibCURL.jl and tested on Downloads.jl, and it looks fine. |
This is great! I would give it a battle test if I could find some time. |
I'm going to merge this. @melonedo could you rebase the commit history to be a little bit cleaner? For example, one commit for the implementation and another one for docs. |
Should I force-push the combined commits here? |
Yup. |
* Reorganize emit! for FunctionProto to extract common parts Wrap code in generated function Support no `@ccall` option Move repeated va_list transformer code to prologue Make variadic functions and to_c_type optional Remove default type deduction for String and Array Disable variadic functions by default
Update documentation
I have manually tested this implementation strategy with Julia's default
printf
, although this can not be done automatically.fix #146 , fix #17