{"action":"created","issue":{"url":"https://api.github.com/repos/dotnet/csharplang/issues/1474","repository_url":"https://api.github.com/repos/dotnet/csharplang","labels_url":"https://api.github.com/repos/dotnet/csharplang/issues/1474/labels{/name}","comments_url":"https://api.github.com/repos/dotnet/csharplang/issues/1474/comments","events_url":"https://api.github.com/repos/dotnet/csharplang/issues/1474/events","html_url":"https://github.com/dotnet/csharplang/issues/1474","id":316755816,"number":1474,"title":"Allow 'in' [readonly 'ref'] based unconstrained extension methods","user":{"login":"makeman-xyz","id":38653828,"avatar_url":"https://avatars3.githubusercontent.com/u/38653828?v=4","gravatar_id":"","url":"https://api.github.com/users/makeman-xyz","html_url":"https://github.com/makeman-xyz","followers_url":"https://api.github.com/users/makeman-xyz/followers","following_url":"https://api.github.com/users/makeman-xyz/following{/other_user}","gists_url":"https://api.github.com/users/makeman-xyz/gists{/gist_id}","starred_url":"https://api.github.com/users/makeman-xyz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/makeman-xyz/subscriptions","organizations_url":"https://api.github.com/users/makeman-xyz/orgs","repos_url":"https://api.github.com/users/makeman-xyz/repos","events_url":"https://api.github.com/users/makeman-xyz/events{/privacy}","received_events_url":"https://api.github.com/users/makeman-xyz/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":3,"created_at":"2018-04-23T10:39:34Z","updated_at":"2018-04-23T14:54:29Z","closed_at":"2018-04-23T14:16:29Z","author_association":"NONE","body":"Consider the code which has a purpose in creation of an extension method logically similar for both classes and structs but without copying of structs\r\n```c#\r\nusing System;\r\n\r\npublic static class C {\r\n    public static T To<T>(this ref T o) where T: struct => o;\r\n    public static T To<T>(this T o) where T: class => o;\r\n    \r\n    /* nice to have */\r\n    //public static T To<T>(this ref T o) => o;\r\n    /* or */\r\n    //public static T To<T>(this in T o) => o;\r\n    \r\n    public static void Main() {\r\n        int i = 0;\r\n        Console.WriteLine(To(ref i));\r\n        Console.WriteLine(To(new Object()));\r\n        \r\n        Console.WriteLine(i.To());\r\n        Console.WriteLine(new Object().To());\r\n    }\r\n}\r\n```\r\n[On the C# 7.2](https://sharplab.io/#v2:CYLg1APgAgDABFAjAbgLACgNQMwMQNgQCY4BhOAbwzhoVyUIBU5GB7AHkYD4AKAFwAWASwDOcAE4BTAGYs4rAJRwA7gMlSWIOCL7iArgGM+cALxd5adLTp4mLDt37CxzRSrUbGWqCTMXqtAE0APQAVHAAdkIGknB8rHACAIYAbrGhwUFwwcE4tnJsnLyCohIycm5+rJbWYfLicBlZOXkMBQ7FznBCERVKVTWBVrStBAgALHgAHDxKVMPWND3GQqZwMIOLeACcPGw8UrJCCgqbi0i7+xGSynAA8gBGAFaSRrMnZ9ZZ1hc8QgB0+w+2WCcHU4lYDVIAGUYIgiIgtIw1HADEkADbo7piJIAWweQgA5npWHoRN8Rohdtdbo8Xm8FIDWO9kCCweIIVDYfDESwUWjMdi4HiCcTSeSFnAAL4YKVAA==) there are compiler errors\r\n```c#\r\n        Console.WriteLine(i.To()); // error CS0121: The call is ambiguous\r\n        Console.WriteLine(new Object().To()); // error CS0121: The call is ambiguous\r\n```\r\n\r\n[On the C# 7.3](https://sharplab.io/#v2:EYLgtghgzgLgpgJwDQBMQGoA+ABADAAmwEYBuAWAChLsBmQogNkICZ8BhfAb0v18LuJMAKviEB7ADxCAfAAoYACwCWUfAjgAzUfjEBKfAHcFiOKJD5YCAK4BjGPgC80neQp9+9YaMkz5y1SJ6hsbqZiyOzmKu7jx8APQAVPgAdko2pjBi+AoQAG6mCXGxvHFxtJ7a4lJyiipqmtpBTi7F+Ik6CPiFraXlgpU+Nf74SsmN+s1Rra19jIQALPQAHLL63G7ufKP2So74uNGbvMQAnLLisupaSrq6h0en52KyyXAG+ADywABWcHart3um1a7keSgAdBdAW04vhEAgxJ02ABlXBEZhEcxCYz4GwQAA2+JGqggYGASgA5lYxFYoCC+I9Xu8vr9/rpIc9oaU4QgEUjUejMaIcXjCcT8KTyVSaXSNrwAL6UeVAA=) this code works great according \r\nhttps://github.com/dotnet/csharplang/blob/master/proposals/csharp-7.3/improved-overload-candidates.md\r\n\r\nIn the current case much more convenient to have a single extension method without constraints\r\n```c#\r\npublic static T To<T>(this ref T o) => o;\r\n/* or */\r\npublic static T To<T>(this in T o) => o;\r\n```\r\n\r\nAs I understand from https://github.com/dotnet/roslyn/issues/165 a restriction for `ref`-based extension methods has been added to avoiding of possible reassignment with reference types, for example like\r\n\r\n```c#\r\npublic static T To<T>(this ref T o) => o = default(T);\r\n```\r\nBut seems that `in` [read-only `ref`] keyword disallows reassignment and may be safely used for unconstrained extension methods.\r\n\r\nWhat do you think? Maybe do I misunderstood something and `in`-based extension methods disabled by some objective reasons?\r\n\r\n----\r\nPosted by @Makeman-from-Makeloft\r\nP.S. Guys, if you will continue useless banning policy I shall be forced to post information as @ghost"},"comment":{"url":"https://api.github.com/repos/dotnet/csharplang/issues/comments/383604458","html_url":"https://github.com/dotnet/csharplang/issues/1474#issuecomment-383604458","issue_url":"https://api.github.com/repos/dotnet/csharplang/issues/1474","id":383604458,"user":{"login":"Joe4evr","id":1058562,"avatar_url":"https://avatars3.githubusercontent.com/u/1058562?v=4","gravatar_id":"","url":"https://api.github.com/users/Joe4evr","html_url":"https://github.com/Joe4evr","followers_url":"https://api.github.com/users/Joe4evr/followers","following_url":"https://api.github.com/users/Joe4evr/following{/other_user}","gists_url":"https://api.github.com/users/Joe4evr/gists{/gist_id}","starred_url":"https://api.github.com/users/Joe4evr/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Joe4evr/subscriptions","organizations_url":"https://api.github.com/users/Joe4evr/orgs","repos_url":"https://api.github.com/users/Joe4evr/repos","events_url":"https://api.github.com/users/Joe4evr/events{/privacy}","received_events_url":"https://api.github.com/users/Joe4evr/received_events","type":"User","site_admin":false},"created_at":"2018-04-23T14:54:29Z","updated_at":"2018-04-23T14:54:29Z","author_association":"NONE","body":"```cs\r\npublic static void M<T>(in T x)\r\n    where T : struct, IDisposable\r\n{\r\n    // x is always copied because T could be non-readonly.\r\n    // This is why `in` extension methods are not allowed.\r\n    x.Dispose();\r\n}\r\n```\r\nThis is also why `struct` Disposables are a Bad Thing™."}}
{"actor":{"display_login":"Joe4evr"}}