2017-01-20 32 views
1

このコードはダートVM(1.22.0-dev.9.0)上で動作しますが、DartPad(不明なバージョン)の作業を行いません。中Iterable <DeclarationMirror>をIterable <MethodMirror>にキャストできないのはなぜですか?

import 'dart:mirrors'; 

class Thing { 
    Thing(); 
} 

void g(ClassMirror c) { 
    var constructors = c.declarations.values 
     .where((d) => d is MethodMirror && d.isConstructor) as Iterable<MethodMirror>; 
    print(constructors); 
} 

void main() { 
    g(reflectClass(Thing)); 
} 

結果:

Unhandled exception: 
type 'WhereIterable<DeclarationMirror>' is not a subtype of type 'Iterable<MethodMirror>' in type cast where 
    WhereIterable is from dart:_internal 
    DeclarationMirror is from dart:mirrors 
    Iterable is from dart:core 
    MethodMirror is from dart:mirrors 

#0  Object._as (dart:core-patch/object_patch.dart:76) 
#1  g (file:///google/src/cloud/srawlins/strong/google3/b.dart:9:55) 
#2  main (file:///google/src/cloud/srawlins/strong/google3/b.dart:14:3) 
#3  _startIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:261) 
#4  _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:148) 

(しかし、 。

0123: (MethodMirror on 'Thing')DartPad結果)

注私はお互いを実装するいくつかのクラスをクラフトに手、そして同じことをすれば、それが動作することできれいに印刷さ

(Instance of '_MM')

答えて

0

.where()によって返された反復可能なだけMethodMirrorのインスタンスを含めることができるという理由だけでは、キャストを許可していません。タイプは、c.declarations.valuesDeclarationMirror)から伝播されます。 DeclarationMirrorMethodMirrorにキャストできますが、Iterable<DeclarationMirror>Iterable<MethodMirror>のキャストは、これらのイテラブルにis-a関係がないため無効です。

dart2jsによってJSに組み立てられたときに、いくつかのジェネリックタイプが削除されたようです。これがDartPadで動作する理由です。

これを簡単https://github.com/dart-lang/sdk/issues/27489

+0

ソリューションのおかげで、バグのリンクを作成するためのオープン問題があり

import 'dart:mirrors'; class Thing { Thing(); } void g(ClassMirror c) { var constructors = new List<MethodMirror>.from( c.declarations.values.where((d) => d is MethodMirror && d.isConstructor)); print(constructors); } void main() { g(reflectClass(Thing)); } 

のような新しいList<MethodMirror>を作成することができます! –

関連する問題