Interesting problem with where parameters


I have the following code:

    abstract type TypedFun end

    abstract type ReturnFun{R} <: TypedFun end

    abstract type NoArgFun{R} <: ReturnFun{R} end

    abstract type FullFun{Tuple,R} <: ReturnFun{R} end

    #single argument function
    struct SAFun{T,R} <: FullFun{Tuple{T},R}

    (⇒)(::Type{T}, ::Type{R}) where {T,R} = SAFun{T, R}
    (⇒)(::Nothing, ::Type{R}) where {R} = NoArgFun{R}

   struct MM{T}

   f(fun::(Any ⇒ MM{T})) where T = fun(3) 

the function f definition gives the following error

ERROR: UndefVarError: R not defined

However this doesn’t give the same error:

f(fun::(Any ⇒ MM)) = fun(3)

Why is this happening…

Thanks, and how can I fix it?


Please provide a self-contained example. We don’t know what SAFun is and you are not providing a stack trace.


This works f(fun::(Any ⇒ MM{T} where T)) = fun(3). Note though that the Any ⇒ MM{T} where T part is evaluated at function definition and not later. Thus having the where outside does not make sense.


what if the same T is used in other parameters of the function such as

f(x::T, fun::(Any ⇒ MM{T})) where T = fun(x)

how do we fix this?