On Thu, 2015-04-09 at 17:57, Benjamin Piwowarski <[email protected]> wrote:
> Le mardi 10 mars 2015 17:56:40 UTC+1, Mauro a écrit :
>>
>> >> Have a look at stagedfunction (v0.4). 
>> >> 
>> > I tried with the nightly build, but without success (using 
>> stagedfunction 
>> > instead of function) - however, it looks like it is the solution, maybe 
>> I 
>> > will just have to wait until this is implemented (and the syntax set). 
>>
>> You will need to adapt the code to work with stagedfunctions.  They work 
>> in stages: with the types of the arguments the code for the actual 
>> function is produced.  Sadly there is no documentation, I think.  See: 
>> https://github.com/JuliaLang/julia/issues/7311 
>> <https://www.google.com/url?q=https%3A%2F%2Fgithub.com%2FJuliaLang%2Fjulia%2Fissues%2F7311&sa=D&sntz=1&usg=AFQjCNETI73h5HytMQQq4nzKEDmxDL2NBg>
>>  
>>
>> >> > The only other way (I can think of) to get this result would be to 
>> use 
>> >> > macros and enumerate the different possible types, but if somebody 
>> can 
>> >> > think of a better solution, thanks! 
>> >> 
>> >> why not write: 
>> >> type typeB1 <: typeB end 
>> >> type typeB2 <: typeB end 
>> >> compute(a::typeB, b::typeB) = return (a, b) 
>> >> 
>> > Because I would like to ensure that a and b are of the same type 
>>
>> In that case use: 
>>
>> compute{T<:typeB}(a::T, b::T) = return (a, b) 
>>
>> T will be set to the concrete type of a and there will be a no-method 
>> error if isa(b,T) is not true. 
>>
>
> This is my fault - I did not the code generic enough. In my case, I would 
> like to use different type transform functions, e.g.
>
> function compute{T<:typeA}(a::transformTypeA(T), b::transformTypeB(T))
>     return (a, b)
> end
>
> I am not sure though staged function are the way to go - I tried to follow 
> some examples, and I would have to make tests on the types (which is not 
> the purpose - I just want to do early error checking by ensuring types are 
> compatible) before returning several version of the function depending on 
> the types.

If you want to compute on the types, as you do with transformTypeA(T)
then stagedfunctions are what you need.  There is now some
documentation, not merged yet, but here:

https://github.com/tlycken/julia/blob/doc-stagedfunctions/doc/manual/metaprogramming.rst#staged-functions

Maybe something like:

function compute{T,S}(a::T, b::S)
    if transformTypeA(T)!=transformTypeB(S)
        error("incompatible types")
    end
    :(return (a, b))
end

Reply via email to