Skip to content

Typed thunks? #373

@oschulz

Description

@oschulz

The discussion on FluxML/Zygote.jl#966 - thunks have really evolved into monads now, right? Especially now that we're adding methods for them to lots of linear algebra and other functions. Shouldn't AbstractThunk have a type parameter, then? Kinda like

abstract type AbstractThunk{T} end

struct Thunk{T,F<:Base.Callable} <: AbstractThunk{T}
    body::F
end

unthunk(t::Thunk{T}) where T = t.body()::T

Base.eltype(t::Thunk{T}) where T = T

macro thunk(body)
    func = Expr(:->, Expr(:tuple), Expr(:block, __source__, body))
    return quote
        f = $(esc(func))
        Thunk{Base._return_type(f,()), typeof(f)}(f)
    end
end


thnk = let A = rand(5,5), B = rand(5,5)
    @thunk A * B
end

eltype(thnk) == typeof(unthunk(thnk))

I've seen type inference fail with thunks quite a few times - maybe having typed thunks would help with that, too?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions