-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RFC: Add a Unit type to the System namespace #33083
Comments
I deny their utility. Having a data type that contains no data does nothing useful; all it does is cause confusion. We have |
Often not used with methods though. Often used as part of lambda based constructs. Essentially tends to be used with more c# functional libraries. |
I like this, but would prefer to be able to pass |
This is a bad idea that comes up over and over again every few months, generally by people who appear to be unaware of the concept of minus 100 points, because they never provide any explanation as to why The distinction between methods that have a return type and methods that don't is a useful one. Blurring that line would be harmful. For example, LINQ methods are written, by design, in a pure-functional style, taking |
|
Yes, it really is! Especially those of us who are aware of its harmful nature!
No, not really. Because if it exists in a standardized form in the BCL -- rather than its current status as some niche thing that only a few FP crazies on the fringe of the ecosystem care about and they can't even settle on a single implementation -- then it's going to get used, and sooner or later I'll end up in a situation where I have to deal with it because some code I need that was written by someone else has I recently had exactly this happen to me with |
It's a struct to avoid allocations. Frameworks often use it when they don't care about the value anymore in a chain of methods. For example in reactive it means you may have processed the data in the chain and now you just care about something has happened. Without having a common struct for providing this you wouldn't be able to combine together chains of reactive events together. |
@glennawatson That seems really short-sighted. Even if you don't care about the data you're processing anymore, how do you know that whoever's next in the chain won't? |
Well. It's not necessarily data in the reactive world you care about. It's about something has happened. Eg a mouse click. You don't want your view model knowing about a mouse event Arg. You only care that something is invoking my logic and my command should run. You can then change it so you respond to another thing happen eg a key down and you don't have to change your code base because all the events are telling you something has happened (eg unit) |
Then why does all this data on MouseEventArgs exist in the first place? This sounds like an argument made by someone unfamiliar with Chesterton's Fence. Just because you don't think it's useful right this moment doesn't mean it should be thrown away. |
I think this will be cool if it can come on board in System namespace |
MouseEventArgs is useful in the context of the view in my example. Maybe you want to have it only respond when there is a mouse event between certain mouse coordinates. You can do a Where() statement restricting that. In the view model how that happens is not a concern for it. Now you can have your view model running on xamarin forms and wpf. A lot of users do. |
@masonwheeler by your reasoning we should he passing the MouseEventArgs everywhere and never stop using it. |
I don't think this is true and calling us "fringe crazies" is unnecessarily insulting. As for it being niche:
Basically every language in common use except C# and Java (F# also has one).
How does not having a @masonwheeler I'd suggest using System.Reactive for a while and seeing whether you have a need for a |
It doesn't.
|
I like and support the idea as a user of both System.Reactive and MediatR it is frustrating having to interop between the two. I kinda wish it was added with This feels very similar to the ML.NET types ( |
You can compose your observable pipeline and split it before the call to transform to |
It doesn't, but it does discourage it. Taking that discouragement away would not be an improvement. |
It is easy already, just start an anonymous method that has side effects. The return type does not change anything. People write ForEach extensions methods themselves and for some reason the |
@jspuij Yes, and the one on |
but since an Action can have just as many side-effects as a Func<T, Unit>, I do not see the relevance to this issue of the existing proliferation of Units. |
The key advantage that
|
Come to think of it, in most functional languages Unit is equivalent to the 0-element tuple. MAybe we are just after the System.ValueTuple struct after all. Edit: I was not the only one who realized this: |
@masonwheeler here is an example of when Unit is useful - and it's not a functional programming fringe https://github.com/jbogard/MediatR/blob/c1ad66ef52434a22c10a0de5e060d13b185ef80b/src/MediatR/IRequestHandler.cs#L28 Could you provide an example of how having Unit is harmful, and can allow people to accidentally "pass a lambda with side effects"? Thanks |
@jspuij interesting point. Wonder if that could use like a |
It is already used to signal an anonymous method without arguments... Would not be very difficult to get used to. |
Just an FYI, this has come up a lot in various C# language repos. Here's a recent one, to support zero-index value tuples: And unit-as-valuetuple: etc. I looked for something "official" but my options were pretty limited, and still are. I don't really want to use |
Oh and this one, which is a much longer discussion of first-class support for an actual |
That seems as a pretty well thought out proposal. Upvoting it too. |
Please make this happen. A built-in |
For what it's worth, absent following links to Wikipedia, my assumption was that a type named Unit is either a strongly-typed string ("Kilometer", "Mile", "Gram", etc) or a combination of a value and a string/strongly-typed-string ( It may be the term of art in functional programming, but it's probably something that would need a different name to fit in with our mostly-procedural libraries. |
@bartonjs it's not functional-language-specific, but type-theory-specific. It just so happens that C# picked a different name to match C-based languages, with different semantics and now we have this "fun" |
Okay, so maybe I have my domain names wrong. But I just wanted to point out that I thought of unit in the sense of a unit of measure, and was surprised to find out that it meant something else. So I'm suggesting that the name is likely going to be a hindrance. Aside from the semi-obvious |
I'd so it is no more hindering than |
|
That's no reason to not consider |
@DavidArno While I really like types like
Regarding naming I favor |
Unit isn't a value, it's a type, so |
@StefanBertels, that is a fair point. I'll say no more on DUs here. 👍 |
The right place for such a proposal is dotnet/runtime. The dotnet/standard repo does not create API - it defines a group of API and infrastructure around it. |
FWIW we have versions of this in the runtime itself |
void is System.Void struct in dotnet |
Another point of contention I've ran into and see others run into is It seems to me The posts on this thread from years ago suggest adding this to .NET standard. That ship has probably already sailed. That said, using |
Abstract
Unit
s are proliferating in the NetCore ecosystem. Nobody can deny their utility (since you can't return a void) but this does introduce the issue of incompatible structs all calledUnit
provided by different packages.Proposal
Implement a
System.Unit
struct, taking on board the needs of the major libraries providingUnit
s at the moment.Benefits
Type
that can be used interchangeably between the major librariesUnit
for future libraries, preventing yet more proliferationExample
This file is an example of integrating
System.Reactive.Unit
(paging @ghuntley @onovotny ) withMediatr.Unit
(credit @jbogard) andlanguage-ext.Unit
(credit @louthy ).This covers 90% of the Use Cases for
Unit
. If this were implemented in theSystem
namespace with other Types, it would solve a lot of issues and prevent a lot of future issues .The text was updated successfully, but these errors were encountered: