zoedsoupe
Is it possible to use native functions names as callbacks on behaviour?
I can “overwrite” native stdlib functions, just by not importing them from the kernel, right?
Like:
defmodule Filter do
Import Kernel, except: [not: 1]
def not args do
# ...
end
end
Okay, that’s valid! But what if I wanted to put this function as a callback in a behavior, what would the syntax look like? Is that possible?
defmodule FilterBehaviour do
Import Kernel, except: [not: 1]
@callback not(args) :: any()
end
It doesn’t seem to work yet as a syntax error ![]()
Marked As Solved
awerment
Huh, works for both @callback and def. And since the implementing module is its own namespace of course, the import Kernel, except: [...] is not needed there.
defmodule Filter do
@callback not(term) :: String.t()
@callback term or term :: String.t()
@callback unquote(:in)(term, term) :: String.t()
end
defmodule Impl do
@behaviour Filter
@impl Filter
def not(a), do: "!#{a}"
@impl Filter
def a or b, do: "#{a} or #{b}"
@impl Filter
def unquote(:in)(a, b), do: "#{a} in #{inspect(b)}"
end
Impl.not(1) <> ", " <> Impl.or(1, 2) <> ", " <> Impl.in(3, [3, 4])
#=> "!1, 1 or 2, 3 in [3, 4]"
Soo, yeah… thanks for the TIL! ![]()
1
Also Liked
sodapopcan
Defining it should work. You define infixes like so:
def a or b, do: a + b
I just don’t know how you would write that as a callback spec ![]()
…and now that I said that:
@callback column() or term() :: String.t()
…also compiles so… there we go, ha.
3
josevalim
Creator of Elixir
You can also do left in right, no need for unquote. ![]()
3
Popular in Questions
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
In Ruby, I can go:
User.find_by(email: "foobar@email.com").update(email: "hello@email.com")
How can I do something similar in Elixir? ...
New
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
can someone please explain to me how Enum.reduce works with maps
New
Credo is smart enough to check for (something like) this:
assert length(the_list) == 0
with this response:
Checking if an enum is empt...
New
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
I have a list say
x = ["23gh", "56kh", "97mh"]
I would like to pass each element to Val in each iteration.
Say, in iteration 1 -------...
New
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum.
...
New
Other popular topics
Manning 2016 Halloween weekend sale via Deal of the Day
Friday, October 28 - Half off all MEAPs - code WM102816LT
Saturday, October 29 ...
New
can someone please explain to me how Enum.reduce works with maps
New
Hi all,
Trying to get some more clarity over utc_datetime and naive_datetime for Ecto:
https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode.
The solution seems to be, in a hyphena...
New
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this
"1000"
What is the ...
New
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
Hello, I have map which I want to convert it to string like this:
the map:
%{last_name: "tavakkoli", name: "shahryar"}
the string I ne...
New
Hi guys, i’m new in the Elixir world, and i have to say, that i love it!
i’m having some problem to understand anonymous functions with ...
New
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New








