{"action":"created","issue":{"url":"https://api.github.com/repos/Microsoft/visualfsharp/issues/1306","repository_url":"https://api.github.com/repos/Microsoft/visualfsharp","labels_url":"https://api.github.com/repos/Microsoft/visualfsharp/issues/1306/labels{/name}","comments_url":"https://api.github.com/repos/Microsoft/visualfsharp/issues/1306/comments","events_url":"https://api.github.com/repos/Microsoft/visualfsharp/issues/1306/events","html_url":"https://github.com/Microsoft/visualfsharp/issues/1306","id":163388856,"number":1306,"title":"Wrong SRTP constraints inferred","user":{"login":"gmpl","id":1261319,"avatar_url":"https://avatars.githubusercontent.com/u/1261319?v=3","gravatar_id":"","url":"https://api.github.com/users/gmpl","html_url":"https://github.com/gmpl","followers_url":"https://api.github.com/users/gmpl/followers","following_url":"https://api.github.com/users/gmpl/following{/other_user}","gists_url":"https://api.github.com/users/gmpl/gists{/gist_id}","starred_url":"https://api.github.com/users/gmpl/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gmpl/subscriptions","organizations_url":"https://api.github.com/users/gmpl/orgs","repos_url":"https://api.github.com/users/gmpl/repos","events_url":"https://api.github.com/users/gmpl/events{/privacy}","received_events_url":"https://api.github.com/users/gmpl/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/Microsoft/visualfsharp/labels/2%20-%20Ready","name":"2 - Ready","color":"CCCCCC"},{"url":"https://api.github.com/repos/Microsoft/visualfsharp/labels/Area-Compiler","name":"Area-Compiler","color":"5319e7"},{"url":"https://api.github.com/repos/Microsoft/visualfsharp/labels/bug","name":"bug","color":"fc2929"},{"url":"https://api.github.com/repos/Microsoft/visualfsharp/labels/Urgency-Soon","name":"Urgency-Soon","color":"eb6420"}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":4,"created_at":"2016-07-01T12:49:10Z","updated_at":"2016-07-07T19:42:03Z","closed_at":null,"body":"Long time ago I was trying to define generic constants, something like the existing GenericZero and GenericOne, but involving a method constraint of a typar and a specific type since we can't simulate static member outside the F# compiler.\r\nBut writing those signatures were not possible because of [this limitation](https://fslang.uservoice.com/forums/245727-f-language/suggestions/7887270-allow-all-inferrable-srtp-constraints-to-be-writte) the only solution I found was involving a phantom type:\r\n\r\n    type M() =\r\n        static member ($) (x:string, M) = \"\"\r\n        static member ($) (x:int   , M) = 0\r\n        static member ($) (x:float , M) = 0.0\r\n\r\n    let inline empty< ^R, ^M when (^R or ^M) : (static member ($) : ^R * M ->  ^R) and ^M :> M> =\r\n        let m = M()\r\n        ((^R or ^M) : (static member ($): ^R * M -> ^R ) (Unchecked.defaultof<'R>, m))\r\n\r\n    let a :int    = empty<  _ , M >\r\n    let b :string = empty<  _ , M >\r\n\r\nBut now that there is a [PR which solves that issue](https://github.com/Microsoft/visualfsharp/pull/1278) I tried again using that version and found that in fact that limitation was hiding a bug in the type inference.\r\n\r\n#### Repro steps\r\n\r\n. Download the [fixed F# compiler ](https://github.com/Microsoft/visualfsharp/pull/1278) \r\n. type build\r\n. open it with Visual Studio\r\n. Set FSIANYCPU as Start Project\r\n. Press F5\r\n. Paste this code:\r\n\r\n    type M() = class end\r\n\r\n    let inline empty< ^R when ( ^R or  M) : (static member ( $ ) :  ^R *  M ->  ^R)> =        \r\n        let m = M()\r\n        Unchecked.defaultof< ^R> $ m: ^R\r\n\r\n#### Expected behavior\r\n\r\n    val inline empty< ^R when ( ^R or  M) : (static member ( $ ) :  ^R * M ->  ^R) > :\r\n       ^R when ( ^R or  M) : (static member ( $ ) :  ^R * M ->  ^R)\r\n\r\n#### Actual behavior\r\n\r\nIt fails to compile with this error:\r\n\r\n          Unchecked.defaultof< ^R> $ (m:M) : ^R;;\r\n      ----^^^^^^^^^^^^^^^^^^^^^^^^\r\n    stdin(155,5): error FS0001: A type parameter is missing a constraint 'when ( ^R or  ^_arg2) : (static member ( $ ) :  ^R *  ^_arg2 ->  ^R)'\r\n\r\n\r\n#### Known workarounds\r\n\r\nNo workaround found apart from the phantom type solution above mentioned which is very ugly.\r\n\r\n#### Related information \r\n\r\nBranch: \r\n\r\n* Branch https://github.com/Microsoft/visualfsharp/pull/1278\r\n\r\nBTW: why in cases like this (even without the fix) the compiler complains about a missing constraint instead of inferring it for you as it does normally?"},"comment":{"url":"https://api.github.com/repos/Microsoft/visualfsharp/issues/comments/231185552","html_url":"https://github.com/Microsoft/visualfsharp/issues/1306#issuecomment-231185552","issue_url":"https://api.github.com/repos/Microsoft/visualfsharp/issues/1306","id":231185552,"user":{"login":"dsyme","id":7204669,"avatar_url":"https://avatars.githubusercontent.com/u/7204669?v=3","gravatar_id":"","url":"https://api.github.com/users/dsyme","html_url":"https://github.com/dsyme","followers_url":"https://api.github.com/users/dsyme/followers","following_url":"https://api.github.com/users/dsyme/following{/other_user}","gists_url":"https://api.github.com/users/dsyme/gists{/gist_id}","starred_url":"https://api.github.com/users/dsyme/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dsyme/subscriptions","organizations_url":"https://api.github.com/users/dsyme/orgs","repos_url":"https://api.github.com/users/dsyme/repos","events_url":"https://api.github.com/users/dsyme/events{/privacy}","received_events_url":"https://api.github.com/users/dsyme/received_events","type":"User","site_admin":false},"created_at":"2016-07-07T19:42:03Z","updated_at":"2016-07-07T19:42:03Z","body":"@gmpl Could you add a full repro for that?  In the original repro code above, ``empty`` takes two type parameters."}}
{"actor":{"display_login":"dsyme"}}