Detecting bogus defdelegate options

46 views
Skip to first unread message

Thomas Stratmann

unread,
Oct 16, 2019, 5:51:43 AM10/16/19
to elixir-lang-core
Hello,

a coworker tried `defdelegate fun(..), to: SomeModule, when: some_conditon` the other day. defdelegate does not implement a `when` option, but does not complain about any excess options.

Since defdelegate is almost always executed at compile time, the impact of an additional check for the options passed should be negligible. It could raise with a meaningful message.

Would this be a welcome change?

Louis Pilfold

unread,
Oct 16, 2019, 6:05:48 AM10/16/19
to elixir-lang-core
Hello!

What would be the desired behaviour of the `when` option?

Cheers,
Louis

--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/8856eeed-f80e-458a-addb-e063e9791473%40googlegroups.com.

José Valim

unread,
Oct 16, 2019, 6:24:38 AM10/16/19
to elixir-l...@googlegroups.com
I believe the reporter wants it to raise, since it is not really supported. If that's the case, please open up a bug report. Thanks.


José Valim
Skype: jv.ptec
Founder and Director of R&D


Kelvin Raffael Stinghen

unread,
Oct 26, 2019, 8:24:32 AM10/26/19
to elixir-l...@googlegroups.com
Hey! Is this something that is going to be added? I liked the idea of having that check, and I can send the PR if you don’t mind.

Best,
Kelvin Stinghen
kelvin....@me.com

Andrea Leopardi

unread,
Oct 26, 2019, 9:40:53 AM10/26/19
to elixir-lang-core
Hey Kelvin,

you're welcome to send a PR that adds stricter checks on options :)

Thank you!

Andrea Leopardi


Rich Morin

unread,
Oct 26, 2019, 10:11:24 AM10/26/19
to elixir-lang-core
I also like the idea of adding option checks to defdelegate.

On a related note, what about adding some sanity checks to `@doc delegate_to`?
For example, I'd like to get a warning about mismatches such as this:

@doc delegate_to: {Foo, :bar, 2}
defdelegate bar(baz), to: Foo

For that matter (ducking), what about avoiding the whole problem? For example,
give `defdelegate` an option to create the `@doc delegate_to` entry automagically:

defdelegate bar(baz), to: Foo, :doc

-r

Rich Morin

unread,
Nov 22, 2019, 12:56:07 PM11/22/19
to elixir-lang-core
About a month ago, I made the following suggestion, which never received any comment:

> ... give `defdelegate` an option to create the `@doc delegate_to` entry automagically:
>
> defdelegate bar(baz), to: Foo, :doc

Please let me know if this (or some alternative) would be possible, useful, etc.

-r

Wojtek Mach

unread,
Nov 22, 2019, 1:29:24 PM11/22/19
to elixir-l...@googlegroups.com
defdelegate already adds a `@doc delegate_to: …` on master!
> --
> 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.
> To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/73A1F5CD-6986-453F-8723-45D5815D93C4%40gmail.com.

Rich Morin

unread,
Nov 22, 2019, 1:55:49 PM11/22/19
to elixir-l...@googlegroups.com
> On Nov 22, 2019, at 10:29, Wojtek Mach <woj...@wojtekmach.pl> wrote:
>
> defdelegate already adds a `@doc delegate_to: …` on master!

Cool. I'd like to examine the relevant code. Can you provide a link?
-r

Wojtek Mach

unread,
Nov 22, 2019, 1:58:39 PM11/22/19
to elixir-l...@googlegroups.com
Let’s see:

~/src/elixir[master]% rg delegate_to
lib/elixir/lib/kernel.ex
4909: @doc delegate_to: {target, as, :erlang.length(as_args)}

lib/elixir/lib/module.ex
2047: defp validate_doc_meta(:delegate_to, value) do
2054: ":delegate_to is a built-in documentation metadata key. It should be a three-element " <>

lib/iex/test/iex/helpers_test.exs
549: "* def func1()\n\ndelegate_to: Delegated.func1/0\n\n\n"
552: "* def func2()\n\ndelegate_to: Delegated.func2/0\n\nDelegator func2 doc\n"

lib/elixir/lib/io/ansi/docs.ex
59: Prints documentation metadata (only `delegate_to`, `deprecated`, `guard`, and `since` for now).
81: {:delegate_to, {m, f, a}}, _printed ->
82: label = metadata_label(:delegate_to, options)

lib/elixir/test/elixir/io/ansi/docs_test.exs
32: delegate_to: {Foo, :bar, 3}
36: \e[33mdelegate_to:\e[0m Foo.bar/3

Looks like the first hit, lib/elixir/lib/kernel.ex:4909, is relevant.
> --
> 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.
> To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/5F0B6BF5-DFB5-456E-AA52-004E1C36BFEB%40gmail.com.

Rich Morin

unread,
Nov 22, 2019, 2:11:48 PM11/22/19
to elixir-l...@googlegroups.com
> On Nov 22, 2019, at 10:58, Wojtek Mach <woj...@wojtekmach.pl> wrote:
>
> ~/src/elixir[master]% rg delegate_to
> lib/elixir/lib/kernel.ex
> 4909: @doc delegate_to: {target, as, :erlang.length(as_args)}

Amazing how much one line of code can do, when it's the right line in the
right place. Anyway, I gather that the upcoming release of Elixir will
generate "@doc delegate_to" lines automagically and I will be able to
remove the ones I've added. Nice.

FWIW, I had suggested a :doc flag in case there were cases where folks
wouldn't want the "@doc delegate_to" to be generated. I take it that this
isn't a concern? Also, now that this feature is being added, is there a
separate use case for adding an explicit "@doc delegate_to" line?

-r



Wojtek Mach

unread,
Nov 22, 2019, 2:19:53 PM11/22/19
to elixir-l...@googlegroups.com
I can’t think of cases where I wouldn’t want `@doc delegate_to` generated for me or where I would use it explicitly.
> --
> 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.
> To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/BA448C06-C519-4CFE-93EF-8DBED5AE94ED%40gmail.com.

Reply all
Reply to author
Forward
0 new messages