hauleth

hauleth

defconstant - library for defining constants in your module

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:

Hex:


Usage is dumb simple. There are 2 macros provided:

  • defconst - for compile time constants
  • defonce - for runtime constants - what it means is that the value of this function will be lazily computed during first call and then cached in persistent_term for 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.

Most Liked

juanjoc

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.

axelson

axelson

Scenic Core Team

Nice! For anyone super lazy here’s a simple example of using the defined 0-arity functions:

defmodule MyConstants do
  import Defconstant

  defconst the_answer, do: 42

  def my_func do
    the_answer() * 2 # returns 84
  end
end

I’ve also submitted a PR to add more docs and examples.

greven

greven

Top notch. I think this would feel right at home In the standard library.

sodapopcan

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 def is quite useful, I see the bigger value prop of defconst to be that it avoids this problem, so perhaps calling that out as well would be good.

Where Next?

Popular in Libraries Top

devonestes
Introducing assertions, the library that helps you write really great test assertions! GitHub: https://github.com/devonestes/assertions ...
New
mathieuprog
Hello :wave: Allow me to introduce you to Tz, an alternative time zone database support to Tzdata. Why another library? First and fore...
New
seancribbs
Today I released a new dialyzer Mix task as the dialyzex package! At the time we started writing this task, the existing dialyzer integra...
New
woylie
I released Doggo, a collection of unstyled Phoenix components. Features Unstyled Phoenix components. Storybook that can be added to...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
danschultzer
In short Plug n’ play OAuth 2.0 provider library. Just set up a resource owner schema with Ecto (your user schema), install the dependen...
New
Crowdhailer
I have been updating a library that allows you to pipe between functions that use the erlang result tuple convention. Assuming you have...
New
benlime
LiveMotion enables high performance animations declared on the server and run on the client. As a follow up to my previous thread A libr...
New
gjaldon
As the title states, EctoEnum has just been updated after some time of hardly any activity in the repo. Here’s the latest release: https:...
New
wojtekmach
Hey everyone! Req is an HTTP client for Elixir that I’ve been working on for quite some time. There is already a lot of HTTP clients out...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3268 119930 1237
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

Sub Categories:

We're in Beta

About us Mission Statement