変数の型名を取得する

Elixirで変数の型名を文字列として取得する。iexインタプリタのiコマンドみたいなことがしたかった。

defmodule Typename do
  @spec get(any) :: String.t()
  def get(x) do
    dict = %{
      &is_atom/1      => "atom type",
      &is_binary/1    => "binary type",
      &is_bitstring/1 => "bitstring type",
      &is_boolean/1   => "boolean type",
      &is_float/1     => "float type",
      &is_function/1  => "function type",
      &is_integer/1   => "integer type",
      &is_list/1      => "list type",
      &is_map/1       => "map type",
      &is_number/1    => "number type",
      &is_pid/1       => "pid type",
      &is_reference/1 => "reference type",
      &is_tuple/1     => "tuple type",
    }

    typename_list = Enum.filter_map(
      dict,
      fn {pred, _} -> pred.(x) end,
      fn {_, name} -> name end)
    if Enum.empty?(typename_list) do
      "unknown type"
    else
      Enum.join(typename_list, " && ")
    end
  end
end

x = "hello"
IO.puts "x type is #{Typename.get(x)}"

出力:

x type is binary type && bitstring type

C++1z 非推奨だったauto_ptr, random_shuffle, 古い<functional>の機能を削除

C++1zでは、非推奨(deprecated)になっていた以下の機能が削除されます。既存のコードでこれらの関数を使用していた場合、コンパイルが通らなくなりますのでご注意ください。

古いスマートポインタ

  • std::auto_ptrクラス

C++11から非推奨。

代わりにstd::shared_ptrクラスやstd::unique_ptrクラスを使用してください。

古いバインダ

  • std::bind1st()関数
  • std::bind2nd()関数
  • std::binder1stクラス std::binder2ndクラス

C++11から非推奨。

代わりにstd::bind()関数もしくはラムダ式を使用してください。

関数ポインタから関数オブジェクトへの変換

  • std::ptr_fun()関数
  • std::pointer_to_unary_functionクラス std::pointer_to_binary_functionクラス

C++11から非推奨。

first_argument_typesecond_argument_typeといった型が必要なくなったため、これらの関数を使うことがなくなりました。

メンバ関数から関数オブジェクトへの変換

  • std::mem_fun()関数
  • std::mem_fun_ref()関数
  • std::mem_fun_tクラス
  • std::mem_fun1_tクラス
  • std::mem_fun_ref_tクラス
  • std::mem_fun1_ref_tクラス
  • std::const_mem_fun_tクラス
  • std::const_mem_fun1_tクラス
  • std::const_mem_fun_ref_tクラス
  • std::const_mem_fun1_ref_tクラス

C++11から非推奨。

代わりにstd::mem_fn()関数、std::bind()関数、ラムダ式といった機能を使用してください。

古いシャッフル関数

C++14から非推奨。

代わりにstd::shuffle()関数を使用してください。

参照

お断り

この記事の内容は、C++1zが正式リリースされる際には変更される可能性があります。正式リリース後には、C++日本語リファレンスサイトcpprefjpの以下の階層の下に解説ページを用意する予定です。