Referencing static class with self

Topics: C# Language Design
Jan 6, 2015 at 12:43 PM
Edited Jan 6, 2015 at 12:49 PM
Hello I think it would be nice if you could reference static class with self inside the class like this:
static class VeryLongClassName
{
    public const int SomeConstant = 2;
    public static string StaticProperty { get { return "Hello"; } }

    public static void LogConstants()
    {
        Console.WriteLine(self.SomeConstant);
        Console.WriteLine(self.StaticProperty);
    }
}
Some classes can have very long names, so using "self" would be beneficial, because typing out "VeryLongClassName.SomeConstant" can be frustrating and using "self" would be very concise.
Jan 6, 2015 at 1:10 PM
But there's no reason to write the class name in there, you can simply write SomeConstant.
Jan 6, 2015 at 1:25 PM
Edited Jan 6, 2015 at 1:26 PM
Yes, of course but if I were to declare a variable inside that method with same name, its use would make it unambiguous. I think of the "self" as "this" equivalent for static classes, you don't have to use "this" in classes but of course you can, same as with "self". And of course there is the question that some people likes to be explicit and write "this" everywhere, it is nice because it can give you code completion in Visual Studio by typing "this." with dot as same as it could be with "self."
Jan 6, 2015 at 1:40 PM
Hmm, I suppose that a reasonable point of view even though I'm not convinced that such name conflicts happen often enough for this to be a problem.

Anyway, the main issue is with the choice of "keyword". self isn't a keyword and it's unlikely that you can make it a contextual keyword, someone may very well have a local variable named self and then there will be no way to distinguish between it and the keyword.

Maybe 'class' can work instead, it is already a keyword and currently you cannot define classes inside methods so there's no ambiguity if you write something like Console.WriteLine(class.SomeConstant);
Jan 6, 2015 at 1:49 PM
Yes, 'class' could work too, the main point was to reference static class, 'self' was just the first thing that popped up in my head because I saw it in other languages somewhere.
Jan 6, 2015 at 3:43 PM
Hi,

Why not do this then?
    using self = VeryLongClassName;
    static class VeryLongClassName
    {
        public const int SomeConstant = 2;
        public static string StaticProperty { get { return "Hello"; } }

        public static void LogConstants()
        {
            Console.WriteLine(self.SomeConstant);
            Console.WriteLine(self.StaticProperty);
        }
    }
Jan 6, 2015 at 5:12 PM
Edited Jan 6, 2015 at 5:19 PM
I suppose you could do that, but there are few problems:
  1. If someone would look at my code, not knowing my coding style then it could be confusing.
  2. 'Using alias directive' is namespace specific in given file, so you wouldn't be able to write multiple static classes using self in one file and one namespace. (Of course you could write twice the namespace in one file containing the class, but that's just ugly.).
    Example:
    namespace TestNamespace
    {
        using self = SomeClass;
    
        static class SomeClass
        {
            //self is referencing SomeClass
        }
    
        static class AnotherClass
        {
            //self is still referencing SomeClass instead of AnotherClass
        }
    }
    
Jan 6, 2015 at 11:39 PM
There's one good reason for asking for that.

I like to qualify every reference to instance members with this and static members with the type name. Non qualified entities are either local variables or function parameters.

That's why I asked for it when C# 2.0 was being discussed. But I never came up with a good name for it. I never thought about self, though.
Jan 12, 2015 at 4:50 PM
PauloMorgado wrote:
That's why I asked for it when C# 2.0 was being discussed. But I never came up with a good name for it. I never thought about self, though.
I don't think self really works, since it would be a legitimate (and in some contexts not unreasonable) identifier. I think allowing class.name as being synonymous with name but limited to members of the class in question should work without ambiguity, and without losing a syntax that might possibly be useful for some other syntax.
Jan 12, 2015 at 4:58 PM
supercat wrote:
PauloMorgado wrote:
That's why I asked for it when C# 2.0 was being discussed. But I never came up with a good name for it. I never thought about self, though.
I don't think self really works, since it would be a legitimate (and in some contexts not unreasonable) identifier. I think allowing class.name as being synonymous with name but limited to members of the class in question should work without ambiguity, and without losing a syntax that might possibly be useful for some other syntax.
How would that for structs? Another keyword?

I though of type. But it will fall in the same case.
Jan 12, 2015 at 5:10 PM
PauloMorgado wrote:
How would that for structs? Another keyword?
You use struct.name, of course.