pattern matching record from its own module

125 views
Skip to first unread message

Saša Jurić

unread,
Feb 28, 2013, 7:25:20 AM2/28/13
to elixir-l...@googlegroups.com
As I mentioned elsewhere, I have a habit of extending records with my own functions.
When I pattern match a record in the same module I do following:

defrecord MyRecord ... do
  def fun(__MODULE__[...]) ...
end

instead of 

defrecord MyRecord ... do
  def fun(MyRecord[...]) ...
end


I wanted to verify is there any downside to this? Any reason why I shouldn't do it?

José Valim

unread,
Feb 28, 2013, 9:05:55 AM2/28/13
to elixir-l...@googlegroups.com
Both formats should be fine. :)


José Valim
Skype: jv.ptec
Founder and Lead Developer


--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

ILYA Khlopotov

unread,
Mar 1, 2013, 2:09:51 PM3/1/13
to elixir-l...@googlegroups.com
>> Both formats should be fine. :)

Actually 'def fun(MyRecord[...])' not always work. For example it doesn't work for nested modules:
defmodule CCCC do
  defrecord State, [ignore: [], path: []] do
    def ignore(State[path: path, ignore: patterns]) do
      true
    end
  end
end

This code would work from iex but wouldn't when you try to compile module from file:
bin/elixirc tt.ex
== Compilation error on file tt.ex ==
** (RuntimeError) expected module State to be loaded and defined
    elixir/lib/elixir/lib/kernel.ex:2963: Kernel."MACRO-access"/3
    tt.ex:6: Kernel.access/2

However I think this must be a bug.

Best regards,
ILYA

José Valim

unread,
Mar 1, 2013, 2:58:47 PM3/1/13
to elixir-l...@googlegroups.com
Please open up an issue in the issues tracker?



José Valim
Skype: jv.ptec
Founder and Lead Developer


Reply all
Reply to author
Forward
0 new messages