-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Champion: using aliases for any types (VS 17.6, .NET 8) #4284
Comments
I'll dual champion this :-) Tbh, I'm fine with any proposal of the form: |
I'm also willing to do the impl work here. I can think of some nice ways to do it that would be easy to add. |
Closing a duplicate of #1239. See https://github.com/dotnet/csharplang/blob/a3d67d141501101af9dbb9e8192ca8b4c717e316/meetings/2020/LDM-2020-07-13.md#generics-and-generic-type-parameters-in-aliases, where we explicitly mentioned adding support for tuple syntax, and https://github.com/dotnet/csharplang/blob/87adc18115ce4b4429610af1330ca4430219ddd6/meetings/2020/LDM-2020-09-28.md#proposal-support-generics-and-generic-type-parameters-in-aliases, where the timing was discussed. |
Not quite following how the other issue subsumes this one -- is it purely because the proposed syntactic alterations include |
I would prefer we break out this narrower proposal. The linked proposal is very broad and contains extra things i don't think are necessary to solve the core problem. |
Personally, my read of the opinion the last time we talked about this was that we're not sure how far we want to go. The proposal itself was relatively simple: permit more things in using aliases. This includes generic types, keywords, and tuples (@agocke this is the part that subsumes this proposal). I wouldn't want to consider a narrower proposal without considering the broader one at the same time. |
I'm going to reactivate this. POtentially bringing back to LDM to discuss. This is a narrower slice than the braoder topic we originally talked about. Importantly, this is extremely cheap to implement and solves a broad swath of the cases, without having to go into much more complex areas like |
Prelim LDM notes:
|
@CyrusNajmabadi does this include aliases for "open" generic types, e.g.
or
|
@Jehoel It does not. |
Based on the proposal, would anonymous types also be allowed? (I don't see where (value) tuples are in the standard to define E.g. allow replacing:
And then referencing
And then referencing I guess there is no way to write an anonymous type as a type as oppose to a value... |
No, the proposal as written would not allow for this. Anonymous types have no user expressible name/syntax taht you can use to refer to their type. |
...which is kinda silly now in 2022: It's a shame that Anonymous Types were originally C# 2.0 to simplify code and allow C# users to be more expressive without the ceremony of fleshed-out
I do understand why there were these restrictions back in C# 2.0: insufficient dev-time budget for release-day improvements, "less is more", and wanting to see how C# programmers would end-up using anonymous-types to see if further investment in improvements is worthwhile. While it's now clear that C# 4.0's: I made a quick comparison table of the main different kinds of product-type/record/tuple in C# today, which I think demonstrates the gap that ATs fall into:
So looking at the table above, every product-type-kind in C# has its own deal-killers: some of which are unavoidable (e.g. the lack of safety with
|
@Jehoel this appears to be out of scope for this proposal. This proposal is about allowing a using alias to any existing type you can already express in C#. You cannot express anonymous-types in c#, so that's not something covered here. If you'd like that to be possible, please open another issue to track that. If it happens, it would fall out that it would then be supported in an alias. that said... teh point of anonymous types is to be... anonymous. If you're going to give them a name, just write a simple record and you'll be good :)
Sure. that's why we have options. There is no goal in the language, or surrounding ecosystem for one perfect type that solves all needs for all users in all cases. Pick what makes sense per domain :)
This is the repo for the C# language. We are not teh EF core team, nor do we force anyone anywhere to do anything. If you would like another team to make a particular change, you can ask them and they can make an appropriate decision for their domain if that's appropriate. It's not our place, or desire, or ability, to force anything.
Please open tracking discussions on any ideas you have around anonymous types. This issue is not the right place for that discussion. If there is enough interest from the community or an LDM member, it may happen. However, writing off topic posts on unrelated issues is not appropriate and will not change anything here for that other feature. |
This comment was marked as off-topic.
This comment was marked as off-topic.
Please open discussions on anything related to that. that is unrelated to the goal of allowing a using alias to have any current C# type on the RHS of hte alias.
Again, the process for getting changes into the language is to open discussions on things like you'd like to see changed. We prioritize based on value and feedback. Complaining on random other proposals does not change this.
Netmage asked a question if this was on topic or not. I stated it wasn't. It's fine to ask. It's not fine to continue on this :)
No, our issues are for tracking the actual course of a proposal to implementation (please see our repo guidelines), they are not for free-form conversation about different topics. Please follow our rules while you're here, i imagine you'd like peopel to do the same in your repos :) |
Does this feature intend that the type alias is persistent (still valid when accessed from another namespace)?
and in another file I have:
Also, currently the documentation states that when you alias with using, the type names are interchangeable (Unit / System.ValueTuple). Currently the only way to get this outcome (for both cases above) is to derive: Is there a different proposal if this one does not address these ? |
No, this proposal does not change the scope in which aliases are defined. If you want an alias to be defined across an entire project you can use global aliases. |
ITNOA @CyrusNajmabadi Is this proposal, allow use
thanks |
No. It is not. |
@CyrusNajmabadi So I think it is good to adding allowing this in this proposal, because when I can using aliases for anything, so I can using aliases for another using aliases. And it is meaningless when |
@soroshsabz this feature is already complete. We are not intending on changing the design here. If you'd like a new set of features, please open a discussion on that topic. To be clear, this feature was simply about expanding the set of types allowed on the RHS of an alias to all types. That's it. :) Anything else is outside of the scope of it.
You def can. For example:
It's simply that within a particular set of usings, the aliases defined in that set are not available within that set itself. But that's no issue as you can just provide the exanded form. For example: using X = Whatever;
using Y = Whatever[]; You may have some duplication in the using-block, but you can still use those aliases interchangeably at the use sites. Given that you only need to declare once, but might reference any number of times, this is totally reasonable. |
@CyrusNajmabadi thanks for details response, the problem is, we cannot use |
Right. But that's fine. Just write:
|
@soroshsabz, I think you have a good point, but you should probably start a new discussion or new issue about it, since this issue is feature-frozen 🙂 |
@Eli-Black-Work I add new discussion in #7253 thanks |
using alias = any_type
. roslyn#50167Summary
Simply put,
is legal. While
is not. I suspect this was simply an oversight, but either way there seems no good reason that it shouldn't work (and it parses terribly and does not produce a good error message either).
Motivation
Language consistency, completing things we started. Without this syntax you cannot give tuple names either, which is a big downside.
Language Change
The grammar will be changed like so:
Concluded Questions
Similarly, what are the semantics of a pointer type in an alias. e.g.
using XPtr = X*;
. Using aliases are in a location that generally cannot be markedunsafe
. So we'd likely want this to be legal, with an error if you then use XPtr in an safe context.Answer:
Aliases to pointers are allowed. However, they must be written in the form
using unsafe X = T*;
unsafe
will be an error.using unsafe X = T*;
is written andT
is not a type that is valid to take a pointer to. Regardless if 'X' is not referenced anywhere in the code.using unsafe X = T*;
would be ok, but the user has not passed the/unsafe
flag to the compiler.What are the semantics of a NRT nullable type with an alias. e.g.
using X = string?;
. Specifically, does the alias have to be an a#nullable enable
region? Or can it be located anywhere. Then, what does it mean if that alias is used in a context that doesn't allow nullable? Is that an error, a warning, or is the nullable annotation silently ignored?Answer:
using X = string?;
is not legal.using X = List<string?>;
remains legal as there is no top-level NRT, just an interior NRT.using X = int?;
remains legal as this is a top-level value type, not a reference type.For unsafe code, should the syntax be
using unsafe X = int*;
orunsafe using X = int*;
. i.e. does theunsafe
modifier come before or after theusing
keyword.Answer (LDM meeting 2/1/23):
using unsafe ...
. This keepsusing
blocks consistent withusing
always being the first token.LDM Discussions
https://github.com/dotnet/csharplang/blob/main/meetings/2021/LDM-2021-09-20.md#type-alias-improvements
https://github.com/dotnet/csharplang/blob/main/meetings/2022/LDM-2022-08-31.md#using-aliases-for-any-type
https://github.com/dotnet/csharplang/blob/main/meetings/2022/LDM-2022-09-28.md#ungrouped
https://github.com/dotnet/csharplang/blob/main/meetings/2023/LDM-2023-01-11.md#using-aliases-for-any-types
The text was updated successfully, but these errors were encountered: