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:
https://github.com/hauleth/defconst

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.

First Post!

sodapopcan

sodapopcan

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.

Last Post!

greven

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. :slight_smile:
But your library is sound!

Where Next?

Popular in Announcing Top

bryanjos
Hi, I just published version 0.23.0 of Elixirscript. https://github.com/bryanjos/elixirscript/blob/master/CHANGELOG.md Most of the chan...
New
kelvinst
Hey everyone! Well, we made this lib a while ago and now we decided to finally go out and public with it! It’s a tool for creating and m...
New
treble37
Just looking for a little feedback on a tiny helper library I built - Sometimes I find the need to convert maps with atom keys to maps w...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44265 214
New
mathieuprog
Hello :waving_hand: Allow me to introduce you to Tz, an alternative time zone database support to Tzdata. Why another library? First a...
New
zoltanszogyenyi
Hey everyone :waving_hand: Excited to join this forum - I am one of the founders and current project maintainers of a popular and open-s...
New
scohen
Lexical Lexical is a next-generation language server for the Elixir programming language. Features Context aware code completion As-you...
New

Other popular topics Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New

We're in Beta

About us Mission Statement