{"action":"created","issue":{"url":"https://api.github.com/repos/pfultz2/Fit/issues/162","repository_url":"https://api.github.com/repos/pfultz2/Fit","labels_url":"https://api.github.com/repos/pfultz2/Fit/issues/162/labels{/name}","comments_url":"https://api.github.com/repos/pfultz2/Fit/issues/162/comments","events_url":"https://api.github.com/repos/pfultz2/Fit/issues/162/events","html_url":"https://github.com/pfultz2/Fit/issues/162","id":149595046,"number":162,"title":"[idea] adaptor for multiple virtual dispatch","user":{"login":"badair","id":2570307,"avatar_url":"https://avatars.githubusercontent.com/u/2570307?v=3","gravatar_id":"","url":"https://api.github.com/users/badair","html_url":"https://github.com/badair","followers_url":"https://api.github.com/users/badair/followers","following_url":"https://api.github.com/users/badair/following{/other_user}","gists_url":"https://api.github.com/users/badair/gists{/gist_id}","starred_url":"https://api.github.com/users/badair/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/badair/subscriptions","organizations_url":"https://api.github.com/users/badair/orgs","repos_url":"https://api.github.com/users/badair/repos","events_url":"https://api.github.com/users/badair/events{/privacy}","received_events_url":"https://api.github.com/users/badair/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":13,"created_at":"2016-04-19T21:54:07Z","updated_at":"2016-04-27T05:08:08Z","closed_at":null,"body":"I know you've got a lot on your plate right now, but I just wanted to run this idea by you. I think Fit is the perfect library candidate to offer a generic, maintainable solution to the multiple dispatch problem.\r\n\r\n[This article](http://eli.thegreenplace.net/2016/a-polyglots-guide-to-multiple-dispatch/) on the topic was recently posted on Hacker News, and is an excellent overview of the problem (I was previously unfamiliar).\r\n\r\nI haven't written any code yet, but I think it an adaptor template could be made.\r\n\r\nThe template would accept the following types:\r\n* `F` - a user-defined function object class with `operator()` overloads to handle dispatch cases\r\n* `B` - a base type pointer/reference\r\n* `D...` - a variadic parameter list of derived type candidates (pointers/references)\r\n\r\nThe adaptor constructor would accept an `F` object, and keep it around as a data member (or something logically equivalent to that - you might have a better way of doing this with your magical macros).\r\n\r\nThe adaptor would supply an `operator()` expecting a number of `B` arguments, which then would be `dynamic_cast`-ed to each type in the `D...` list. The `F` instance would be invoked once all arguments had been `dynamic_cast`-ed successfully, passing the successfully casted pointers as arguments.\r\n\r\nDoes that make sense?\r\n\r\nIf you think this is something that belongs in Fit, I could try to come up with an implementation at a later point in time."},"comment":{"url":"https://api.github.com/repos/pfultz2/Fit/issues/comments/214971129","html_url":"https://github.com/pfultz2/Fit/issues/162#issuecomment-214971129","issue_url":"https://api.github.com/repos/pfultz2/Fit/issues/162","id":214971129,"user":{"login":"pfultz2","id":1306044,"avatar_url":"https://avatars.githubusercontent.com/u/1306044?v=3","gravatar_id":"","url":"https://api.github.com/users/pfultz2","html_url":"https://github.com/pfultz2","followers_url":"https://api.github.com/users/pfultz2/followers","following_url":"https://api.github.com/users/pfultz2/following{/other_user}","gists_url":"https://api.github.com/users/pfultz2/gists{/gist_id}","starred_url":"https://api.github.com/users/pfultz2/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pfultz2/subscriptions","organizations_url":"https://api.github.com/users/pfultz2/orgs","repos_url":"https://api.github.com/users/pfultz2/repos","events_url":"https://api.github.com/users/pfultz2/events{/privacy}","received_events_url":"https://api.github.com/users/pfultz2/received_events","type":"User","site_admin":false},"created_at":"2016-04-27T05:08:08Z","updated_at":"2016-04-27T05:08:08Z","body":"Looking at this more, I don't think implementing visitors using a monad gains anything. Ultimately, its just function composition within composition(its like `.(.).` in haskell, I believe). Writing this in terms of the Fit library would look like this:\r\n\r\n```cpp\r\nFIT_STATIC_FUNCTION(binary_compose) = partial(limit_c<2>(compose));\r\nFIT_STATIC_FUNCTION(visit_compose) = by(binary_compose, capture(apply)(compose));\r\nFIT_STATIC_FUNCTION(zip_visit) = compose(capture(partial)(flow), visit_compose);\r\n```\r\n\r\nThen you would call this with your visit functions like this:\r\n\r\n```cpp\r\nzip_visit(visit(x), visit(y))([](auto x, auto y) { ... });\r\n```\r\n\r\nI haven't tested this yet, and I know it won't work in its current form, because the `binary_compose` function needs to handle the `result_type`."}}