Hi all, I recently made a little package StaticModules.jl (awaiting registration in 3 days).
The idea is that a
StaticModule is kinda like a
module, but can be created in the local scope, is immutable and doesn’t support things like
using. Basically a fancy
NamedTuple that you can ‘run code inside’.
julia> using StaticModules julia> @staticmodule Foo begin x = 1; y = x^2 + 2x - 1 f(z) = (x^2 + 2y)/z end StaticModule Foo containing f = f y = 2 x = 1 julia> f(x/y) ERROR: UndefVarError: x not defined Stacktrace:  top-level scope at REPL:1 julia> @with Foo begin f(x/y) end 10.0
@with macro will work with any type that’ll give you values from
julia> nt = (;a=1, b="hi") (a = 1, b = "hi") julia> @with nt begin a + length(b) end 3
Importantly, you should be able to use
@with without suffering any runtime performance penalty, so this can be used even in things like tight-loops if you want to namespace some code.
I kinda see this as offering similar (though different) features from packages like Parameters.jl. One advantage this has is that
StaticModules.@with will work on arbitrary structs and
Parameters.@unpack requires that you either register a struct with Parameters.jl, or tell it what symbols to unpack from a struct or named tuple. @mauro3, if you’re interested, I think
StaticModules.@with can be lifted for Parameters.jl pretty easily, though it’d add new dependancies.
All comments, questions, suggestions or bikeshedding welcome!