hauleth
It is partially a response to @PragTob’s blogpost
and @michalmuskala’s tweet:
We need proper first-class constants in Erlang that a module can define
It simply allows you to quickly define constants in your modules without resolving to “ugly hacks” in form of module attributes or unquote in “weird places”.
Source:
https://github.com/hauleth/defconst
Hex:
Usage is dumb simple. There are 2 macros provided:
defconst- for compile time constantsdefonce- for runtime constants - what it means is that the value of this function will be lazily computed during first call and then cached inpersistent_termfor future uses
Example:
defmodule Foo do
import Defconstant
defconst comptime do
# This will be evaulated at compile time
Enum.sum([
0, 2, 3, 4, 5, 6, 8, 12, 30, 32, 33, 34, 35, 36, 38, 40, 42, 43, 44, 45,
46, 48, 50, 52, 53, 54, 55, 56, 58, 60, 62, 63, 64, 65, 66, 68, 80, 82,
83, 84, 85, 86, 88
])
end
defonce runtime do
2 * 1068 + 1
end
end
And that is it.
Oh, one (obvious) limitation - it can define only 0-ary functions, as otherwise it would be impossible to have “constants”. If you want to have caching based on arguments, then you need to look up for other libraries.
Trending in Announcing
The repo is at GitHub - cyberchitta/openai_ex: Community maintained Elixir library for OpenAI API · GitHub.
Docs are at OpenaiEx User Gu...
New
WebAuthnLiveComponent WebAuthnComponents
See this post about renaming the package.
Passwordless authentication for Phoenix LiveView app...
New
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
I released Doggo, a collection of unstyled Phoenix components.
https://github.com/woylie/doggo
Features
Unstyled Phoenix components....
New
Hi everyone,
I’ve been working on this protobuf library for 3 years. We use it in the company I work for, EasyMile, to communicate with ...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
I’ll shortly be launching Text, a nascent text analysis library.
Current functionality
In this early version (not ready for prime time) ...
New
Other Trending Topics
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with.
But I don’t use it anymore.
Serverless was the promis...
New
What IDE or editor are you using for Elixir development?
Personally, I use Zed, and I really like it, but sometimes I wish there were a ...
New
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #ai
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
sodapopcan
PragTob
Really cool work, thank you!
axelson
Nice! For anyone super lazy here’s a simple example of using the defined 0-arity functions:
I’ve also submitted a PR to add more docs and examples.
sodapopcan
I was puzzled at first by the usefulness of this but after reading the code, I get it and it’s very cool.
I like the doc additions, @axelson. I feel that while getting an actual error over a “won’t match” warning you would get with
defis quite useful, I see the bigger value prop ofdefconstto be that it avoids this problem, so perhaps calling that out as well would be good.hauleth
I also have an idea to provide
comptime/constmacro that would force compile time evaluation (quite it will be just wrapper onunquote) andoncemacro. That would allow to write in-function fragments that will have the same semantics asdefconstanddefonce.sodapopcan
Ah dang, my bad, I thought it was already doing this. The second part of my previous can be ignored.
greven
Top notch. I think this would feel right at home In the standard library.
hauleth
I have added support for private functions (haven’t released version
1.1.0with that features yet). But it have come to me that there is one behaviour that may be problematic for some people, so I come here with question for anyone interested in this library:https://github.com/hauleth/defconst/issues/3
I would like to hear your opinions on that issue.
juanjoc
FYI a long time ago I had created a similar library for Elixir called ex_const which also included support for enumerated values. There never was much interest, so I didn’t go much further.
greven
I guess since the docs pushed to just use module attributes, it’s normal people would just use that, I try to follow conventions over libraries unless there is a clear gap in the std.
But your library is sound!