Avoiding "always matches" warnings

I have a module that is being used. The purpose of the module is to provide some default functions that can be overridden. I am use __before_compile__ to ensure there is a catch-all function at the end of the module with the lowest precedence.

However, in the consuming module if I override the catch-all with a custom catch-all I get the expected warning:

warning: this clause cannot match because a previous clause at line N always matches

defoverridable doesn’t work here because technically it isn’t being overriden. Is there any way to avoid this warning?

One thought I had, but I don’t think it is possible, is at compile-time in __before_compile__ if I had a way to detect if the catch-all function was already defined I would just not define in __before_compile__

3 Likes

You could always parse the module and add functions that are not already defined?

You could also override the base user’s version and then call their’s via super(...) catching unhandled cases or so.

A few other ways… ^.^

2 Likes

I am not actually sure this will work; doesn’t use add code to the module that is being compiled, and as such isn’t the new code added before we have any idea about what functions the user will add to the module?

Using defoverridable here would ‘work’, but also mean that the earlier versions of your functions (the non-catch-all variants) would be overridden.

This is a very interesting problem, @bcardarella. I also would love to hear a solution for this.

1 Like

True, not at that point, however you can do things like redefine def’s, use __before_compile__ to inject things based on usage, etc… I’ve done that quite a few times… >.>

Yep, but he could put in the ‘default’ versions at the place of ‘use’ with defoverridable, then the user can override ‘some’ of them, then can inject more functions at the end to call the super’s and handle non-handled cases that the user did not handle so as it add in the default states. :slight_smile:

1 Like

Heh. I had not thought about redefining def. That’s a wonderful but play-with-fire levels of dangerous kind of solution to this problem.

1 Like

Oh yes, more like playing with bombs! If you just need to define attributes or so you could do the __on_definition__ or whatever it is called to build things up, but I tend to do… bigger things to def/defp… ^.^

I’ve gotten lucky with not hitting anything really odd yet, but eh… We need a strong statically typed system, badly. ^.^

1 Like

You can do this by annotating the block as generated:

quote generated: true do
  def something_that_would_warn_if_clauses_above_always_match(...)
  end
end
7 Likes

That works :slight_smile:

1 Like

You learn something new every day! :smiley:

1 Like