hauleth

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 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.

Showing Posts 1 to 10

sodapopcan

sodapopcan

PragTob

PragTob

Really cool work, thank you!

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.

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.

hauleth

hauleth OP

I also have an idea to provide comptime/const macro that would force compile time evaluation (quite it will be just wrapper on unquote) and once macro. That would allow to write in-function fragments that will have the same semantics as defconst and defonce.

sodapopcan

sodapopcan

Ah dang, my bad, I thought it was already doing this. The second part of my previous can be ignored.

greven

greven

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

hauleth

hauleth OP

I have added support for private functions (haven’t released version 1.1.0 with 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

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

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!

— All posts loaded —

Where Next? Top

Trending in Announcing Top

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
handnot2
Samly can be used to enable SAML 2.0 Single Sign On in a Plug/Phoenix application. This library uses Erlang esaml to provide plug enabl...
New
woylie
Flop is an Elixir library that applies filtering, ordering and pagination parameters to your Ecto queries. offset-based pagination with...
New
MRdotB
I needed to reuse React components from my Chrome extension in my Phoenix/LiveView backend. I noticed that for Svelte/Vue, there are live...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
fuelen
Hi all! I want to present a small library which provides a mix task for generating an Entity-Relationship Diagram for Ecto schemas. You...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
AstonJ
This showed up on my feed.. anyone heard of it? Just hype? Ox Alpha is a reasoning model designed for coding, sustained ag...
New
sergio
It’s not that it’s vocabulary is too advanced. It’s something worse. I get lost trying to follow even a paragraph written by Claude. It’...
New
sorenone
Today we’re releasing Oban for Python. Not an Oban client in Python. Not a pythonx wrapper embedded in Elixir. Nope, it’s a fully operati...
New
akoutmos
@hugobarauna, Dr. Dimitrios Koutmos (my brother) and I (Alex Koutmos) have been hard at work on writing a book on how you can use Elixir ...
New
pferriby
Introductory paragraph I’ll be looking for a keen junior or someone that has a couple of years experience in the real world (so you’ve be...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews