Scala Either : simplest way to get a property that exists on right and left -
i have template using valueobject might 1 of 2 flavours depending on used in our app. importing either:
valueobject: either[ objecta, objectb ]
both objects have identically named property on them retrieve calling
valueobject.propertya
which doesn't work.
what concise/ best way of doing this?
assuming 2 objects have same type (or supertype / trait) defines property - can use merge
returns left if exists , right otherwise, lowest common type of both:
scala> class myclass { | def propertya = 1 | } defined class myclass scala> val e1: either[myclass, myclass] = left(new myclass) e1: either[myclass,myclass] = left(myclass@1e51abf) scala> val e2: either[myclass, myclass] = right(new myclass) e2: either[myclass,myclass] = right(myclass@b4c6d0) scala> e1.merge.propertya res0: int = 1 scala> e2.merge.propertya res1: int = 1
Comments
Post a Comment