Interesting problem with where parameters


#1

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}
        fn::Function
    end

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

   struct MM{T}
     x::T
   end

   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?


#2

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


#3

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.


#4

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?