LoginSignup
6
6

More than 5 years have passed since last update.

Converter の startChunkedConversion が不可解

Last updated at Posted at 2014-07-17

自身を読み込んで、各行をちょっと加工して標準出力するスクリプトを書こうとしてこう書いた:

import 'dart:io';
import 'dart:convert';

void main(){
  new File(Platform.script.toFilePath())
    .openRead() // Stream<List<int>> 返すのか
    .transform(SYSTEM_ENCODING.decoder) // じゃあ、List<int> を String に変換して
    .transform(new LineSplitter()) // String を行分割して List<String> に変換して
    .expand((List<String> lines) => lines) // 複数行 List<String> を一行にフラットにしよう
    // ↑=> type 'String' is not a subtype of type 'List<String>' of 'lines'. ←???
    .listen((String line) {
      print('* $line');
    });
}

どうやら LineSplitterStreamTransformer<String, List<String> を継承しているのに、transform メソッドに与えると Stream<List<String>> ではなく Stream<String> を返すようだ。つまり、.expand は必要ないようだ。意味がわからない。

どうもその辺りを漁っていると、ConverterstartChunkedConversion の引数/返り値には、クラスで定義されている S, T がついていないのが原因な気がする:

abstract class Converter<S, T> implements StreamTransformer {

  // ...

  /**
   * Starts a chunked conversion.
   */
  ChunkedConversionSink startChunkedConversion(Sink sink) {
    throw new UnsupportedError(
        "This converter does not support chunked conversions: $this");
  }

  // ...
}

これの影響で、Converter で定義したジェネリック型を使わず自由な型が返せてしまうため、チグハグなインターフェースが出来てしまったようす。

どうしてこうなったんだろう。

6
6
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6