How to construct `Either<L, R>` without implicit conversions?
See original GitHub issueI’m having an issue where I’m being forced not to use implicit conversions:
public interface X { }
public class Y : X { }
Either<string, X> a = new Y(); // compiles
Either<string, X> b = new Y() as X; // Cannot convert initializer type 'X' to target type 'Either<string, X>'
This looks like a compiler bug in IMO, but I’m hesitant to rush to such a harsh conclusion.
In any case, because I have an instance of an interface, and not of the concrete class, I have to construct an Either
using some other means.
Maybe I’m missing something, but the constructors are private, and the two static helpers Right
and Left
are internal.
Issue Analytics
- State:
- Created 7 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Being explicit about implicit conversions
Another issue is that there might be lots of injective homomorphisms foo -> bar , but no canonical one. For example, there are...
Read more >How can I prevent implicit conversion among `std::function ...
Letting the appropriate std::function objects be constructed implicitly obviously fails – alternatives (explicitly construct std::function ...
Read more >Implicit conversion with auto constructors - Pitches
One thing I found easier in Kotlin is that you can just write code in sequences/streams/arrays and have them all implicitly convert to...
Read more >Proposed Changes and Restrictions For Implicit Conversions
This Pre-SIP proposes to go ahead with restricting implicit conversions. The topic has already been extensively discussed in Can We Wean ...
Read more >Implicit Conversions in C: The Hidden Enemy - YouTube
Implicit conversions are dangerous: they are not directly apparent in the source code and the rules that govern them are quite intricate; ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@dcastro It’s because implicit operators don’t work with interfaces, it’s a limitation of C#. In your IEnumerable example instead use the lang-ext built-in
List<T>()
Make sure you’re
using static LanguageExt.Prelude
. If not, then you can directly use theEmpty
field on theLst<T>
type:If you want to do similar behaviour yourself, try this:
That avoids the allocation and implicitly casts.
I’m unclear on why you aren’t using the one that compiles?
The static functions you’re after for explicitly defining left and right are: Prelude.Left<string, X>(…) and Prelude.Right<string, X>(…)
On Thu, 4 Aug 2016 at 11:24 Diogo Castro notifications@github.com wrote: