-
Notifications
You must be signed in to change notification settings - Fork 4.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
New type aliases for long established types #12874
Comments
It would (also) be nice to be able to define assembly/namespace wide custom aliases #7451 (currently mislabeled as Discussion). |
Also related to #11993. |
While the idea has merit, there are some non-obvious fishhooks to consider. For example, aliasing |
@theunrepentantgeek how about this..,
Perhaps this is becoming a little too convoluted. |
The thing that will probably kill this suggestion is that it would break existing code. This code is valid under C#6 and earlier:
But, type aliases aren't permitted as variable names, so this code is not permitted under C#6 and earlier:
If date became a valid type alias, then the first example will suddenly fail to compile. |
Having given some more thought to this... One of the key difficulties I see students face when learning object oriented programming is being able to differentiate between the language itself, in terms of it's reserved words (keywords), semantics & syntax, and the framework itself; at a granular level, the type system, or the objects that the language is working with. Most, if-not-all C syntax/style languages seem to do this. You know what...lets expand on that - even some mid-level developers don't fundamentally know the difference! With regards to the aforementioned suggestion, rather than adding new type aliases to the language, it would actually be more appropriate to allow type aliases to be completely configurable throughout the language, source code, and development experience. Imagine C# without the following keywords: byte sbyte short ushort int uint long ulong float double decimal bool string char object null Yes, you saw that correctly, null is in there too! Now the question is, how would you write an application without these keywords? One (partial) solution would be to use the fully qualified type... Instead of this...
You could do this...
But, what happens when you need null? - since null is no longer a keyword, you can't do this...
However if I remember correctly, null actually refers to Therefore in this scenario, we as developers would need
Since we no longer have any type aliases to work with, C# would require a way for these to be configurable within the scope of the application. Alternatively the runtime could automatically do this under the hood for backwards compatibility. By default, the application would have a configuration file, which I have named "TypeDefs.cs"
These definitions become global, that is, used throughout the application and cannot be overwritten once defined. Subsequent attempts to override a type definition would result in a compiler error. Roslyn code analysis could have additional analysers to nag people who deviate from the standard type definitions. At the file level...
In summary, this provides the following improvements to the language:
Food for thought (syntax alternatives)
Food for thought (C#7 nullable reference types proposal) This feature might actually fit really well with the nullable reference type proposal. [(https://github.com//issues/5032)] Consider the following type definitions
Well, we've defined that
|
@series0ne
Type aliases already have a syntax with the Global type aliases have already been proposed and declined. I don't see how aliases improve the non-nullable references problem. TypeScript can be much looser-and-faster with type definitions (and survive a breaking syntax change), but in C# (and the CLR) union types don't exist, and as mentioned above |
@series0ne: Maybe you are confusing |
@JesperTreetop correct! @HaloFour having investigated this in ILSpy, I can see where I was wrong, it's not null I was thinking about, it's void In essence then, null is a reserved word with unary value (it can only ever be null) This would explain why this works...
And this doesn't
I understand your point about type aliases already having syntax with the using keyword...I guess this proposal would essentially allow two strains of aliasing...back to the drawing board. |
We're so fond of this feature request, that we've retroactively added support to previous versions of our compilers. You can even select your own spelling for these types! To do so, place the following near the top of your source file: using datetime = System.DateTime;
using intptr = System.IntPtr;
using guid = System.Guid; We don't think we can possibly improve on that. Just kidding. I agree with @theunrepentantgeek #12874 (comment) |
Ever since C# 1.0 there have been built in aliases for several of the CTS compatible types (...and some non CTS compatible types)
For example
System.Int32
as intSystem.Single
as floatSystem.String
as stringSuggestions 1
There is one known CTS compatible type that has no alias, which is
System.IntPtr
and I would like to know how feasible it would be to add an alias for this type to future versions of C#For example
System.IntPtr
as ptr or intptrSuggestion 2
There are several types that are regularly used in C#, but are not CTS compatible, however it might be nice to have aliases for them anyway
For example
System.DateTime
as date or datetimeSystem.Guid
as guidSystem.UIntPtr
as uptr or uintptrPlease discuss.
The text was updated successfully, but these errors were encountered: