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.