Misleading error message: "def can?/3 defines defaults multiple times"

My mistake: I had two “use Ash.Domain” lines in my file – the second one to add “AshAdmin.Domain”.

The error message was misleading to me. The message indicated the problem was with the “defmodule” line.

My wish: the error message was more along the lines of “don’t repeat ‘use Ash.Domain’ in the same file”

Suggestion: the error message should indicate the problem is in the second “use Ash.Domain” line instead of the “defmodule …” line.

Here’s the error I got

heb@howards-mbp ogl % mix compile
Compiling 3 files (.ex)
    error: def can?/3 defines defaults multiple times. Elixir allows defaults to be declared once per definition. Instead of:

        def foo(:first_clause, b \\ :default) do ... end
        def foo(:second_clause, b \\ :default) do ... end

    one should write:

        def foo(a, b \\ :default)
        def foo(:first_clause, b) do ... end
        def foo(:second_clause, b) do ... end

    │
  1 │ defmodule Ogl.Lesson do
    │ ^^^^^^^^^^^^^^^^^^^^^^^
    │
    └─ lib/ogl/lesson.ex:1


== Compilation error in file lib/ogl/lesson.ex ==
** (CompileError) lib/ogl/lesson.ex: cannot compile file (errors have been logged)
    /Users/heb/dev/ogl/lib/ogl/lesson.ex:1: (file)
    (stdlib 6.1) erl_eval.erl:904: :erl_eval.do_apply/7
    (stdlib 6.1) erl_eval.erl:271: :erl_eval.exprs/6
    (stdlib 6.1) erl_eval.erl:436: :erl_eval.expr/6
    (spark 2.2.27) /Users/heb/dev/ogl/lib/ogl/lesson.ex:1: Spark.Dsl.__before_compile__/1

Yeah, this is pretty interesting. I imagine that pretty much any module that defines a use like this does not anticipate being used twice :laughing: since it’s a code mix in like that. It wouldn’t be a high priority, but would be relatively easy to detect so makes sense to add. Could you open an issue on the ash repo?

Done.

I don’t have a good sense of when I can and when I can’t repeat a macro call. E.G. in router.ex, multiple scope "/" do lines are acceptable.

1 Like

I would hate to start a bunch of stuff like the C header stuff

#ifndef __MYHEADERNAME__
ext bool global_bool;
#define __MYHEADERNAME__
#endif
1 Like

99.9% of the time, a use macro is meant to be performed only once.

I should have figured that out.

Quoting Peter, Paul, and Mary, “I’ll learn so that my later days will be prosperous…”