Yes, we should either improve the current error message or just be smart about it. Macro.operator? can be used to check if something is an operator, so this should be relatively easy to make trivial.
*José Valimhttps://dashbit.co/ <https://dashbit.co/>* On Wed, Jan 22, 2025 at 10:37 AM Wojtek Mach <woj...@wojtekmach.pl> wrote: > The other day I was talking with a friend and I said Elixir basically has > no keywords, relatively little syntax, and we can look up a lot of things > like `iex> h if`. Most of the things we'd look up are `fun` and `mod.fun` > but others, especially operators, you need to know how to write it for `h` > to accept it. For example: > > ``` > iex> h %{} > > iex> h Kernel.* > iex> h Kernel.&& > > iex> h Kernel.SpecialForms.& > iex> h Kernel.SpecialForms.:: > ``` > > Today we have this: > > ``` > iex> h 1 + 2 > The "h" helper expects a Module, Module.fun or Module.fun/arity, got: 3 > If instead of accessing documentation you would like more information > about a value or about the result of an expression, use the "i" helper > instead > ``` > > I'd like to propose allowing arbitrary expressions in `h` so that we teach > people to copy-paste whatever they are looking at to learn more: > > ``` > iex> h 1 + 2 > The "h" helper expects a Module, Module.fun or Module.fun/arity, got: 1 + 2 > > To learn about + operator, do: > > iex> h Kernel.+ > ``` > > With this I believe it would be easy to learn about otherwise kind of hard > to discover things: > > ```elixir > iex> h %{map | k: v} # info on map update syntax > iex> h ~D[2025-01-01] # info on sigil_D > iex> h @compile # info on module attributes and/or that particular > one > iex> h &is_atom/1 # info on captures > iex> h a..b > iex> h _ > ``` > > Another idea, and this is probably a bridge to far, is special casing in > the IEx evaluator `h` so that: > > ``` > iex> h & > ``` > > just works (as opposed to wait until the expression is completed). > > I believe the implementation should be fairly straightforward: > > ``` > diff --git a/lib/iex/lib/iex/helpers.ex b/lib/iex/lib/iex/helpers.ex > index 8964954aa..673f3abca 100644 > --- a/lib/iex/lib/iex/helpers.ex > +++ b/lib/iex/lib/iex/helpers.ex > @@ -360,6 +360,22 @@ def h() do > iex> h(Enum.all?) > > """ > + defmacro h({:+, _, [_, _]} = ast) do > + puts_error(""" > + The "h" helper expects a Module, Module.fun or Module.fun/arity, got: > #{Macro.to_string(ast)} > + > + To learn about + operator, do: > + > + iex> h Kernel.+ > + """) > + > + dont_display_result() > + end > + > + defp puts_error(string) do > + IO.puts(IEx.color(:eval_error, string)) > + end > + > defmacro h(term) do > quote do > IEx.Introspection.h(unquote(IEx.Introspection.decompose(term, > __CALLER__))) > ``` > > -- > 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-core+unsubscr...@googlegroups.com. > To view this discussion visit > https://groups.google.com/d/msgid/elixir-lang-core/D569DB44-82DA-415B-9DA9-36509C179497%40wojtekmach.pl > <https://groups.google.com/d/msgid/elixir-lang-core/D569DB44-82DA-415B-9DA9-36509C179497%40wojtekmach.pl?utm_medium=email&utm_source=footer> > . > -- 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-core+unsubscr...@googlegroups.com. To view this discussion visit https://groups.google.com/d/msgid/elixir-lang-core/CAGnRm4Km1YRZc%3Do1Gb6bH%3D0Nqqi%3DP86hLf49sGtCD37ytdDm7w%40mail.gmail.com.