On 13/10/15 16:34, Alexei Sholik wrote:
> It's more than just a style issue. Function clauses are checked in a
> specific order at runtime to decide which one to invoke. Changing the
> order of clauses in your source code will change the semantics of your
> program.
This is a valid point but it is not the issue that is in question here,
as far as I can tell.
You appear to be talking about this warning:
aa.ex:5: warning: this clause cannot match because a previous clause
at line 2 always matches
while the question was about this warning:
aa.ex:8: warning: clauses for the same def should be grouped
together, def aa/1 was previously defined (aa.ex:2)
In the latter case, the code still works as designed (as far as I can
tell).
> Spreading clauses all over a module and interleaving them with other
> functions will only make it more difficult to read the code and make
> sure it's correct.
That still sounds like a style issue to me.
Consider something like this:
def handle_call( :foobar ) do
... a bit of logic involving a couple of convenience functions
... called "foo" and "bar" that are only used by this piece of
... code
end
def foo (...) do
...
end
def bar (...) do
...
end
#... more handle_calls with other requests, and their corresponding
#... helper functions defined right after them
I do agree that putting all the helper functions somewhere else is one
way to organise your code. It might even be the *better* way in 90% of
the cases.
But it's not the only way. And in some situations the one above will
read easier for someone trying to debug one specific request type.
I don't mind if you say "yes, this is a style issue that we feel so
strongly about that we enforce it with a compiler warning". That's
fine, just say so.
What's confusing me is that you seem to be saying it's something *more
than just style*, and I am unable to see where that comes from.
regards
sitaram
>
> So what the Elixir compiler does is the gentle way to handle this
> issue. An Erlang program wouldn't even compile if it had interleaved
> function clauses.
>
> >>>>>> "m" == mraptor <
mra...@gmail.com <mailto:
mra...@gmail.com>> writes:
> >
> > m> I'm grouping them on different principle, not based on name, but
> > m> on functionality.
> >
> > It's easy (and fast) enough to do something like:
> >
> > def event(:x, var), do: one_thing(var)
> > def event(:y, var), do: another_thing(var)
> > def event(:z, var), do: something_else_entirely(var)
> >
> > and then your maintainers (or your future self) won't curse you forever.
> > The follow on functions can then be grouped as you see fit.
> >
> > Although there are no doubt pathalogical cases, what you describe (a
> > multi-headed function doing different enough stuff that you want to
> > separate the heads in the source) strikes me as an anti-pattern.
>
> That sounds like a *style* issue, which is often subjective. Should that
> even *be* a compiler warning?
>
>
>
> --
> You received this message because you are subscribed to the Google Groups "elixir-lang-talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
elixir-lang-ta...@googlegroups.com <mailto:
elixir-lang-talk%2Bunsu...@googlegroups.com>.
> --
> You received this message because you are subscribed to the Google Groups "elixir-lang-talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
elixir-lang-ta...@googlegroups.com <mailto:
elixir-lang-ta...@googlegroups.com>.
> To view this discussion on the web visit
https://groups.google.com/d/msgid/elixir-lang-talk/CAAPY6eMbwzrQ%2BCyoDBTKdTwo7j3soi9A%3Dnmqn36pVHTjC%3Dx9Mg%40mail.gmail.com <
https://groups.google.com/d/msgid/elixir-lang-talk/CAAPY6eMbwzrQ%2BCyoDBTKdTwo7j3soi9A%3Dnmqn36pVHTjC%3Dx9Mg%40mail.gmail.com?utm_medium=email&utm_source=footer>.