Preview Feedback: C# 12 Alias any type #7110
Replies: 20 comments 72 replies
-
Brainstorming here, I didn't have time to try but looking for feedback. We have an issue where our applications is case insensitive, meaning when we compare names, we cannot use the == operator. We have done a roslyn analyzer rule which check for the Name property of a specific interface. Any other property is not checked. |
Beta Was this translation helpful? Give feedback.
-
Porposal suggests the change:
which highly limits the use case for such an aliasing system to the readability within one compilation unit. However, if we were to take two separate compilation units, it seems like it would make it harder to understand. Force developers to read using directives to understand why we can put ("m", 100) in place of Measurement (as in example Are there any plans to further extend the types system? It would be reasonable to allow such aliasing that would allow type naming to propagate outside the compilation unit. It would allow users to define such things as already for example |
Beta Was this translation helpful? Give feedback.
-
What!? 😮 |
Beta Was this translation helpful? Give feedback.
-
Problem with an alias is it isn't a strong type, so from https://devblogs.microsoft.com/dotnet/check-out-csharp-12-preview/?WT.mc_id=DOP-MVP-5001655 where they show ... using DatabaseInt = int?; DatabaseInt isn't protected such that you couldn't assign it QueueInt. |
Beta Was this translation helpful? Give feedback.
-
I'm also seeing, that this feature will be highly misused or misinterpret by most casual programmers. Instead of a simple alias that could lead to misinterpretation, I would favor real derived types (maybe with constraints) like in F#. By that way we could create types like By creating the alias type feature people are going to create exactly this kind of code and feeling safe, but it will be only an alias and still allows to call |
Beta Was this translation helpful? Give feedback.
-
I only sorry that the keyword is not "type" like in majority of languages. |
Beta Was this translation helpful? Give feedback.
-
Instead of just "alias" I'd like to see type checking. |
Beta Was this translation helpful? Give feedback.
-
Please note that this is NOT a brand new language feature. It just fills the gap for the already existing feature since C# 2. |
Beta Was this translation helpful? Give feedback.
-
From this distance, I can't see myself using the public readonly record struct AuthToken(String Token); That's something that I've wanted in C# for a long time, but I had thus far overlooked this feature of C# 10. |
Beta Was this translation helpful? Give feedback.
-
Wouldn't a private struct record be the same as an alias? I don't get it, C# Language features were about having to write less, now it is about writing things differently. I'm still waiting to for actually useful semi-auto properties that have been promised since C# 10 (?) How about you stop botching easy to implement bloatware on top and do the work that is actually useful... You were a bit slow before C# 8 when it comes to implementing features, now you can't differ quantity from quality.... |
Beta Was this translation helpful? Give feedback.
-
I'm looking for the feature that would help addressing the Primitive Obsession problem. There are a lot of expositions of this problem in different areas. For instance in terms of identity of the objects we used to identify them via Guid or integral type. The method that accepts identities of multiple different entities opens the possibility for the errors where entities are mixed, for instance:
Or units of measurement suffer with the same problem:
Unfortunately aliases do not help with primitive obsession. Shall I create another proposal? |
Beta Was this translation helpful? Give feedback.
-
I have one request I'd like to throw in related to using aliases for any type. Maybe it's a separate feature, I'm not sure. I haven't and won't have time in the immediate future to put together a super tight spec, so forgive me if I have overlooked something. Request: Reasons:
Example: So this:
Or this:
Would become this:
Or even better, this:
Hope this all makes sense. Thx for reading. |
Beta Was this translation helpful? Give feedback.
-
FWIW, I like this proposal. I am constantly waging a war to reduce visual clutter and eliminate complex expressions and overly long lines. People talk about being explicit to increase clarity, but the positives quickly turn into negatives when you start combining tuples, generic types and fully qualified names. Visually parsing the super explicit thing you are looking at becomes hard and sometimes even requires temporary on-the-spot editing to break statements apart to simply read it all, let alone understand what is happening. I love tuples but don't use them as much as I'd hoped in some cases b/c of this issue. Hopefully this proposal nails that. I do think a type system would be good too. C# pretty much systematically improves and simplifies nearly everything in C++ and it is almost comical how much easier the language is to use and how much more empowering it is; I probably write C# 2x-5x faster than I write C++ code, with far fewer bugs. The one area where this hasn't been true for me so far is usings vs typedefs. (Though usings are obviously way better than includes.) I have found myself trying all sorts of things to work around some of the issues here, and ftm have settled on writing generic classes to have little subclass wrappers for different type combinations to give them simpler names. It sounds like you guys are working on something long term there, which would be awesome. Personally, I love that the C# team continually makes improvements to the language that make me more productive and reduce what I have to write. Complexity is a tricky thing; but I'd rather have the complexity be in learning lots of simple features than entangled in a large codebase that would be simplified by those new features. Less is more for MY code, not the language feature set (within reason, ofc). So keep it up. It is absolutely understandable that some of the features the community wants are complicated and have to be thought through and implemented carefully, perhaps over multiple versions. And in the mean time, it is still good to deliver value via easier, simpler features. Cheers. |
Beta Was this translation helpful? Give feedback.
-
Can we get a way to alias types inside namespaces? I would like to wrap up some types for my game engine instead of having to reference the library or write my own |
Beta Was this translation helpful? Give feedback.
-
Hello, can we make alias for static methods? So we can add global using for Console methods for example: Console.WriteLine method (global using WriteLine = System.Console.WriteLine;) and after this we can use just WriteLine everywhere insted of Console.WriteLine. |
Beta Was this translation helpful? Give feedback.
-
I know the C# 12 ship has sailed but I was sad to see that this alias any type feature doesn't support generic types. For instance, where I work we have a different notion of what a |
Beta Was this translation helpful? Give feedback.
-
Has there been discussion on using static string;
var combined = Join(' ', 1, 2, 3); Today we have to do This would be well in line with aspects of C# 12's |
Beta Was this translation helpful? Give feedback.
-
Note: this was not done because the cases where static members arise are basically identical to the cases where named type can be used. So there was no 'gap' per se. saying, for example, |
Beta Was this translation helpful? Give feedback.
-
FYI, I wanted to alias the following type:
but it doesn't accept
but for my scenario I also tried
Maybe in the future it could be improved to accept |
Beta Was this translation helpful? Give feedback.
-
So, is it "alias any type" feature, or "alias any type, but"? |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Aliases for any type (except nullable reference types) are included starting in C# 12 preview 3.
You can use this discussion for feedback as you use the feature.
You can learn more about aliases for any type in the article What's new in C# 12 article.
Beta Was this translation helpful? Give feedback.
All reactions