Curl Global Community
type-switch で Array-of 、HashTable-of を判別したい - Printable Version

+- Curl Global Community (https://communities.curl.com)
+-- Forum: Discussions (https://communities.curl.com/forumdisplay.php?fid=1)
+--- Forum: General Curl questions (https://communities.curl.com/forumdisplay.php?fid=2)
+--- Thread: type-switch で Array-of 、HashTable-of を判別したい (/showthread.php?tid=680)



type-switch で Array-of 、HashTable-of を判別したい - umemura - 12-26-2012

type-switch でクラス型の判定を行う際、
配列(Array-of)や、ハッシュテーブル(HashTable-of)を判別するにはどうすればよいですか。

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

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



RE: type-switch で Array-of 、HashTable-of を判別したい - ysugahara - 12-27-2012

type-switch は isa 比較なので、パラメータの違う型を同じように扱うのは難しいと思います。




RE: type-switch で Array-of 、HashTable-of を判別したい - wbardwell - 12-28-2012

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.