You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
REQUEST: Pattern matching/better type inferencing with generics. #5023
It sounds like you want the bounds of type variables to participate in type inference. I can see us taking such a well-specified and prototyped change.
It sounds like you want the bounds of type variables to participate in
type inference. I can see us taking such a well-specified and prototyped
change.
—
Reply to this email directly or view it on GitHub #5023 (comment).
I think I might be able to take a shot at this. I'm currently toying with a prototype of this that works by promoting generic constraints of inferred type parameters to fake arguments. The idea is to add a 4th step to the phase 2 of the inference that works this way:
For every constraint type of every type parameter that has been successfully fixed a new fake argument is added. The fixed type is used as an argument type, its default value is passed as an argument expression and the formal argument type is set to the constraint type. This is done no more than once per type parameter.
Phase 1 is invoked for the newly added arguments.
Phase 2 body is considered to make some progress if any constraints were promoted, and is considered to make no progress otherwise.
The type inferrer that exists today is able to infer that TContainer is Container and then fails. Here's what happens if the idea I've described is implemented:
We have a type parameter TContainer that's fixed to Container. It has IEnumerable<TItem> constraint which is promoted to a fake argument like this:
Phase 2 continues its loop and is now able to fix TComparand to string
The fake arguments obviously exist only in the type inferrer's imagination =)
So what do you guys think? This seems to be working for the simple scenarios it's designed for, but there's a lot of scary stuff in the type inference that I haven't even begun to understand, so it would be useful to get input from someone who has been familiar with this part of the compiler for more than a day ;)
@gafter It's always possible to have something like M<>("foo"); to disambiguate, it was suggested in multiple posts before, what do you think about it?
Personally, I feel like this feature should be considered it takes away the verbosity of generics and hopefully you will come up with ways to do it! I'm really looking forward to this one.
p.s. A tool to modify the code is a great idea the question is whether people will want to run such a tool.
@gafter Well, can you do M<>("foo") today? afaik it's an error but dunno maybe I'm unaware and it's actually a breaking change, can you please elaborate?
I think adding a new syntax for enabling new type inference features is not a good idea. First, it would be very confusing to have two subtly different type inference mechanisms for M("foo") and M<>("foo"). Second, it doesn't fix the long-term problem of handling the breaking changes: even if the new syntax ships, there can easily be future improvements that will introduce new breaking changes for this new syntax and we will once again have exactly the same problem we're discussing right now.
So I believe introducing an analyzer-like mechanism of detecting and fixing the code affected by the breaking changes is the way to go here, like mentioned in #7850 (comment). I'm really curious about the details of how this is supposed to work though. @gafter, is there an issue/document that tracks/describes this? And if there isn't one, can you tell us what the current thinking on this matter is? I guess it has been discussed by LDM quite a few times during the years of developing the language, so you guys should have some knowledge of what the main challenges are and some ideas on solving them.
Reacted by Joseph Musser, Eyal, Asad Saeeduddin, Kristian Hellang, David Roth, artdedeco and Nenad Vićentić
I think adding a new syntax for enabling new type inference features is not a good idea. First, it would be very confusing to have two subtly different type inference mechanisms for M("foo") and M<>("foo")
I totally agree with you that having a tool to modify the code and accommodate the required changes is way better than introducing a new syntax, simply because if there's no need for a new syntax then no one needs to learn anything new, the old way was always to introduce new syntax to evolve a language if and only if it's necessary so my proposal came before I knew a tool is proposed.
However, in cases that a tool wouldn't be feasible for whatever reason (and in all honesty I don't think that there should be a reason not to change it but still), would you rather not have it at all?
However, in cases that a tool wouldn't be feasible for whatever reason (and in all honesty I don't think that there should be a reason not to change it but still), would you rather not have it at all?
If we're talking about the breaking changes in general, then I'd say it depends on the discoverability of the breaks. If the code just stops compiling after an upgrade, that's okay: you have a clear error and a pretty good idea how to fix it. But some breaking changes are next to impossible to detect manually, even if they are properly documented and understood. The inference changes caused by the current issue is a good example of that. Combing though all the overload calls that could have changed their behavior after recompiling is very difficult and error-prone even if you know the codebase well. So I think it wouldn't be nice to ship such a feature without an automatic way to find the breaks.
I don't think the tool is too difficult to write, though. Even if designing a proper upgrade path that provides a nice user experience across all possible IDEs and console compilers could be tricky, writing a simple Roslyn analyzer + code fix per a breaking change seems quite possible.
The authors of types that implement IEnumerable<T> then have the option of implementing this extra interface with IExplicitEnumerable<T, CustomEnumerator<T>>.
Advantages:
For all types, method invocation against the type itself instead of indirect via the interface (i.e. faster)
For value types, no boxing allocations
It's possible at present if you supply the explicit type parameters:
but that's hideous, especially since you have to remember what the CustomEnumerator<T> class was for any implementing type. Compiler inference as proposed here would take care of that.
+1 for letting the pressure for a backwards-compat analyzer continue to build, rather than relieving it by contorting language features to squeak past backwards-compat issues. @gafter would you folks be more open to proposals with backward compat issues if they came with Roslyn refactorings to address them? It shouldn't be too difficult to write one that addresses the problem you brought up, for example, although I haven't thought about all edge cases.
In order to facilitate that transition, we have started closing language design discussions from the roslyn repo with a note briefly explaining why. When we are aware of an existing discussion for the feature already in the new repo, we are adding a link to that. But we're not adding new issues to the new repos for existing discussions in this repo that the language design team does not currently envision taking on. Our intent is to eventually close the language design issues in the Roslyn repo and encourage discussion in one of the new repos instead.
Our intent is not to shut down discussion on language design - you can still continue discussion on the closed issues if you want - but rather we would like to encourage people to move discussion to where we are more likely to be paying attention (the new repo), or to abandon discussions that are no longer of interest to you.
If you happen to notice that one of the closed issues has a relevant issue in the new repo, and we have not added a link to the new issue, we would appreciate you providing a link from the old to the new discussion. That way people who are still interested in the discussion can start paying attention to the new issue.
Also, we'd welcome any ideas you might have on how we could better manage the transition. Comments and discussion about closing and/or moving issues should be directed to #18002. Comments and discussion about this issue can take place here or on an issue in the relevant repo.
As described in #5023 (comment), the LDM looked at this and found it to be a breaking change, and one that we would therefore be quite unlikely to ever take.
I'm wondering why a syntax like Method<Type1, _, _, _, Type5<_>, _>(args) was not proposed. It seems to be backward compatible - just tell the compiler "well, i have some generic parameters there, could you infer them for me from the context?"
Reacted by navikale, Krzysztof Maczyński, Zhenwei Wu, /人 ⌒ ‿‿ ⌒ 人\ and wiktor-golonka
why was this ruled out as a breaking change ?
it appears the only case where this would be a breaking change is if there is ambiguity, so why not fallback to old behavior of needing to explicitly define all type arguments when there is ambiguity ?
Activity
gafter commentedon Sep 6, 2015
It sounds like you want the bounds of type variables to participate in type inference. I can see us taking such a well-specified and prototyped change.
TonyValenti commentedon Sep 6, 2015
That sounds great! This will make my code so much cleaner. Thank you!
On Sunday, September 6, 2015, Neal Gafter notifications@github.com wrote:
Tony Valenti
gafter commentedon Sep 6, 2015
@TonyValenti I've labeled this "up for grabs" in case someone from the community has the time and interest to take it on.
HellBrick commentedon Nov 7, 2015
I think I might be able to take a shot at this. I'm currently toying with a prototype of this that works by promoting generic constraints of inferred type parameters to fake arguments. The idea is to add a 4th step to the phase 2 of the inference that works this way:
Here's an example to illustrate the idea:
The type inferrer that exists today is able to infer that
TContainerisContainerand then fails. Here's what happens if the idea I've described is implemented:TContainerthat's fixed toContainer. It hasIEnumerable<TItem>constraint which is promoted to a fake argument like this:x, providing us bounds forTItemtype.TItemtoItem.TItemis fixed, we can promote its constraint like this:yTComparandtostringSo what do you guys think? This seems to be working for the simple scenarios it's designed for, but there's a lot of scary stuff in the type inference that I haven't even begun to understand, so it would be useful to get input from someone who has been familiar with this part of the compiler for more than a day ;)
bbarry commentedon Nov 7, 2015
I presume your phase 2 would still fail if
Itemwere this instead:HellBrick commentedon Nov 7, 2015
Yep, it fails for the same reason the following fails today (because in the end it boils down to exactly the same problem):
HellBrick commentedon Nov 8, 2015
I've submitted a PR with the prototype to provide something less abstract to discuss.
MrJul commentedon Nov 16, 2015
I know the Roslyn team is probably busy with Update 1, but it would be nice to get some feedback on @HellBrick's great PR :)
gafter commentedon Nov 16, 2015
@HellBrick We'll take a look at this, thanks!
gafter commentedon Nov 16, 2015
Removing "Up For Grabs" as @HellBrick has grabbed it!
15 remaining items
soyeyalon commentedon Jun 16, 2016
@gafter It's always possible to have something like
M<>("foo");to disambiguate, it was suggested in multiple posts before, what do you think about it?Personally, I feel like this feature should be considered it takes away the verbosity of generics and hopefully you will come up with ways to do it! I'm really looking forward to this one.
p.s. A tool to modify the code is a great idea the question is whether people will want to run such a tool.
gafter commentedon Jun 16, 2016
@eyalsk How does that make this not a breaking change?
TonyValenti commentedon Jun 16, 2016
Because today you have to specify type parameters when you call a
function. You can't simply do Foo<>(Param)
On Thu, Jun 16, 2016 at 10:47 AM, Neal Gafter notifications@github.com
wrote:
Tony Valenti
soyeyalon commentedon Jun 16, 2016
@gafter Well, can you do
M<>("foo")today? afaik it's an error but dunno maybe I'm unaware and it's actually a breaking change, can you please elaborate?HellBrick commentedon Jun 16, 2016
I think adding a new syntax for enabling new type inference features is not a good idea. First, it would be very confusing to have two subtly different type inference mechanisms for
M("foo")andM<>("foo"). Second, it doesn't fix the long-term problem of handling the breaking changes: even if the new syntax ships, there can easily be future improvements that will introduce new breaking changes for this new syntax and we will once again have exactly the same problem we're discussing right now.So I believe introducing an analyzer-like mechanism of detecting and fixing the code affected by the breaking changes is the way to go here, like mentioned in #7850 (comment). I'm really curious about the details of how this is supposed to work though. @gafter, is there an issue/document that tracks/describes this? And if there isn't one, can you tell us what the current thinking on this matter is? I guess it has been discussed by LDM quite a few times during the years of developing the language, so you guys should have some knowledge of what the main challenges are and some ideas on solving them.
soyeyalon commentedon Jun 16, 2016
@HellBrick
I totally agree with you that having a tool to modify the code and accommodate the required changes is way better than introducing a new syntax, simply because if there's no need for a new syntax then no one needs to learn anything new, the old way was always to introduce new syntax to evolve a language if and only if it's necessary so my proposal came before I knew a tool is proposed.
However, in cases that a tool wouldn't be feasible for whatever reason (and in all honesty I don't think that there should be a reason not to change it but still), would you rather not have it at all?
HellBrick commentedon Jun 17, 2016
If we're talking about the breaking changes in general, then I'd say it depends on the discoverability of the breaks. If the code just stops compiling after an upgrade, that's okay: you have a clear error and a pretty good idea how to fix it. But some breaking changes are next to impossible to detect manually, even if they are properly documented and understood. The inference changes caused by the current issue is a good example of that. Combing though all the overload calls that could have changed their behavior after recompiling is very difficult and error-prone even if you know the codebase well. So I think it wouldn't be nice to ship such a feature without an automatic way to find the breaks.
I don't think the tool is too difficult to write, though. Even if designing a proper upgrade path that provides a nice user experience across all possible IDEs and console compilers could be tricky, writing a simple Roslyn analyzer + code fix per a breaking change seems quite possible.
ThatRendle commentedon Jun 21, 2016
This could really optimize LINQ extension methods (and similar).
Example: given this slightly more involved version of
IEnumerableone could write generic forms of LINQ extension methods that look something like this:
The authors of types that implement
IEnumerable<T>then have the option of implementing this extra interface withIExplicitEnumerable<T, CustomEnumerator<T>>.Advantages:
It's possible at present if you supply the explicit type parameters:
but that's hideous, especially since you have to remember what the
CustomEnumerator<T>class was for any implementing type. Compiler inference as proposed here would take care of that.masaeedu commentedon Jul 15, 2016
+1 for letting the pressure for a backwards-compat analyzer continue to build, rather than relieving it by contorting language features to squeak past backwards-compat issues. @gafter would you folks be more open to proposals with backward compat issues if they came with Roslyn refactorings to address them? It shouldn't be too difficult to write one that addresses the problem you brought up, for example, although I haven't thought about all edge cases.
Thaina commentedon Dec 24, 2016
Wish for this too
gafter commentedon Mar 27, 2017
We are now taking language feature discussion in other repositories:
Features that are under active design or development, or which are "championed" by someone on the language design team, have already been moved either as issues or as checked-in design documents. For example, the proposal in this repo "Proposal: Partial interface implementation a.k.a. Traits" (issue 16139 and a few other issues that request the same thing) are now tracked by the language team at issue 52 in https://github.com/dotnet/csharplang/issues, and there is a draft spec at https://github.com/dotnet/csharplang/blob/master/proposals/default-interface-methods.md and further discussion at issue 288 in https://github.com/dotnet/csharplang/issues. Prototyping of the compiler portion of language features is still tracked here; see, for example, https://github.com/dotnet/roslyn/tree/features/DefaultInterfaceImplementation and issue 17952.
In order to facilitate that transition, we have started closing language design discussions from the roslyn repo with a note briefly explaining why. When we are aware of an existing discussion for the feature already in the new repo, we are adding a link to that. But we're not adding new issues to the new repos for existing discussions in this repo that the language design team does not currently envision taking on. Our intent is to eventually close the language design issues in the Roslyn repo and encourage discussion in one of the new repos instead.
Our intent is not to shut down discussion on language design - you can still continue discussion on the closed issues if you want - but rather we would like to encourage people to move discussion to where we are more likely to be paying attention (the new repo), or to abandon discussions that are no longer of interest to you.
If you happen to notice that one of the closed issues has a relevant issue in the new repo, and we have not added a link to the new issue, we would appreciate you providing a link from the old to the new discussion. That way people who are still interested in the discussion can start paying attention to the new issue.
Also, we'd welcome any ideas you might have on how we could better manage the transition. Comments and discussion about closing and/or moving issues should be directed to #18002. Comments and discussion about this issue can take place here or on an issue in the relevant repo.
As described in #5023 (comment), the LDM looked at this and found it to be a breaking change, and one that we would therefore be quite unlikely to ever take.
declard commentedon Nov 9, 2019
I'm wondering why a syntax like
Method<Type1, _, _, _, Type5<_>, _>(args)was not proposed. It seems to be backward compatible - just tell the compiler "well, i have some generic parameters there, could you infer them for me from the context?"ahmednfwela commentedon Dec 7, 2021
why was this ruled out as a breaking change ?
it appears the only case where this would be a breaking change is if there is ambiguity, so why not fallback to old behavior of needing to explicitly define all type arguments when there is ambiguity ?
so for this case:
Mwith ambiguous constraintsPreferMostStrictTypeArgumentOverload)M<T,U>overloadM(object)overloadCyrusNajmabadi commentedon Dec 7, 2021
@ahmednfwela language design happens over at dotnet/csharplang now.