question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to construct `Either<L, R>` without implicit conversions?

See original GitHub issue

I’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:closed
  • Created 7 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
louthycommented, Aug 4, 2016

@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>()

        Either<Exception, IEnumerable<T>> F<T>()
        {
            return List<T>();
        }

Make sure you’re using static LanguageExt.Prelude. If not, then you can directly use the Empty field on the Lst<T> type:

        Either<Exception, IEnumerable<T>> F<T>()
        {
            return Lst<T>.Empty;
        }

If you want to do similar behaviour yourself, try this:

        static class Empty<T>
        {
            public static readonly T[] Enumerable = new T[0];
        }

        Either<Exception, IEnumerable<T>> F<T>()
        {
            return Empty<T>.Enumerable;
        }

That avoids the allocation and implicitly casts.

1reaction
louthycommented, Aug 4, 2016

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:

I’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 https://github.com/louthy/language-ext/blob/master/LanguageExt.Core/Either.cs#L627 and Left https://github.com/louthy/language-ext/blob/master/LanguageExt.Core/Either.cs#L631 are internal.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/louthy/language-ext/issues/121, or mute the thread https://github.com/notifications/unsubscribe-auth/AB5kk9SEvRWYQZgsZtJf74d31tJ4DPiwks5qcb3ngaJpZM4JcjoM .

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found