{"action":"created","issue":{"url":"https://api.github.com/repos/nim-lang/Nim/issues/8306","repository_url":"https://api.github.com/repos/nim-lang/Nim","labels_url":"https://api.github.com/repos/nim-lang/Nim/issues/8306/labels{/name}","comments_url":"https://api.github.com/repos/nim-lang/Nim/issues/8306/comments","events_url":"https://api.github.com/repos/nim-lang/Nim/issues/8306/events","html_url":"https://github.com/nim-lang/Nim/issues/8306","id":340891464,"node_id":"MDU6SXNzdWUzNDA4OTE0NjQ=","number":8306,"title":"[RFC] Keyword-Only Arguments; leads to more readable code and fwd-compatible changes in API's","user":{"login":"timotheecour","id":2194784,"node_id":"MDQ6VXNlcjIxOTQ3ODQ=","avatar_url":"https://avatars2.githubusercontent.com/u/2194784?v=4","gravatar_id":"","url":"https://api.github.com/users/timotheecour","html_url":"https://github.com/timotheecour","followers_url":"https://api.github.com/users/timotheecour/followers","following_url":"https://api.github.com/users/timotheecour/following{/other_user}","gists_url":"https://api.github.com/users/timotheecour/gists{/gist_id}","starred_url":"https://api.github.com/users/timotheecour/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/timotheecour/subscriptions","organizations_url":"https://api.github.com/users/timotheecour/orgs","repos_url":"https://api.github.com/users/timotheecour/repos","events_url":"https://api.github.com/users/timotheecour/events{/privacy}","received_events_url":"https://api.github.com/users/timotheecour/received_events","type":"User","site_admin":false},"labels":[{"id":122058493,"node_id":"MDU6TGFiZWwxMjIwNTg0OTM=","url":"https://api.github.com/repos/nim-lang/Nim/labels/Language%20design","name":"Language design","color":"eb6420","default":false},{"id":32256242,"node_id":"MDU6TGFiZWwzMjI1NjI0Mg==","url":"https://api.github.com/repos/nim-lang/Nim/labels/RFC","name":"RFC","color":"1d1d1d","default":false}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":5,"created_at":"2018-07-13T05:59:41Z","updated_at":"2018-07-13T09:46:07Z","closed_at":null,"author_association":"CONTRIBUTOR","body":"let's start with a quizz: which one of these is easier to understand ? (taken from @citycide's excellent `glob` library)\r\n```nim\r\n# from https://citycide.github.io/glob/\r\nwalkGlobKinds(\"*.*\", true, true, false, false)\r\nwalkGlobKinds(\"*.*\", root = \"\", relative = true, expandDirs = true, includeHidden = false; includeDirs = false)\r\n```\r\n\r\nClearly, the 2nd one. The 1st one pretty much forces you to read the docs at  **every single** such function call even if you're familiar with that API.\r\n\r\nYet, the 2nd one is not enforced by the compiler and relying on convention sucks.\r\n\r\n* in python, they solved this problem by introducing keyword only arguments [PEP3102](https://www.python.org/dev/peps/pep-3102/) \r\n\r\nThe use case is to force callers to use keyword calling as opposed to positional calling for these arguments, with benefits:\r\n* can result in much more readable code, see above\r\n* gives API writer more freedom to evolve API (eg adding params in the middle or reshuffling), all without breaking any code\r\n* removal of params in will cause compilation error if caller calls these params as opposed to silently accept wrong code in some cases, see [1]\r\nNOTE: keyword-only arguments would make such API evolution and parameter deprecation much easier and less error prone (see https://github.com/citycide/glob/issues/11 [RFC] API simplification for example)\r\n\r\n## python's approach\r\nhere's python's syntax + example from https://stackoverflow.com/questions/2965271/forced-naming-of-parameters-in-python to illustrate:\r\n```python\r\n>>> def foo(pos, *, forcenamed):\r\n...   print(pos, forcenamed)\r\n... \r\n>>> foo(pos=10, forcenamed=20)\r\n10 20\r\n>>> foo(10, forcenamed=20)\r\n10 20\r\n>>> foo(10, 20)\r\nTraceback (most recent call last):\r\n  File \"<stdin>\", line 1, in <module>\r\nTypeError: foo() takes exactly 1 positional argument (2 given)\r\n```\r\n\r\nNOTE: relevant snippet from [PEP3102](https://www.python.org/dev/peps/pep-3102/ introduced keyword only arguments : \r\n> Keyword-only arguments are not required to have a default value. Since Python requires that all arguments be bound to a value, and since the only way to bind a value to a keyword-only argument is via keyword, such arguments are therefore 'required keyword' arguments. Such arguments must be supplied by the caller, and they must be supplied via keyword.\r\n\r\n## suggested syntax up for discussion: \r\nwe could use same syntax as python, nice and simple, but which particular syntax doesn't matter much\r\n\r\n\r\n[1]\r\n```nim\r\nproc foo(a=100, b=20)=discard\r\nfoo(3)\r\nfoo(a=3) # with keyword-only arg\r\n\r\n# later `a=100` is removed\r\nproc foo(b=20)=discard\r\nfoo(3) # silently accepted with wrong semantics\r\nfoo(a=3)  # with keyword-only arg: better: compilation error\r\n```\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/nim-lang/Nim/issues/comments/404783947","html_url":"https://github.com/nim-lang/Nim/issues/8306#issuecomment-404783947","issue_url":"https://api.github.com/repos/nim-lang/Nim/issues/8306","id":404783947,"node_id":"MDEyOklzc3VlQ29tbWVudDQwNDc4Mzk0Nw==","user":{"login":"andreaferretti","id":1962818,"node_id":"MDQ6VXNlcjE5NjI4MTg=","avatar_url":"https://avatars1.githubusercontent.com/u/1962818?v=4","gravatar_id":"","url":"https://api.github.com/users/andreaferretti","html_url":"https://github.com/andreaferretti","followers_url":"https://api.github.com/users/andreaferretti/followers","following_url":"https://api.github.com/users/andreaferretti/following{/other_user}","gists_url":"https://api.github.com/users/andreaferretti/gists{/gist_id}","starred_url":"https://api.github.com/users/andreaferretti/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/andreaferretti/subscriptions","organizations_url":"https://api.github.com/users/andreaferretti/orgs","repos_url":"https://api.github.com/users/andreaferretti/repos","events_url":"https://api.github.com/users/andreaferretti/events{/privacy}","received_events_url":"https://api.github.com/users/andreaferretti/received_events","type":"User","site_admin":false},"created_at":"2018-07-13T09:46:07Z","updated_at":"2018-07-13T09:46:07Z","author_association":"COLLABORATOR","body":"@timotheecour  a little more verbose, but one can do\r\n\r\n```nim\r\nvar opt = defaultOptions()\r\nopt.relative = true\r\n\r\nwalkGlobKinds(\"*.*\", root = \"\", opt)\r\n```"}}
{"actor":{"display_login":"andreaferretti"}}