{"action":"created","issue":{"url":"https://api.github.com/repos/dotnet/roslyn/issues/11516","repository_url":"https://api.github.com/repos/dotnet/roslyn","labels_url":"https://api.github.com/repos/dotnet/roslyn/issues/11516/labels{/name}","comments_url":"https://api.github.com/repos/dotnet/roslyn/issues/11516/comments","events_url":"https://api.github.com/repos/dotnet/roslyn/issues/11516/events","html_url":"https://github.com/dotnet/roslyn/issues/11516","id":156395271,"number":11516,"title":"C# Design Notes for May 10, 2016","user":{"login":"MadsTorgersen","id":6677272,"avatar_url":"https://avatars.githubusercontent.com/u/6677272?v=3","gravatar_id":"","url":"https://api.github.com/users/MadsTorgersen","html_url":"https://github.com/MadsTorgersen","followers_url":"https://api.github.com/users/MadsTorgersen/followers","following_url":"https://api.github.com/users/MadsTorgersen/following{/other_user}","gists_url":"https://api.github.com/users/MadsTorgersen/gists{/gist_id}","starred_url":"https://api.github.com/users/MadsTorgersen/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/MadsTorgersen/subscriptions","organizations_url":"https://api.github.com/users/MadsTorgersen/orgs","repos_url":"https://api.github.com/users/MadsTorgersen/repos","events_url":"https://api.github.com/users/MadsTorgersen/events{/privacy}","received_events_url":"https://api.github.com/users/MadsTorgersen/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/dotnet/roslyn/labels/Area-Language%20Design","name":"Area-Language Design","color":"5319e7"},{"url":"https://api.github.com/repos/dotnet/roslyn/labels/Design%20Notes","name":"Design Notes","color":"fc2929"},{"url":"https://api.github.com/repos/dotnet/roslyn/labels/Language-C%23","name":"Language-C#","color":"207de5"},{"url":"https://api.github.com/repos/dotnet/roslyn/labels/Language-VB","name":"Language-VB","color":"207de5"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":23,"created_at":"2016-05-24T00:27:31Z","updated_at":"2016-05-26T09:03:53Z","closed_at":null,"body":"C# Language Design Notes for May 10\r\n=============================\r\n\r\nIn this meeting we took a look at the possibility of adding new kinds of extension members, beyond extension methods.\r\n\r\nExtension members\r\n--------------------------\r\n\r\nEver since we added extension methods in C# 3.0, there's been a consistent ask to add a similar feature for other kinds of function members. While \"instance methods\" are clearly the most important member kind to \"extend with\" from the outside, it seems arbitrary and limiting not to have e.g. properties, constructors or static members.\r\n\r\nUnfortunately, the declaration syntax for extension methods is sort of a local optimum. The static method with the extra `this` parameter works great for methods: It is low cost, and it comes with an obvious disambiguation syntax. However, this approach doesn't carry over well to other kinds of members. A property with an extra `this` parameter? And how would extension static members attach to their type - cannot be through a `this` parameter representing an instance! As we were designing C# 4 we tried very hard to answer these questions and design an \"extension everything\" feature that was a syntactic extension (no pun intended) of extension methods. We failed, creating a succession of monsters until we gave up.\r\n\r\nThe other option is to take a step back, and do a different style of design. Most proposals nowadays do this, and the idea actually goes all the way back to debates we had when adding extension methods in the first place: instead of the extension *members* saying what they extend, maybe they should be grouped into \"extensions\"; class-like groups of extension methods that all extend the same type. With the enclosing \"extension class\" doing all the extension, the members can just be declared with their normal syntax:\r\n\r\n``` c#\r\nclass Person\r\n{\r\n    public string Name { get; }\r\n    public Person(string name) { Name = name; }\r\n}\r\n\r\nextension class Enrollee extends Person\r\n{\r\n    static Dictionary<Person, Professor> enrollees = new ();                                            // static field\r\n    public void Enroll(Professor supervisor) { enrollees[this] = supervisor; }                          // instance method\r\n    public Professor Supervisor => enrollees.TryGetValue(this, out var supervisor) ? supervisor : null; // instance property\r\n    public static ICollection<Person> Students => enrollees.Keys;                                       // static property\r\n    public Person(string name, Professor supervisor) : this(name) { this.Enroll(supervisor); }          // constructor\r\n}\r\n```\r\n\r\nIssue #11159 summarizes the recent proposals along these lines.\r\n\r\nThis syntactic approach has clear advantages - writing an extension member is as simple as writing a normal member. It does have some challenges, too. One is, what does it compile into? Another is, how do you disambiguate when several extension members are in scope? Those are trivially answered by the current extension methods, but need more elaborate answers here.\r\n\r\nLooking at our current BCL libraries, it is clear that there are many places where layering is poor. Essentially, there's a trade off between layering (separating dependencies) and discoverability (offering members *on* the important types). *If* you have a file system, you want to offer a constructor overload that takes a file name. Extension constructors (for instance) address this.\r\n\r\nAlso, extension members allow putting concrete functionality on interfaces, and on specific instances of generic types.\r\n\r\nWe think that the most useful and straightforward kinds of extension methods to add would be instance and static versions of methods, properties and indexers, as well as static fields. We don't think instance fields make sense - where would they live? The answer to that is at best complicated, and probably not efficient.\r\n\r\nExtension constructors would also be useful. There's a bit more of a design space here, depending on whether you want them to be just like instance constructors (which would require the specific extended type to already have a constructor to delegate to), or more like factories (which could be added to interfaces etc. and produce instances of more derived types).\r\n\r\nExtension operators are in a bit of a gray zone. Some of them, like arithmetic operators, probably make sense. Equality operators are hard, because everyone's already got one, so the \"built-in\" would always shadow. Implicit conversion operators are especially problematic, because their invocation doesn't have an associated syntax, so it's very subtle when they are applied, and there's no easy way to choose *not* to do it.\r\n\r\nImplementation-wise, the extension class would probably turn into a static class, and the extension members would probably be represented as static methods, with the most obvious mapping we can find. There's a case for implementing extension properties via \"indexed properties\" which are supported in IL (and VB), though maybe we'd prefer mapping to something that could be written directly in C#.\r\n\r\nThe name of the extension would be the name of the static class, and can also be used in disambiguation, for which we would need a syntax. Maybe existing syntax, such as `((Enrollee)person).Supervisor` or `(person as Enrollee).Supervisor` could be used when disambiguating instance members or operators, whereas static members would just be accessed directly off of the generated static class, as in `Enrollee.Students`.\r\n\r\nArguably, consumption is more important than production. In this case, that is an argument both for and against introducing a new syntactic approach to declaring extension members: On the one hand, it makes it less of a problem if we effectively deprecate the existing syntax, on the other it makes it less compelling to add syntax for it, instead of maybe some attribute-based approach, or other less syntax-heavy solutions.\r\n\r\nFor extension properties, people often point to `Enumerable.Count(IEnumerable src)` as an example of why they want them, but that's a terrible example, because it isn't efficient. Even so, there's probably a good need for these - the Roslyn code base for instance has many little extension methods on top of interfaces, that \"should\" have been properties. `Kind()` is a perfect example.\r\n\r\nConclusion\r\n---------------\r\n\r\nWe think this is worth pursuing further. We will come back to it in future design meetings to start ironing out the details.\r\n"},"comment":{"url":"https://api.github.com/repos/dotnet/roslyn/issues/comments/221816869","html_url":"https://github.com/dotnet/roslyn/issues/11516#issuecomment-221816869","issue_url":"https://api.github.com/repos/dotnet/roslyn/issues/11516","id":221816869,"user":{"login":"miloush","id":10546952,"avatar_url":"https://avatars.githubusercontent.com/u/10546952?v=3","gravatar_id":"","url":"https://api.github.com/users/miloush","html_url":"https://github.com/miloush","followers_url":"https://api.github.com/users/miloush/followers","following_url":"https://api.github.com/users/miloush/following{/other_user}","gists_url":"https://api.github.com/users/miloush/gists{/gist_id}","starred_url":"https://api.github.com/users/miloush/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/miloush/subscriptions","organizations_url":"https://api.github.com/users/miloush/orgs","repos_url":"https://api.github.com/users/miloush/repos","events_url":"https://api.github.com/users/miloush/events{/privacy}","received_events_url":"https://api.github.com/users/miloush/received_events","type":"User","site_admin":false},"created_at":"2016-05-26T09:03:53Z","updated_at":"2016-05-26T09:03:53Z","body":"@qrli The problem with the first two is that they create inheritance expectations on the extensions itself, like the ability to override another extension.\r\n\r\nNo but seriously, if state needs to be brought in, it just seems easy to lift the static requirement and require either constructor or explicit operator \r\n```C#```\r\n[Extension]\r\npublic class PersonExtensions\r\n{\r\n    public PersonExtensions(Person p) { ... }\r\n}\r\n```\r\nYou don't introduce any new syntax to the language, and you even continue supporting extending different types within one extending class. And you can instantiate/call it only when the extension method is used.\r\n\r\nBecause the only other option I see is to do it the dependency property way, i.e. big dictionary."}}
{"actor":{"display_login":"miloush"}}