-
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
Proposal: Type Alias Declaration #7451
Comments
This behaviour could be added to the #3993, meaning that type aliases declared in wherever other than a compilation-unit be accessible through type-name wherever in the same assembly (via namespace, etc), as long as it wouldn't be a breaking change. |
I'm very happy to have found this proposal and would love to see it become a thing. Hurrey =) Edit: Would it be possible to provide this feature for interfaces, too? |
@mpeelen It should be. Also |
Small note here. The approach with compile-time erasure will block some interesting features such as f# unit types and/or sql domain types. If these are not planned in foreseeable future, all seems to be ok. Otherwise, at the end there will be two competing ways to declare the type alias. |
+1, love this one |
The is no concept of aliases in the CLR. So currently there is no way to expose them that way. Without CLR support, they will be solely a language feature and will be erased, just like F# type abbreviations.
You're right, this should be just an extension to the existing using-alias-directive. The problem is that with introducing new scoping for aliases, it might be a breaking change, so there should be some additional keyword to distinguish between the two. Clearly, |
agreed. However, c# already solved this with attribute annotations. Examples: c# dynamic, extension methods, tuples (#347), c#7 non-null reference types etc.
I'm not sure that reusing the Using directive is local, Wouldn't it be better to use distinguishable syntax for each of them? |
@ig-sinicyn I don't know if using attributes for this would be an adoptable option. "Wouldn't it be better to use distinguishable syntax for each of them?" then we're back to your point, "at the end there will be two competing ways to declare the type alias." |
Ok, let's summ it up:
|
@ig-sinicyn Just to mentioned, F# type abbreviations also use erasure. "Both are ugly" I don't know what do you mean by "ugly", it should be consistent, even if it's consistently "ugly", as you say. do you have any non-ugly syntax in mind? |
@ig-sinicyn Units of measure and type abbreviations are not the same thing. The proposed feature is similar to the latter rather than the former. |
Oops, got it. Thanks for the correction! |
👍 I am tired of copying things like |
I think I'd prefer it if the C# compiler borrowed project-wide aliases from VB.NET in the form of a command-line argument (which can be set through project properties):
That wouldn't handle any of your "scoped alias" concerns, but is it really that necessary to be able to define an alias only within a block of code smaller than a single file? Judging from the comments here and elsewhere that I've seen I'd say the most common request is project-wide (if not solution-wide). |
If I interpret @gafter 's comment correctly it seems to be in reference to the C# compiler having to read non-C# source files (such as JSON) in order to affect the compilation. I wouldn't suggest going that route but rather using command-line arguments to the compiler itself. The compiler already has command line arguments which affect how it treats source files, although perhaps none that affect name resolution like this would (aside To note the second proposed solution to #2044 is a |
@HaloFour I don't see how command-line args would solve this, because, then, the source must be compiled with a specific set of args for aliases, therefore, there will be a config-like file and we're back to gafter's comment. |
@alrz Which is the same situation for language version, references, resources, etc. I'm not a big fan of project-wide aliases in general. Makes me think of the |
@HaloFour I think the ability to name specific types for DDD scenarios or the use case @orthoxerox mentioned would be nice. Also with introducing named tuples it actually provides a lightweight syntax to name specific tuple types to be used across the project without declaring new ones (where it's not really necessary). Also, it's nothing like |
This is different, but I wish that I could create a type alias which would allow me to change a type name or namespace or even assembly, and still have |
This is quite like that, namespace Foo {
type Str = System.String;
} But without proper clr support to expose its metadata, it would represent the exact same type as |
Move this proposal to csharplang repo please. I want this. |
@gulshan Filed issue: dotnet/csharplang#259. |
In addition to an using-alias-directive which "introduces an identifier that serves as an alias for a namespace or type within the immediately enclosing compilation unit or namespace body", an alias-declaration can be represented as a type-declaration and be referenced by a type-name anywhere within the same assembly.
The text was updated successfully, but these errors were encountered: