Scalar/Iterable conversion
It seems that a higher rate of reuse is possible between Scalar and Iterable decorators if there's a clear way
to convert one to another.
Proposal:
Let's introduce
-
IterableOfScalar(Scalar<T>) -
ScalarOfIterable(Iterable<T>)
A more radical change would be to extend Scalar<T> with Iterable<T>,
which would make iterable.* decorators such as Mapped applicable to any Scalar.
I.e new ScalarOfIterable<>(new Mapped<>(..., new ScalarOf<>(...))) // no need for scalar.Mapped
@andreoss IMHO extend Scalar<T> with Iterable<T> is no way but I'd like to ask you:
- Can we add a new ctor in
IterableOfto put aScalar<T>instead of create a new class? - Can we add a new ctor in
ScalarOfto put aIterable<T>instead of create a new class?
@fabriciofx Overloaded ctors would be ambiguous
-
IterableOf<>(Scalar<T>)could be as wellIterable<Scalar<T>> -
ScalarOf<>(Iterbale<T>)could beScalar<Iterable<T>>
@andreoss how would ScalarOfIterable behave for a scalar with multiple values?
I like the idea anyway, it makes sense!
Concerning constructors vs classes, it's always been a problem to decide between the two in many cases, we will see if it is possible to do it with constructor but as you said there is the ambiguity problem, so introducing classes should be acceptable :)
@victornoel I believe ScalarOfIterable should work only on single-element Iterable, thowring NoSuchElement and IllegalArgumentExeception otherwise. Scalar which iterates over Iterable doesn't make much sense
@andreoss
Scalarwhich iterates overIterabledoesn't make much sense
Yes, that was my main problem with it.
I believe
ScalarOfIterableshould work only on single-elementIterable, thowringNoSuchElementandIllegalArgumentExeceptionotherwise.
So if the Iterable is empty, NoSuchElement or IllegalArgumentException is good, but if there is more than one element, I don't think it is really needed to test for it.
And so in that case, there is already FirstOf (we should just add a constructor taking only an Iterable).
@andreoss let's do the following, what do you think:
- using
FirstOftaking a singleIterableto get the functionality of your proposedScalarOfIterable - continue using an
Iteratorto have something to iterate over anIterable - introducing
IterableOfScalartaking aScalaras your propose- Too bad
IterableOfalready takes aScalarofIterator, Java and its type erasure really sucks sometimes
- Too bad
As for having Scalar extend Iterable, this is not such a bad idea actually, a Scalar is an Iterable of exactly one element. But I'm always worried about introducing more methods to a type hierarchy, even if they are just interfaces. So let's go with the delegation instead of the inheritance :)