Comments (8)
The problem is that struct
doesn't support covariance. So there's no easy fix for this.
For example this below won't work:
public static void Foo(Some<IEnumerable<string>> val)
{
}
public void SomeCastTest1()
{
var some = new Some<List<string>>(new List<string>() { "a", "b", "c" });
Foo(some);
}
But this will:
public void SomeCastTest2()
{
Foo(new List<string>() { "a", "b", "c" });
}
So you could either not pass Some<T>
into functions, and only use them to protect function parameters. Or, you could manually extract the .Value
public void SomeCastTes3()
{
var some = new Some<List<string>>(new List<string>() { "a", "b", "c" });
Foo(some.Value);
}
Or you could implement your own cast method:
public static Some<R> Cast<T, R>(Some<T> value) where T : R =>
value.Value;
The problem with that is the type-inference system is poor in c# so you end up with lots of type boilerplate:
Foo(Cast<List<string>,IEnumerable<string>>(some));
So unfortunately there aren't any perfect solutions here.
from language-ext.
Hi.
I think I've tried what you suggested...
public void SomeCastTes3()
{
var some = new Some<List<string>>(new List<string>() { "a", "b", "c" });
Foo(some.Value);
}
But it doesn't seem to be acceptable to the compiler...
from language-ext.
I think I may have found a solution...
public static Some<T> ToSome<T>(this T value) { return Create(value); }
Then to fix the error in the previous screenshot I can do filterMut.Value.ToSome<IFilter<T>>()
in c# 5, which is not too messy.
from language-ext.
The implicit type conversion also seems to fail at times when no unwrapping is required, e.g...
ToImmutable()
returns an IFilterIm<T>
but the compiler complains about implicitly converting this to a Some<IFilterIm<T>>
I wonder if this is some implicit casting limitation related to Generics?
However, using the static ToSome()
method I proposed above without any type annotation makes it acceptable to the compiler.
from language-ext.
Hi. Wondering if you have any further thoughts on this?
from language-ext.
Hi @donners77 ,
If I'm honest I haven't spent too much time thinking about it, no. I'd be slightly reluctant to add ToSome()
as an extension method to T
, mainly because it then pollutes the intellisense for everything, and I think that's a bit too much of an intrusion by a library, especially for those who don't use the Some<T>
type, but are using other aspects of the library.
Obviously ToSome()
works because it's not doing implicit type casting of a T
to a Some<T>
, it's passing a T
to the constructor of Some<T>
where the implicit type casting of T
happens. Until struct
can do covariant implicit type-casting I think you're stuck with ToSome()
as a solution. You'll just need to create it yourself.
I guess one way could be to put the extension method into its own namespace. Perhaps LanguageExt.SomeHelp
. That would mean you could opt into it. I guess I'd be amenable to this idea. What do you think?
from language-ext.
Hi. I don't mind. Happy to just implement it for myself. Happy for it to be in the namespace you suggested if you think other people will need it.
from language-ext.
Cool, I've now added this to the LanguageExt.SomeHelp
namespace. Thanks for the suggestion :)
from language-ext.
Related Issues (20)
- FileIO bug for Test when opening new file for writing
- Directory IO bug for Test when enumerating files
- Simple Kickstart Project available? CodeGen tool is outdated? dotnet8 HOT 1
- Implicit conversion of int? to Option<int> HOT 2
- SequenceParallel performance degradation in 4.4.8 HOT 1
- Refit.ApiException: 'An error occured deserializing the response.'
- Constructor not found for LanguageExt.LongRange HOT 1
- Alternative to inherit from Fin and other monads
- EitherAsync is missing HOT 4
- Add `DoAsync` to Either types (and maybe others) HOT 2
- Release build fails for a big application HOT 5
- v5 resource tracking issue? HOT 6
- Latest v5 alpha IEnumerable<T> ToSeq() extension is missing HOT 4
- v5 await more than a single forked IO, similar to Task.WhenAll HOT 3
- Higher-Kinded Types in Algebraic Free Monads with language-ext?
- Either.Bind triggers memory allocation unlike Either.BindLeft HOT 10
- Missing release notes for 4.4.9 HOT 1
- v5 - Use custom sub-type of Expected losses all custom properties after RunAsync HOT 6
- Use of subtype of Expected throws InsufficientExecutionStackException due to self referencing Head HOT 1
- Extensions: result.GetValueOrDefault() and result.GetValue()
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from language-ext.