Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Elixir's sigils are amazing. There are date sigils that allow you to do what the OP does: ~N[2023-01-01 12:00:00]

But you can also define your own sigils to create new "custom syntax" for almost any struct. Kind of a special case of reader macros, I guess. Very convenient.



Swift has the expressiblebyTypeliteral series of protocols for this.

For example you could write an extension on Date to add initialization from a string:

    extension Date: ExpressibleByStringLiteral {
        public init(stringLiteral value: String) {
             // parse the string here. 
        }

    }

You can then do things like:

    let happyNewYear: Date = “2023-01-01 12:00:00”

There are a protocols for all literal types. For example, you could implement ExpressibleByIntegerLiteral and have have it init the Date object from a unix timestamp. There is even an ExpressibleByNilLiteral.


It's "funny" that things that are considered an anti-feature and something that needs to be avoided by all means in one place is considered a great feature in another place. This points strongly in the direction that there is no logic behind such "considerations".

What you just showed was a implicit conversion from String to Date. Something you would get beaten up for in Scala land.


C++ has the same with implicit constructors, generally considered to be a footgun that should be disabled with the explicit-keyword unless such a cast makes sense, implicit constructors are otherwise the default. For example vector has a constructor with takes integer size argument, if it wasn't explicit you could accidentally do vector v = {10} which would construct a vector with 10 empty elements, instead of one element with value 10. This also has to do with the ambigous curly brace syntax in c++.


Well even JavaScript has custom string literal templates. Is this really that special?


Elixir sigils[1][2].

Eg: ~w(foo bar bat) is a word list. `~ letter bracketed-text lettersasmodifiers` desugars as sigil_<letter>(text,modifiers). Similar to foo_str() of Julia[3], but for one-letter-names and more brackets. But not the unicode brackets of Raku.

[1] https://elixir-lang.org/getting-started/sigils.html [2] https://hexdocs.pm/elixir/main/syntax-reference.html#sigils [3] https://docs.julialang.org/en/v1/manual/metaprogramming/#met...


The most recent addition to the digit family being Phoenix’s new ~p”/healht”, which is a HTTP route string that automatically verifies whether the route exists, and returns compile-time warnings when you link to a path that didn’ doesn’t. It’s fantastic, and really surprising it took this long to be added to any web framework.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: