> abstract typeA
>
> immutable typeA1 <: typeA end
> immutable typeA2 <: typeA end
>
> type typeB1 end
> type typeB2 end

More Julian would be to write:

transformType(T::Type{typeA1}) = typeB1
transformType(T::Type{typeA2}) = typeB2
transformType(T::Type) = error("Cannot handle type $T")

> function transformType(T)
>     if T == typeA1
>        return typeB1
>     elseif T == typeA2
>        return typeB2
>     end
>     error("Cannot handle type $T")
> end
>
> function compute{T<:typeA}(a::transformType(T), b::transformType(T))
>     return (a, b)
> end
>
> but it failed with the following error message
>
> ERROR: Cannot handle type T<:typeA
>  in error at error.jl:21
>  in transformType at /Users/bpiwowar/Desktop/test.jl:15
>  in include at 
> /opt/homebrew-cask/Caskroom/julia/0.3.5/Julia-0.3.5.app/Contents/Resources/julia/lib/julia/sys.dylib
>  in include_from_node1 at loading.jl:128
>  in process_options at 
> /opt/homebrew-cask/Caskroom/julia/0.3.5/Julia-0.3.5.app/Contents/Resources/julia/lib/julia/sys.dylib
>  in _start at 
> /opt/homebrew-cask/Caskroom/julia/0.3.5/Julia-0.3.5.app/Contents/Resources/julia/lib/julia/sys.dylib
> while loading /Users/bpiwowar/Desktop/test.jl, in expression starting on 
> line 18
>
> The type transformType(T)is hence computed when the method is defined, but 
> not when the method is called. Is there any way to prevent this (to defer 
> evaluation at compilation time?). 

Have a look at stagedfunction (v0.4).

> 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)

Reply via email to