https://github.com/marius311/selffunctions.jl
Julia macro for writing more concise functions with a "self"-like argument
https://github.com/marius311/selffunctions.jl
julia macros
Last synced: about 1 year ago
JSON representation
Julia macro for writing more concise functions with a "self"-like argument
- Host: GitHub
- URL: https://github.com/marius311/selffunctions.jl
- Owner: marius311
- Created: 2018-09-20T20:14:34.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2020-12-12T02:35:35.000Z (over 5 years ago)
- Last Synced: 2025-02-07T09:33:36.258Z (over 1 year ago)
- Topics: julia, macros
- Language: Julia
- Size: 7.81 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# SelfFunctions.jl
SelfFunctions.jl provides a macro, `@self`, which gives functions an implicit argument of a specified type, and lets you implicitly access the fields of that type from inside the function. It is quite similar to how C++ class member functions work.
It is most useful when you have a struct storing "parameters" of a model, and then are writing many mathematical functions that use those parameters. SelfFunctions.jl lets you write those functions succinctly and without obscuring the mathematics.
To install,
```
add https://github.com/marius311/SelfFunctions.jl.git
```
An example,
```julia
using SelfFunctions
# struct which stores parameters of a model
struct Rosenbrock{T}
a::T
b::T
end
# define some mathematical function
@self Rosenbrock rosen(x,y) = (a-x)^2 + b*(y-x^2)^2
# create model and call function
r = Rosenbrock(1, 2)
rosen(r, 3, 4) # returns 54
```
The magic is that the macro rewrites,
```julia
@self Rosenbrock rosen(x,y) = (a-x)^2 + b*(y-x^2)^2
```
to
```julia
rosen(self::Rosenbrock,x,y) = (self.a-x)^2 + self.b*(y-x^2)^2
```
Note that because the fields of `Rosenbrock` are known, the macro knows to only modify `a` and `b`. It is moderately smart about which variables to modify; many, but not all, cases should work. Note also that inner calls to "self" functions do not explicitly need to pass the first argument, this is inserted automatically:
```julia
@self Rosenbrock shifted_rosen(x,y) = rosen(x+1, y+1)
shifted_rosen(r, 2, 3) # gives 54 as before
```
Thanks to @fcard for the cool trick which allows this to work with no performance overhead (see also https://github.com/fcard/SelfFunctions.jl which does basically the same thing but with different syntax, although as of this writing is not compatible with Julia 1.0).
Capturing type parameters works as you might expect,
```julia
@self Rosenbrock{T} rosen(x,y) where {T} = (a-x)^2 + b*(y-x^2)^2 + zero(T)
```