{"action":"created","issue":{"url":"https://api.github.com/repos/JuliaLang/julia/issues/16422","repository_url":"https://api.github.com/repos/JuliaLang/julia","labels_url":"https://api.github.com/repos/JuliaLang/julia/issues/16422/labels{/name}","comments_url":"https://api.github.com/repos/JuliaLang/julia/issues/16422/comments","events_url":"https://api.github.com/repos/JuliaLang/julia/issues/16422/events","html_url":"https://github.com/JuliaLang/julia/pull/16422","id":155478930,"number":16422,"title":"WIP: static evaluation of method_exists (when true)","user":{"login":"timholy","id":1525481,"avatar_url":"https://avatars.githubusercontent.com/u/1525481?v=3","gravatar_id":"","url":"https://api.github.com/users/timholy","html_url":"https://github.com/timholy","followers_url":"https://api.github.com/users/timholy/followers","following_url":"https://api.github.com/users/timholy/following{/other_user}","gists_url":"https://api.github.com/users/timholy/gists{/gist_id}","starred_url":"https://api.github.com/users/timholy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/timholy/subscriptions","organizations_url":"https://api.github.com/users/timholy/orgs","repos_url":"https://api.github.com/users/timholy/repos","events_url":"https://api.github.com/users/timholy/events{/privacy}","received_events_url":"https://api.github.com/users/timholy/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2016-05-18T11:32:28Z","updated_at":"2016-05-18T11:49:43Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/JuliaLang/julia/pulls/16422","html_url":"https://github.com/JuliaLang/julia/pull/16422","diff_url":"https://github.com/JuliaLang/julia/pull/16422.diff","patch_url":"https://github.com/JuliaLang/julia/pull/16422.patch"},"body":"Given that I encouraged @nalimilan to shoot for #16401, I felt I should chip in by trying to figure out how to evaluate `method_exists` at compile time. Also of apparent interest to @stevengj (#7088).\r\n\r\nI tried to simply follow #7088, but there are clearly some differences. Here's my test script:\r\n```jl\r\nfunction foo(a, b)\r\n    if ccall(:jl_method_exists, Cint, (Any, Any), typeof(-).name.mt, Tuple{typeof(-), typeof(a), typeof(b)}) != 0\r\n        return a - b\r\n    end\r\n    b\r\nend\r\n\r\nfunction foo2(a, b)\r\n    if my_method_exists(-, (typeof(a), typeof(b)))\r\n        return a - b\r\n    end\r\n    b\r\nend\r\n\r\n@inline function my_method_exists(f, t)\r\n    return ccall(:jl_method_exists, Cint, (Any, Any), typeof(f).name.mt, makett(f, t)) != 0\r\nend\r\n\r\nusing Base.@pure\r\n\r\n@pure function makett(f, t)\r\n    t1 = Base.to_tuple_type(t)\r\n    Tuple{isa(f,Type) ? Type{f} : typeof(f), t1.parameters...}\r\nend\r\n```\r\nFollowing with gdb, it's clear that `foo` works (confirmed by `@code_native`, though in contrast with `isleaftype` this happy outcome is not revealed with `@code_warntype`). However, with `foo2` it doesn't. It also doesn't work if I evaluate `makett` and store it in a variable before issuing the `ccall`. Do I have to statically evaluate the call to `makett`, or am I barking up the wrong tree here?\r\n"},"comment":{"url":"https://api.github.com/repos/JuliaLang/julia/issues/comments/220003020","html_url":"https://github.com/JuliaLang/julia/pull/16422#issuecomment-220003020","issue_url":"https://api.github.com/repos/JuliaLang/julia/issues/16422","id":220003020,"user":{"login":"yuyichao","id":712232,"avatar_url":"https://avatars.githubusercontent.com/u/712232?v=3","gravatar_id":"","url":"https://api.github.com/users/yuyichao","html_url":"https://github.com/yuyichao","followers_url":"https://api.github.com/users/yuyichao/followers","following_url":"https://api.github.com/users/yuyichao/following{/other_user}","gists_url":"https://api.github.com/users/yuyichao/gists{/gist_id}","starred_url":"https://api.github.com/users/yuyichao/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/yuyichao/subscriptions","organizations_url":"https://api.github.com/users/yuyichao/orgs","repos_url":"https://api.github.com/users/yuyichao/repos","events_url":"https://api.github.com/users/yuyichao/events{/privacy}","received_events_url":"https://api.github.com/users/yuyichao/received_events","type":"User","site_admin":false},"created_at":"2016-05-18T11:49:43Z","updated_at":"2016-05-18T11:49:43Z","body":"Can you just make `method_exists` `@pure`? It should be apart from #265 and that's effectively what you are assuming here anyway?\r\n"}}