top | item 40530694

(no title)

gamache | 1 year ago

> Turns out sometimes you gotta use these "orphan" functions even though they logically belong to a certain type (in this case, why isn't tuple_size called like Tuple.size or something?)

tuple_size/1 is a guard, and guards are built-in. The compiler itself uses them. Unlike regular functions, you are allowed to use guards in a function head, like:

    def foo(my_tuple) when tuple_size(my_tuple) == 3 do ...
Official docs: https://hexdocs.pm/elixir/1.16.3/patterns-and-guards.html#gu...

discuss

order

giraffe_lady|1 year ago

Ahh, that's it. I said in a sibling comment that I had no idea why it was like that. But reading this made me realize that at one point I did know why! Completely forgot this bit of knowledge in the last year of not using the language.

gamache|1 year ago

Oh yeah, FWIW a "built-in" function or macro just means it's a part of the Kernel module, which is automatically required+imported in Elixir modules. So it's not orphaned, it's just in the one special module that gets brought in by default.