ฉันจะออกจากที่นี่เพื่อเห็นแก่ใครบางคนหวังว่าจะหาเวอร์ชั่นที่มีสติจริง ๆ ในขณะนี้ยังไม่มีคำตอบที่ดีเกี่ยวกับเรื่องนี้เกิดขึ้นบน google ...
defmodule Util do
    def typeof(self) do
        cond do
            is_float(self)    -> "float"
            is_number(self)   -> "number"
            is_atom(self)     -> "atom"
            is_boolean(self)  -> "boolean"
            is_binary(self)   -> "binary"
            is_function(self) -> "function"
            is_list(self)     -> "list"
            is_tuple(self)    -> "tuple"
            true              -> "idunno"
        end    
    end
end
เพื่อความสมบูรณ์กรณีทดสอบ:
cases = [
    1.337, 
    1337, 
    :'1337', 
    true, 
    <<1, 3, 3, 7>>, 
    (fn(x) -> x end), 
    {1, 3, 3, 7}
]
Enum.each cases, fn(case) -> 
    IO.puts (inspect case) <> " is a " <> (Util.typeof case)
end
นี่คือทางออกของโปรโตคอล ฉันไม่แน่ใจว่าพวกเขาเร็วขึ้นหรือไม่ (ฉันหวังว่าพวกเขาจะไม่วนซ้ำทุกประเภท) แต่มันก็น่าเกลียดมาก (และเปราะบางถ้าพวกเขาเพิ่มหรือลบประเภทพื้นฐานหรือเปลี่ยนชื่อมันจะทำลายมัน)
defprotocol Typeable, do: def typeof(self)
defimpl Typeable, for: Atom, do: def typeof(_), do: "Atom"
defimpl Typeable, for: BitString, do: def typeof(_), do: "BitString"
defimpl Typeable, for: Float, do: def typeof(_), do: "Float"
defimpl Typeable, for: Function, do: def typeof(_), do: "Function"
defimpl Typeable, for: Integer, do: def typeof(_), do: "Integer"
defimpl Typeable, for: List, do: def typeof(_), do: "List"
defimpl Typeable, for: Map, do: def typeof(_), do: "Map"
defimpl Typeable, for: PID, do: def typeof(_), do: "PID"
defimpl Typeable, for: Port, do: def typeof(_), do: "Port"
defimpl Typeable, for: Reference, do: def typeof(_), do: "Reference"
defimpl Typeable, for: Tuple, do: def typeof(_), do: "Tuple"
IO.puts Typeable.typeof "Hi"
IO.puts Typeable.typeof :ok