global::using MyAlias = ....

Topics: C# Language Design
May 26, 2014 at 11:36 AM
At the moment, using aliases can only be used in the file they are defined. I would love to be able to do ...
global::using AppFunc = System.Func<IDictionary<string, object>, Task>;
... so I don't have do repeatedly define it.
May 26, 2014 at 11:46 AM
Edited May 26, 2014 at 11:49 AM
I'd hate just spam <aol>me too!</aol> here, but this is something I'd like very much! +1

(also, codeplex needs a voting system!)

In fact, perhaps a slightly different syntax, similar to extern alias, how about:
global alias AppFunc = System.Func<IDictionary<string, object>, Task>;
This would keep backwards compatibility (using directives), and make those aliases explicit.
May 26, 2014 at 12:17 PM
Yes, this syntax is better
May 26, 2014 at 12:17 PM
Here is the first thought off the top of my head: this highly effects readability of single code file IMO.
May 26, 2014 at 12:19 PM
sorry, I thought we were talking about namespace usings, too :) ignore the previous comment :)
May 26, 2014 at 2:10 PM
I think this syntax is better, maybe.
//alias in global
alias global.AppFunc1 = System.Func<int>;
//alias in System namespace
alias System.AppFunc2 = System.Func<IDictionary<string, object>, Task>;
This decrease conflicts, and you can ignore alias you don't need.
May 26, 2014 at 2:56 PM
I think this would be useful for namespace usings as well. I have some shared source code that needs to target WPF, Silverlight, WinRT, and Windows Forms. Many of the type names are the same (mostly across the first three) but in different namespaces, so all my files need to have something like this at the top:
using System;

#if WINRT
using Windows.UI;
#elif SILVERLIGHT || WPF
using System.Windows.Media;
#else
using System.Drawing;
#endif
with occasional aliases as well to resolve any differences in the names of types which are functionally equivalent for my purposes. It would be great to put this all in one file that defines some global usings for the project.
May 26, 2014 at 3:03 PM
Edited May 27, 2014 at 7:43 AM
So do you suggest to allow this to be declared in any file of the project and apply to all the files? In any scope? Would you allow duplicates?

If you want to use a file that takes advantages of elsewhere declared global alias in another project, how do you expect users to do it?

MasuqaT:
Would alias System.AppFunc2 = ... be equal to namespace System { alias AppFunc2 = ... } ?

Edit: Also note that you would need to support alias global::System.AppFunc2 so perhaps alias global::AppFunc1 would make more sense ... which in turn would mean alias AppFunc1 should work and you no longer have the notion of global alias...
May 26, 2014 at 3:18 PM
JanKucera wrote:
So do you suggest to allow this to be declared in any file of the project and apply to all the files? In any scope? Would you allow duplicates?
Yes, I would think so. I would think it would act similarly to if you did those things in a single file. If you have a duplicated global using, you would get a warning.
If you want to use a file that takes advantages of elsewhere declared global alias in another project, how do you expect users to do it?
I would think this would only work within the same project.
MasuqaT:
Would alias System.AppFunc2 = ... be equal to namespace System { alias AppFunc2 = ... } ?
I would assume that in code in the System namespace (or a child namespace), the second alias would be used. In all other namespaces or in the global namespace, the first alias would be used, similar to how it works when you do something like that in a single file. Although it is slightly different in that scenario since in something like this:
using X = System.Action;

namespace N
{
    using X = System.Func<int>;
}

namespace N
{
    class Y
    {
        private X x;
    }
}
The field is declared as an Action, not a Func<int>.
May 26, 2014 at 4:43 PM
hmemcpy wrote:
I'd hate just spam <aol>me too!</aol> here, but this is something I'd like very much! +1

(also, codeplex needs a voting system!)
IIRC, UserVoice is still where these suggestions are supposed to go and that does have a voting mechanism. For anything I've suggested here I'd first post to UserVoice (or find a similar posting, which there usually is) and then post that URL here.
May 27, 2014 at 10:12 AM
My experience of Microsoft & UserVoice is that it's where suggestions go to die.
May 27, 2014 at 11:57 AM
markrendle wrote:
My experience of Microsoft & UserVoice is that it's where suggestions go to die.
And most suggestions will, particularly if they don't have a lot of votes. But the C# Roslyn team did specifically mention that several of the new features planned for C# 6.0, such as string interpolation and null propagation, were taken from UserVoice based on the number of votes. I'd expect that features suggested on these forums would get less far than those posted to UserVoice.
May 27, 2014 at 1:46 PM
UserVoice was effectively abandoned for some time - suggestions and comments went unanswered for years. Besides, I spent all my votes years ago and I don't expect I'll ever see them back.
May 27, 2014 at 3:24 PM
damianh wrote:
UserVoice was effectively abandoned for some time - suggestions and comments went unanswered for years. Besides, I spent all my votes years ago and I don't expect I'll ever see them back.
Probably feels that way, yeah. I'm sure that these teams get considerably more feedback than they can reasonably manage and most of it probably isn't stuff that would be immediately rejected so it just languishes waiting on enough popularity to make it worthwhile implementing. I don't expect much more to come from these forums, except where feedback has been explicitly requested.

I'm curious as to how the Roslyn team approaches forks and pull requests. I can only assume that those requests are going to be held to absolutely insane standards and still likely get rejected if they don't fit the existing agenda and charter.