scala - How can I define a type to present a stream which can provide integers, but also may fail? -
suppose want define "numberloader", load integers place demand, can give type:
type numberloader = stream[integer] but may throw errors when loading (say, loads integers via network computer), there should networkerror in type.
finally, defined as:
type numberloader = stream[either[networkerror, integer]] if seems work, feel little strange. one?
you want represent
a stream of can either networkerror or integer stream[ either[ networkerror, integer]] so type looks appropriate , well-suited.
you can alternatively use future or try in place of either, lose flexibility of specifying kind of exception expect, both future , try failures hold generic throwable.
Comments
Post a Comment