zoedsoupe

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 :thinking:

Marked As Solved

awerment

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! :sweat_smile:

Also Liked

sodapopcan

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 :thinking:

…and now that I said that:

@callback column() or term() :: String.t()

…also compiles so… there we go, ha.

josevalim

josevalim

Creator of Elixir

You can also do left in right, no need for unquote. :slight_smile:

Where Next?

Popular in Questions Top

shahryarjb
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
sergio
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
dotdotdotPaul
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
Tee
can someone please explain to me how Enum.reduce works with maps
New
bsollish-terakeet
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
mgjohns61585
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
mathew4509
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
LegitStack
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
chewm
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
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New

Other popular topics Top

peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
itssasanka
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
malloryerik
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
quazar
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
script
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
qwerescape
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
shahryarjb
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
baxterw3b
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
belgoros
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

We're in Beta

About us Mission Statement