{"action":"created","issue":{"url":"https://api.github.com/repos/dotnet/roslyn/issues/14731","repository_url":"https://api.github.com/repos/dotnet/roslyn","labels_url":"https://api.github.com/repos/dotnet/roslyn/issues/14731/labels{/name}","comments_url":"https://api.github.com/repos/dotnet/roslyn/issues/14731/comments","events_url":"https://api.github.com/repos/dotnet/roslyn/issues/14731/events","html_url":"https://github.com/dotnet/roslyn/issues/14731","id":185210335,"number":14731,"title":"Proposal: Functors","user":{"login":"eyalsk","id":3756641,"avatar_url":"https://avatars.githubusercontent.com/u/3756641?v=3","gravatar_id":"","url":"https://api.github.com/users/eyalsk","html_url":"https://github.com/eyalsk","followers_url":"https://api.github.com/users/eyalsk/followers","following_url":"https://api.github.com/users/eyalsk/following{/other_user}","gists_url":"https://api.github.com/users/eyalsk/gists{/gist_id}","starred_url":"https://api.github.com/users/eyalsk/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/eyalsk/subscriptions","organizations_url":"https://api.github.com/users/eyalsk/orgs","repos_url":"https://api.github.com/users/eyalsk/repos","events_url":"https://api.github.com/users/eyalsk/events{/privacy}","received_events_url":"https://api.github.com/users/eyalsk/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/dotnet/roslyn/labels/0%20-%20Backlog","name":"0 - Backlog","color":"CCCCCC"},{"url":"https://api.github.com/repos/dotnet/roslyn/labels/Area-Language%20Design","name":"Area-Language Design","color":"5319e7"}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":12,"created_at":"2016-10-25T19:24:25Z","updated_at":"2016-10-26T03:16:48Z","closed_at":null,"body":"## What is a functor?\r\n\r\nA functor or a function object in C++ is simply put an object that is treated like a method and is executed similarly to a method.\r\n\r\nThe benefit of functors over methods is that you can do some processing _and_ store the state as part of the object.\r\n\r\n### The problem\r\n\r\nThere are various patterns where by design the implementation of some objects in the system have a single public method.\r\n\r\nJust as an example, in implementations of the Command pattern, objects may have a similar method to `Execute` that basically does some processing.\r\n\r\nHere is a simplistic and dummy implementation that demonstrates how the Command pattern _might_ be implemented.\r\n```\r\nclass Add\r\n{\r\n\tpublic int Calculate(int a, int b)\r\n\t{\r\n\t\treturn a + b;\r\n\t}\r\n}\r\n\r\nAdd add = new Add();\t\r\nint sum = add.Calculate(2, 2);\r\n```\r\nNow, this works perfectly fine the only downside to this approach is the extra call to the method `Calculate` and it doesn't matter what its name is or how short it is, it will never make sense because the class represents an operation and it should be used and expressed as such.\r\n\r\nAlternatively, we can rewrite this example as follow:\r\n```\r\nFunc<int, int, int> add = (int a, int b) => a + b;\r\n\t\r\nint sum = add(2, 2);\r\n```\r\nHowever, The class `Add` may or may not have more methods, it may or may not have state so a delegate might not always be feasible and delegates comes with their own pros and cons.\r\n\r\n### The solution\r\n\r\nSo how do I propose to solve it? well, in C++ I'd use a functor to solve it and I see no reason why C# cannot benefit from it too.\r\n\r\nAllow us to overload the function operator.\r\n\r\n```\r\nclass Add\r\n{\r\n\tpublic static int operator ()(int a, int b)\r\n\t{\r\n\t\treturn a + b;\r\n\t}\r\n}\r\n\r\nAdd add = new Add();\r\nint sum = add(2, 2);\r\n```\r\nToday we have indexers in the language and we can treat objects as if they were a collection of some sort why not functions? :)\r\n\r\n**Edit:**\r\n\r\n1. Changed `public static Add operator ()(int a, int b)` to `public static int operator ()(int a, int b)`\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/dotnet/roslyn/issues/comments/256239227","html_url":"https://github.com/dotnet/roslyn/issues/14731#issuecomment-256239227","issue_url":"https://api.github.com/repos/dotnet/roslyn/issues/14731","id":256239227,"user":{"login":"eyalsk","id":3756641,"avatar_url":"https://avatars.githubusercontent.com/u/3756641?v=3","gravatar_id":"","url":"https://api.github.com/users/eyalsk","html_url":"https://github.com/eyalsk","followers_url":"https://api.github.com/users/eyalsk/followers","following_url":"https://api.github.com/users/eyalsk/following{/other_user}","gists_url":"https://api.github.com/users/eyalsk/gists{/gist_id}","starred_url":"https://api.github.com/users/eyalsk/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/eyalsk/subscriptions","organizations_url":"https://api.github.com/users/eyalsk/orgs","repos_url":"https://api.github.com/users/eyalsk/repos","events_url":"https://api.github.com/users/eyalsk/events{/privacy}","received_events_url":"https://api.github.com/users/eyalsk/received_events","type":"User","site_admin":false},"created_at":"2016-10-26T03:16:48Z","updated_at":"2016-10-26T03:16:48Z","body":"@HaloFour \r\n\r\n> My world is CQRS and I've found no issues with implementing the command pattern on top of delegates. \r\n\r\nI never said that there's an issue with the current approach or that it's bad or not applicable without my proposal.\r\n\r\n> The complaint you make about tracking state is moot, you can take a delegate to an instance method of some service class.\r\n\r\nI didn't really make a _complaint_, I wrote a proposal and if you dislike it or any of the arguments I made then all I can say is I respect your opinion but it isn't mine.\r\n\r\n> That's a very non-compelling argument. I don't see why effort should be expended just to allow the source to look slightly different without actual improvements regarding verbosity or composition.\r\n\r\nIn your opinion it's very non-compelling but luckily I have my own opinion and I see things through a different kind of lens.\r\n\r\nI'm not trying to convince you here but making the source look slightly different and a lot closer to the programmer intents is in my opinion worth it.\r\n\r\nI'm not part of the design team, you aren't one of them either, we're here to write/express our own ideas/thoughts/opinions and whether efforts should be put into this feature it's for the design team to decide."}}
{"actor":{"display_login":"eyalsk"}}