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.

Covariance of immutable containers

See original GitHub issue

Hello! I’ve just started to use Language.Ext.Core and I’ve ran into the small inconvenience - Either<L, R> (like all other container types in this library) is not covariant on its type arguments. In some cases it forces me to cast returned value manually to the base class. Does this design have some purpose? If I understand everything correctly we can make immutable containers covariant essentially for free. But maybe I’ve missed something important… Thank you in advance.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
louthycommented, May 18, 2016

@Hinidu Indeed it’s sometimes a little ugly I’m afraid. To reduce the amount of clutter from casting I tend to use the as operator (even if it’s slightly less efficient):

    public static Either<Error, TValue> GetEither<TKey, TValue>(
        this IDictionary<TKey, TValue> dictionary, TKey key)
    {
        return dictionary.TryGetValue(key)
            .ToEither(() => new KeyNotExistError<TKey, TValue>(key) as Error);
    }

Or you could use MapLeft if you want to be more declarative:

    public static Either<Error, TValue> GetEither<TKey, TValue>(
        this IDictionary<TKey, TValue> dictionary, TKey key)
    {
        return dictionary.TryGetValue(key)
            .ToEither(() => new KeyNotExistError<TKey, TValue>(key))
            .MapLeft(x => x as Error);
    }
0reactions
Hiniducommented, May 18, 2016

@louthy Thank you for suggestions 👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

Immutability and Co/Contravariance - Nami's Tech Blog
There are many reasons to use immutable data structures and you ... Lists (and other containers) not being covariant makes our lives hard....
Read more >
Covariance, Contravariance, and Invariance
In Python, most immutable containers are covariant. Tuples and frozensets (their type is FrozenSet ) are the most significant ones.
Read more >
Why doesn't C++ support covariance in STL containers like ...
So, the reason, STL containers are not covariant, is because C++ doesn't support that. Note that std::vector is mutable, so it cannot be ......
Read more >
Returning immutable.Map with covariant type - scala
I have a Container type that is covariant on its type parameter. class Container[+T](val map: Map[Int, T] ...
Read more >
Immutable Interfaces and Covariant Return Types in Java
Today, let's look at a simple example which shows how the Immutable Interface pattern and also covariant return types can help you write ......
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