Created
March 10, 2023 09:29
-
-
Save mayel/994e34d6bfd7c1f61c1b4dc994efcfd1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Types do | |
def typeof(nil), do: nil | |
def typeof(false), do: false | |
def typeof(true), do: true | |
def typeof(atom) when is_atom(atom) do | |
if function_exported?(atom, :__info__, 1) || Code.ensure_loaded?(atom) do | |
if function_exported?(atom, :__struct__, 0), do: :struct, else: Module | |
else | |
Atom | |
end | |
end | |
def typeof(list) when is_list(list) do | |
if Keyword.keyword?(list), do: Keyword, else: List | |
end | |
def typeof(string) when is_binary(string) or is_bitstring(string), do: String | |
def typeof(number) when is_integer(number), do: Integer | |
def typeof(float) when is_float(float), do: Float | |
def typeof(tuple) when is_tuple(tuple), do: Tuple | |
def typeof(function) when is_function(function), do: Function | |
def typeof(pid) when is_pid(pid), do: Process | |
def typeof(port) when is_port(port), do: Port | |
def typeof(reference) when is_reference(reference), do: :reference | |
def typeof(%{__struct__: exception_struct} = exception) when is_exception(exception), do: exception_struct | |
def typeof(exception) when is_exception(exception), do: Exception | |
def typeof(%{__struct__: struct}), do: struct | |
def typeof(struct) when is_struct(struct), do: :struct | |
def typeof(map) when is_map(map), do: Map | |
def typeof(_), do: :unknown | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment