{"id":17771450,"url":"https://github.com/marius311/selffunctions.jl","last_synced_at":"2025-04-01T15:18:42.104Z","repository":{"id":149184310,"uuid":"149665243","full_name":"marius311/SelfFunctions.jl","owner":"marius311","description":"Julia macro for writing more concise functions with a \"self\"-like argument","archived":false,"fork":false,"pushed_at":"2020-12-12T02:35:35.000Z","size":8,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-07T09:33:36.258Z","etag":null,"topics":["julia","macros"],"latest_commit_sha":null,"homepage":null,"language":"Julia","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/marius311.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-09-20T20:14:34.000Z","updated_at":"2020-12-12T02:35:37.000Z","dependencies_parsed_at":null,"dependency_job_id":"33964911-4af4-4b05-a15f-ebb453efaaee","html_url":"https://github.com/marius311/SelfFunctions.jl","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marius311%2FSelfFunctions.jl","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marius311%2FSelfFunctions.jl/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marius311%2FSelfFunctions.jl/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marius311%2FSelfFunctions.jl/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/marius311","download_url":"https://codeload.github.com/marius311/SelfFunctions.jl/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246660077,"owners_count":20813338,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["julia","macros"],"created_at":"2024-10-26T21:33:00.953Z","updated_at":"2025-04-01T15:18:42.084Z","avatar_url":"https://github.com/marius311.png","language":"Julia","funding_links":[],"categories":[],"sub_categories":[],"readme":"# SelfFunctions.jl\n\nSelfFunctions.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. \n\nIt 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. \n\nTo install, \n\n```\nadd https://github.com/marius311/SelfFunctions.jl.git\n```\n\nAn example, \n\n```julia\n\nusing SelfFunctions\n\n# struct which stores parameters of a model\nstruct Rosenbrock{T}\n    a::T\n    b::T\nend\n\n# define some mathematical function\n@self Rosenbrock rosen(x,y) = (a-x)^2 + b*(y-x^2)^2\n\n# create model and call function\nr = Rosenbrock(1, 2)\nrosen(r, 3, 4) # returns 54\n```\n\nThe magic is that the macro rewrites,\n\n```julia\n@self Rosenbrock rosen(x,y) = (a-x)^2 + b*(y-x^2)^2\n```\nto \n```julia\nrosen(self::Rosenbrock,x,y) = (self.a-x)^2 + self.b*(y-x^2)^2\n```\n\nNote 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:\n\n```julia\n@self Rosenbrock shifted_rosen(x,y) = rosen(x+1, y+1)\nshifted_rosen(r, 2, 3) # gives 54 as before\n```\n\nThanks 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).\n\nCapturing type parameters works as you might expect,\n```julia\n@self Rosenbrock{T} rosen(x,y) where {T} = (a-x)^2 + b*(y-x^2)^2 + zero(T)\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarius311%2Fselffunctions.jl","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmarius311%2Fselffunctions.jl","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarius311%2Fselffunctions.jl/lists"}