-
Notifications
You must be signed in to change notification settings - Fork 3.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Expose getting process dictionary value for specific key in Process.info/2 #13505
Expose getting process dictionary value for specific key in Process.info/2 #13505
Conversation
…nfo/2 This functionality is present since OTP 26.2: erlang/otp#7707
9fefe58
to
8e53a61
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
lib/elixir/lib/process.ex
Outdated
@@ -834,7 +837,7 @@ defmodule Process do | |||
|
|||
See `:erlang.process_info/1` for more information. | |||
""" | |||
@spec info(pid) :: keyword | nil | |||
@spec info(pid) :: [process_info_result_item] | nil |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nit] Maybe this one is too imprecise here? Since the key will always be an atom, keyword
might be more accurate.
Or should we refer :erlang.process_info_result_item()
which is a bit more precise? But dialyzer would generalize to atom
anyway.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this function can actually return individual dictionary keys? 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nit] Maybe this one is too imprecise here? Since the key will always be an atom, keyword might be more accurate.
I don't think this function can actually return individual dictionary keys? 🤔
Good points about this function clause, I will revert this change to info/1 spec
Or should we refer :erlang.process_info_result_item() which is a bit more precise? But dialyzer would generalize to atom anyway.
Unfortunately the type is not exported, Dialyzer says:
lib/process.ex:840:30: Unknown type erlang:process_info_result_item/0
lib/elixir/lib/process.ex
Outdated
def info(pid, {:dictionary, key}) do | ||
nilify(:erlang.process_info(pid, {:dictionary, key})) | ||
end | ||
|
||
def info(pid, spec) when is_atom(spec) or is_list(spec) do | ||
nilify(:erlang.process_info(pid, spec)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would actually remove the specific clauses. Now that Erlang/OTP has good error messages, they will probably be more decent than function clause error?
def info(pid, {:dictionary, key}) do | |
nilify(:erlang.process_info(pid, {:dictionary, key})) | |
end | |
def info(pid, spec) when is_atom(spec) or is_list(spec) do | |
nilify(:erlang.process_info(pid, spec)) | |
def info(pid, spec) do | |
nilify(:erlang.process_info(pid, spec)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On Erlang 26.2.2 the error looks like:
iex(1)> Process.info(self(), :unknown)
** (ArgumentError) errors were found at the given arguments:
* 2nd argument: invalid item or item list
:erlang.process_info(#PID<0.114.0>, :unknown)
(elixir 1.17.0-dev) lib/process.ex:864: Process.info/2
iex:1: (file)
We can rely on better ArgumentError messages on newer OTP
lib/elixir/lib/process.ex
Outdated
@spec info(pid, process_info_item | [process_info_item]) :: | ||
process_info_result_item | [process_info_result_item] | nil |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@spec info(pid, process_info_item | [process_info_item]) :: | |
process_info_result_item | [process_info_result_item] | nil | |
@spec info(pid, process_info_item) :: process_info_result_item | nil | |
@spec info(pid, [process_info_item]) :: [process_info_result_item] | nil |
I think this could work as well and be better for documentation (althoguh maybe Dialyzer would unify these types internally?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pleasantly surprised to report that this improves the spec for Dialyzer as well:
for {:should_warn, _} = Process.info(self(), [:some_atom])
:
The pattern
{'should_warn', _} can never match the type
'nil' | [{atom() | {'dictionary', _}, _}]
and for [{:should_warn, _}] = Process.info(self(), :some_atom)
The pattern
[{'should_warn', _}] can never match the type
'nil' | {atom() | {'dictionary', _}, _}
💚 💙 💜 💛 ❤️ |
This functionality is present since OTP 26.2:
erlang/otp#7707
Discussed at #12857 (comment)