Skip to content
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

Merged
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions lib/elixir/lib/process.ex
Original file line number Diff line number Diff line change
Expand Up @@ -839,13 +839,17 @@ defmodule Process do
nilify(:erlang.process_info(pid))
end

@type process_info_item :: atom | {:dictionary, term}
@type process_info_result_item :: {process_info_item, term}

@doc """
Returns information about the process identified by `pid`,
or returns `nil` if the process is not alive.

See `:erlang.process_info/2` for more information.
"""
@spec info(pid, atom | [atom]) :: {atom, term} | [{atom, term}] | nil
@spec info(pid, process_info_item | [process_info_item]) ::
process_info_result_item | [process_info_result_item] | nil
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@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?)

Copy link
Contributor Author

@michallepicki michallepicki Apr 22, 2024

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', _}, _}

def info(pid, spec)

def info(pid, :registered_name) do
Expand All @@ -856,7 +860,7 @@ defmodule Process do
end
end

def info(pid, spec) when is_atom(spec) or is_list(spec) do
def info(pid, spec) do
nilify(:erlang.process_info(pid, spec))
end

Expand Down