Curl Global Community

Full Version: type-switch で Array-of 、HashTable-of を判別したい
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
type-switch でクラス型の判定を行う際、
配列(Array-of)や、ハッシュテーブル(HashTable-of)を判別するにはどうすればよいですか。

目的は、あるインスタンスの中身が配列、もしくはHashTableであれば、
コンテナループで中の要素を確認したいからです。

Array-of int も、Array-of String も同じように扱いたいのですが、
type-switch では CASE 文をパラメタの数だけ用意しなければいけないのでしょうか。
type-switch は isa 比較なので、パラメータの違う型を同じように扱うのは難しいと思います。

type-switch lets you check the type of something and bind a local variable to the type, so you use type-switch to check for each specific type, and write specific code for Array-of int or Array-of String.

It is possible to write the code that looks at the type of the parameters in a ParameterizedType, so you can use type-of to get the type of your object, and then do type-switch on that Type to see if it is a ParameterizedType and then look at ParameterizedType.positional-type-parameters and ParameterizedType .keyword-type-parameters. But if you do not cast the object to a specific type then you will still be doing "any" operations, which are slow, but if that is OK for your use then you can use that technique to write more generic code.