Replies: 11 comments
-
#90 is very useful. Also having alias defined in namespace and be usable from other namespaces is neat feature. In fact for generic intensive classes and structs this can be very helpful to keep things short and concise. Both #90 and this can help for scenarios where you have generics in generics and typing them is frustrating. It can be helpful for generic object composition scenarios. I have good example for that. Struct enumerators. Cool thing about struct enumerators is that they can build up together and form variety of struct enumerators in efficient way. For example ReverseEnumerator<RangeEnumerator<SelectEnumerator<TSource, TIn, TOut>, ...> Obviously i exegerated here but main point is Writing such thing is frustrating. Its a complex Declaration but alias can help with it. Maybe more advanced type inference can also help where you dont need to write generic arguments anymore. Then you probably wouldnt use alias anyway. You still probably use alias if it gives meaningful name in more specific context |
Beta Was this translation helpful? Give feedback.
-
The look up approach of below is what you mean by 1/3 slower? Is that due to reflection?
What exactly is the use case that would require some sort of language change. Could you elaborate. From what code you do show, I am not sure why your problem would not be solved by IoC, or some other pattern. |
Beta Was this translation helpful? Give feedback.
-
The only case I see for a global alias is structs that are generics. Not structs in general. Unless it is constrained, you will end up with typedef/define explosions that C++ suffers from where you have an absurd number of type definitions for strings.
|
Beta Was this translation helpful? Give feedback.
-
@dx-prog The code is slower because I am using a Interface or Abstract class instead of a normal class. In a Interface or Abstract class I'm calling a virtual method which makes my struct slower. Its, well, just a tiny bit slower (and only when I have many iterations). Nevertheless, I think a global alias (with restrictions of course) wouldn't be a bad feature. |
Beta Was this translation helpful? Give feedback.
-
@dx-prog i wouldnt be as condifent as you. The word "you will" isnt quite right. Its better to say "you probably will". No one can stop you doing terrible things. C++ did terrible thing wasnt because of alias feature. Im pretty sure there are other reasons. Certainly c# would not introduce cryptic and confusing aliases. Its only used by developers who want to reduce amount of typing. Yes. Having global aliases can open new path for doing terrible things. But this is true for every single feature of the language. Think about enormous usages of pocket knife. Plenty of good stuff you can do with it, meanwhile you can kill someone using same knife. So what you say? Knife is good or bad? :) |
Beta Was this translation helpful? Give feedback.
-
The CLR is designed to be cross-platform, which is why Another reason is that the usage of pointers in C# is much lower. |
Beta Was this translation helpful? Give feedback.
-
Copy paste of #259
|
Beta Was this translation helpful? Give feedback.
-
Hello!
I am working with classes and structs which requires more than one generic, butall types are dependent on the first generic type. Here is a example:
As already mentioned, the second generic type
T1
is logically dependent of the first generic typeT
. For example ifT
isint
the second typeT1
has to be its logical counterpart, lets name itIntFuncs
. So the only logical declaration of this struct would be:A possible solution would be something like this:
But due to the fact that I am using a interface instead of a class now, the code runs 1/3 slower then the first approach with two generics.
My proposal:
You can define a "global" alias for structs and classes, something like this:
This also has benefits if you're writing an library with internal generics but you want them to be easy accessed from outside. For classes this is already possible, if you simply inherit the generic class, but you have to write all your constructors again (even if youre just calling the base constructor):
So this could be a shortcut aswell:
Thats my proposal. In fact I would add some restrictions so that only internal structs / classes can have a public alias or something.
Yes I know you can create your own alias with
but this must be declared in every file youre using this struct / class. And as a library developer you can't provide simple access to your structs / classes with this approach. Thats why I think my proposal would be a cool feature in an upcoming C# version.
Thank you.
Beta Was this translation helpful? Give feedback.
All reactions