JuliaStaging / JuliaStaging/GeneralizedGenerated.jl
Closures with Fix1 for type stability
- Dominant language
- Julia
- Stars
- 89
- Forks
- 12
- PR merge metrics
- No merged PRs in 30d
Description
Hi @thautwarm ,
In a new version of Soss, I'm trying to really nail down type stability. Using GG I was having lots of trouble getting constructions like
```julia
For(n -> Normal(α + β * x[n], σ), 1:N)
```
to be type-stable.
After lots of exploring, I realized this could be solved by putting this function outside of GG:
```julia
f(ctx, n) = Normal(ctx.α + ctx.β * ctx.x[n], ctx.σ)
f(ctx::NamedTuple) = Base.Fix1(f, ctx)
```
Then within GG, it could be called as
```julia
For(f((;α,β,σ,x)), 1:N)
```
That works great, but it's tricky to automate, since the `f` needs to be made available separately within the module, and I'd rather not use `eval` to put it there.
I eventually found this to work:
```julia
For(let f = ctx -> Base.Fix1(ctx) do ctx, n
Normal(ctx.α + ctx.β * ctx.x[n], ctx.σ)
end
f((; α, β, σ, x))
end, 1:N)
```
So now, I think I can just rewrite things this way before calling GG. But I wanted to check with you before doing this, since it seems plausible the current Closure approach could be updated to something similar, and maybe it would help other cases with type stability. What do you think?
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.