{"action":"created","issue":{"url":"https://api.github.com/repos/dotnet/csharplang/issues/2106","repository_url":"https://api.github.com/repos/dotnet/csharplang","labels_url":"https://api.github.com/repos/dotnet/csharplang/issues/2106/labels{/name}","comments_url":"https://api.github.com/repos/dotnet/csharplang/issues/2106/comments","events_url":"https://api.github.com/repos/dotnet/csharplang/issues/2106/events","html_url":"https://github.com/dotnet/csharplang/issues/2106","id":394439765,"node_id":"MDU6SXNzdWUzOTQ0Mzk3NjU=","number":2106,"title":"More overloadable operators","user":{"login":"CreatorVilius","id":31628630,"node_id":"MDQ6VXNlcjMxNjI4NjMw","avatar_url":"https://avatars0.githubusercontent.com/u/31628630?v=4","gravatar_id":"","url":"https://api.github.com/users/CreatorVilius","html_url":"https://github.com/CreatorVilius","followers_url":"https://api.github.com/users/CreatorVilius/followers","following_url":"https://api.github.com/users/CreatorVilius/following{/other_user}","gists_url":"https://api.github.com/users/CreatorVilius/gists{/gist_id}","starred_url":"https://api.github.com/users/CreatorVilius/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/CreatorVilius/subscriptions","organizations_url":"https://api.github.com/users/CreatorVilius/orgs","repos_url":"https://api.github.com/users/CreatorVilius/repos","events_url":"https://api.github.com/users/CreatorVilius/events{/privacy}","received_events_url":"https://api.github.com/users/CreatorVilius/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":2,"created_at":"2018-12-27T16:41:13Z","updated_at":"2018-12-28T14:20:31Z","closed_at":null,"author_association":"NONE","body":"- More overloadable operators\r\n    - `` ` `` - Grave accent\r\n        - Example: `` Any operator`(String str) {} ``\r\n        - Can be static\r\n        - ```cs\r\n          class A {\r\n              public int b = 0;\r\n              public int c = 0;\r\n              public A() {\r\n                  // ...\r\n              }\r\n              public int operator `(string d) {\r\n                  if(d == \"b\") {\r\n                      return this.b;\r\n                  } else if(d == \"c\") {\r\n                      return this.c;\r\n                  }\r\n              }\r\n          }\r\n          A myVar;\r\n          Console.WriteLine(myVar`b` + myVar`c`);\r\n          ```\r\n    - `@` - I know that there is @\"str\"\r\n        - Example: `Any operator@(Any any)`\r\n        - ```cs\r\n          class A {\r\n              public int[] b = {};\r\n              public A(int... c) {\r\n                  this.b = c;\r\n              }\r\n              public int operator @(int d) {\r\n                  int i = 0;\r\n                  int[] f = {};\r\n                  foreach(int e in this.b) {\r\n                      f[i] = this.b[i] + d;\r\n                      i++;\r\n                  }\r\n                  return f;\r\n              }\r\n          }\r\n          A someInt = new A(10, 5, 7);\r\n          someInt @ 2; // 12, 7, 9\r\n          ```\r\n    - `**`\r\n        - Same as overloading operator `*`, but can have 0-1 arguments\r\n    - `#`\r\n        - Same as `@` example.\r\n\r\nWhat do you think? Am I missing operator? Or you think something should be changed?"},"comment":{"url":"https://api.github.com/repos/dotnet/csharplang/issues/comments/450366290","html_url":"https://github.com/dotnet/csharplang/issues/2106#issuecomment-450366290","issue_url":"https://api.github.com/repos/dotnet/csharplang/issues/2106","id":450366290,"node_id":"MDEyOklzc3VlQ29tbWVudDQ1MDM2NjI5MA==","user":{"login":"willard720","id":10124911,"node_id":"MDQ6VXNlcjEwMTI0OTEx","avatar_url":"https://avatars3.githubusercontent.com/u/10124911?v=4","gravatar_id":"","url":"https://api.github.com/users/willard720","html_url":"https://github.com/willard720","followers_url":"https://api.github.com/users/willard720/followers","following_url":"https://api.github.com/users/willard720/following{/other_user}","gists_url":"https://api.github.com/users/willard720/gists{/gist_id}","starred_url":"https://api.github.com/users/willard720/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/willard720/subscriptions","organizations_url":"https://api.github.com/users/willard720/orgs","repos_url":"https://api.github.com/users/willard720/repos","events_url":"https://api.github.com/users/willard720/events{/privacy}","received_events_url":"https://api.github.com/users/willard720/received_events","type":"User","site_admin":false},"created_at":"2018-12-28T14:20:31Z","updated_at":"2018-12-28T14:20:31Z","author_association":"NONE","body":"This is bad because I have no idea what any of your operators do. Can you imagine how unpredicatable the language would become if this code:\r\n\r\n```csharp\r\npublic void DoFoo()\r\n{\r\n    var foo = new Foo();\r\n    Console.WriteLine(foo.GetValue());\r\n\r\n    var bar = foo.GetValue();\r\n    bar.DoBar();\r\n   \r\n    var result = Foo.GetNumber() + Bar.GetNumber();\r\n}\r\n```\r\n\r\nTurned into this:\r\n\r\n```csharp\r\npublic void DoFoo()\r\n{\r\n    var foo = new Foo();\r\n    Console.WriteLine(`foo);\r\n\r\n    var bar = `foo;\r\n    @bar;\r\n   \r\n    var result = ~Foo # Bar ~;\r\n}\r\n```\r\n\r\nHow are we supposed to know what's going on here?\r\n\r\nAlso, `@` is already in use to name a variable a preexisting keyword. You can make a field called `@this` and in the metadata it's name would be `this`. Why would you want to overload that?\r\n\r\nWhen we overload, it's to add or expand the base functionality, such as this:\r\n\r\n```csharp\r\npublic static Vector2 operator+ (Vector2 a, Vector2 b) => new Vector2(a.X + b.X, a.Y + b.Y);\r\n\r\n...\r\n\r\nvar a = new Vector2(1, 2);\r\nvar b = new Vector2(2, 1);\r\nvar c = a + b;    // This == (3, 3);\r\n```\r\n\r\nIt's predictable and we know what's going to happen. Having `foo@` is completely unreadable and inferior to `foo.DoSomething();` in every way besides time it takes to type (which isn't that important)."}}
{"actor":{"display_login":"willard720"}}