{"action":"created","comment":{"url":"https://api.github.com/repos/Araq/Nim/pulls/comments/28730044","id":28730044,"diff_hunk":"@@ -0,0 +1,262 @@\n+#\n+#\n+#            Nim's Runtime Library\n+#        (c) Copyright 2015 Nim Contributors\n+#\n+#    See the file \"copying.txt\", included in this\n+#    distribution, for details about the copyright.\n+#\n+\n+## :Author: Oleh Prypin\n+##\n+## Abstract\n+## ========\n+##\n+## This module implements types which encapsulate an optional value.\n+##\n+## A value of type ``?T`` (``Maybe[T]``) either contains a value `x`\n+## (represented as ``just(x)``) or is empty (``nothing(T)``).\n+##\n+## This can be useful when you have a value that can be present or not.\n+## The absence of a value is often represented by ``nil``, but it is not always\n+## available, nor is it always a good solution.\n+##\n+##\n+## Tutorial\n+## ========\n+##\n+## Let's start with an example: a procedure that finds the index of a character\n+## in a string.\n+##\n+## .. code-block:: nim\n+##\n+##   import optionals\n+##\n+##   proc find(haystack: string, needle: char): ?int =\n+##     for i, c in haystack:\n+##       if c == needle:\n+##         return just i\n+##     return nothing(int)  # This line is actually optional,\n+##                          # because the default is empty\n+##\n+## The ``?`` operator (template) is a shortcut for ``Maybe[T]``.\n+##\n+## .. code-block:: nim\n+##\n+##   try:\n+##     assert(\"abc\".find('c')[] == 2)  # Immediately extract the value\n+##   except FieldError:  # If there is no value\n+##     assert false  # This will not be reached, because the value is present\n+##\n+## The ``[]`` operator demonstrated above returns the underlying value, or\n+## raises ``FieldError`` if there is no value. There is another option for\n+## obtaining the value: ``val``, but you must only use it when you are\n+## absolutely sure the value is present (e.g. after checking ``has``). If you do\n+## not care about the tiny overhead that ``[]`` causes, you should simply never\n+## use ``val``.\n+##\n+## How to deal with an absence of a value:\n+##\n+## .. code-block:: nim\n+##\n+##   let result = \"team\".find('i')\n+##\n+##   # Nothing was found, so the result is `nothing`.\n+##   assert(result == nothing(int))\n+##   # It has no value:\n+##   assert(result.has == false)\n+##   # A different way to write it:\n+##   assert(not result)\n+##\n+##   try:\n+##     echo result[]\n+##     assert(false)  # This will not be reached\n+##   except FieldError:  # Because an exception is raised\n+##     discard\n+##\n+## Now let's try out the extraction template. It returns whether a value\n+## is present and injects the value into a variable. It is meant to be used in\n+## a conditional.\n+##\n+## .. code-block:: nim\n+##\n+##   if pos ?= \"nim\".find('i'):\n+##     assert(pos is int)  # This is a normal integer, no tricks.\n+##     echo \"Match found at position \", pos\n+##   else:\n+##     assert(false)  # This will not be reached\n+##\n+## Or maybe you want to get the behavior of the standard library's ``find``,\n+## which returns `-1` if nothing was found.\n+##\n+## .. code-block:: nim\n+##\n+##   assert((\"team\".find('i') or -1) == -1)\n+##   assert((\"nim\".find('i') or -1) == 1)\n+\n+import typetraits\n+\n+\n+type\n+  Maybe*[T] = object\n+    ## An optional type that stores its value and state separately in a boolean.\n+    val: T\n+    has: bool\n+\n+\n+template `?`*(T: typedesc): typedesc =\n+  ## ``?T`` is equivalent to ``Maybe[T]``.\n+  Maybe[T]\n+\n+\n+proc just*[T](val: T): Maybe[T] =\n+  ## Returns a ``Maybe`` that has this value.\n+  result.has = true\n+  result.val = val\n+\n+proc nothing*(T: typedesc): Maybe[T] =\n+  ## Returns a ``Maybe`` for this type that has no value.\n+  result.has = false\n+\n+\n+proc has*(maybe: Maybe): bool =\n+  ## Returns ``true`` if `maybe` isn't `nothing`.\n+  maybe.has\n+\n+converter toBool*(maybe: Maybe): bool =\n+  ## Same as ``has``. Allows to use a ``Maybe`` in boolean context.\n+  maybe.has\n+\n+\n+proc val*[T](maybe: Maybe[T]): T =","path":"lib/pure/optionals.nim","position":131,"original_position":131,"commit_id":"4f25a36481e2665d39c38bbbe3665ea8e7027f48","original_commit_id":"4f25a36481e2665d39c38bbbe3665ea8e7027f48","user":{"login":"flaviut","id":5213469,"avatar_url":"https://avatars.githubusercontent.com/u/5213469?v=3","gravatar_id":"","url":"https://api.github.com/users/flaviut","html_url":"https://github.com/flaviut","followers_url":"https://api.github.com/users/flaviut/followers","following_url":"https://api.github.com/users/flaviut/following{/other_user}","gists_url":"https://api.github.com/users/flaviut/gists{/gist_id}","starred_url":"https://api.github.com/users/flaviut/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/flaviut/subscriptions","organizations_url":"https://api.github.com/users/flaviut/orgs","repos_url":"https://api.github.com/users/flaviut/repos","events_url":"https://api.github.com/users/flaviut/events{/privacy}","received_events_url":"https://api.github.com/users/flaviut/received_events","type":"User","site_admin":false},"body":"This is too easy to use IMO, I'd like something long and verbose like \"unsafeVal\" better.","created_at":"2015-04-20T21:15:23Z","updated_at":"2015-04-20T21:15:23Z","html_url":"https://github.com/Araq/Nim/pull/2515#discussion_r28730044","pull_request_url":"https://api.github.com/repos/Araq/Nim/pulls/2515","_links":{"self":{"href":"https://api.github.com/repos/Araq/Nim/pulls/comments/28730044"},"html":{"href":"https://github.com/Araq/Nim/pull/2515#discussion_r28730044"},"pull_request":{"href":"https://api.github.com/repos/Araq/Nim/pulls/2515"}}},"pull_request":{"url":"https://api.github.com/repos/Araq/Nim/pulls/2515","id":33095787,"html_url":"https://github.com/Araq/Nim/pull/2515","diff_url":"https://github.com/Araq/Nim/pull/2515.diff","patch_url":"https://github.com/Araq/Nim/pull/2515.patch","issue_url":"https://api.github.com/repos/Araq/Nim/issues/2515","number":2515,"state":"open","locked":false,"title":"Add optionals module (Maybe[T])","user":{"login":"BlaXpirit","id":371383,"avatar_url":"https://avatars.githubusercontent.com/u/371383?v=3","gravatar_id":"","url":"https://api.github.com/users/BlaXpirit","html_url":"https://github.com/BlaXpirit","followers_url":"https://api.github.com/users/BlaXpirit/followers","following_url":"https://api.github.com/users/BlaXpirit/following{/other_user}","gists_url":"https://api.github.com/users/BlaXpirit/gists{/gist_id}","starred_url":"https://api.github.com/users/BlaXpirit/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/BlaXpirit/subscriptions","organizations_url":"https://api.github.com/users/BlaXpirit/orgs","repos_url":"https://api.github.com/users/BlaXpirit/repos","events_url":"https://api.github.com/users/BlaXpirit/events{/privacy}","received_events_url":"https://api.github.com/users/BlaXpirit/received_events","type":"User","site_admin":false},"body":"- [**Documentation**](http://blaxpirit.com/f/nim/optionals.html)\r\n\r\n- [Code](https://github.com/BlaXpirit/Nim/blob/optionals/lib/pure/optionals.nim)\r\n\r\n- > This module implements types which encapsulate an optional value.\r\n  > \r\n  > A value of type `?T` (`Maybe[T]`) either contains a value *x* (represented as `just(x)`) or is empty (`nothing[T]`).\r\n  > \r\n  > This can be useful when you have a value that can be present or not. The absence of a value is often represented by `nil`, but it is not always available, nor is it always a good solution.\r\n\r\n- [Discussion in IRC](http://irclogs.nim-lang.org/10-04-2015.html)\r\n\r\nThis pull request is not final. I am looking for feedback.\r\n\r\nI am planning to squash the commits at the end, but for now I will keep the history.\r\n\r\nWhat I don't like:\r\n- <del>No way to opt out of `MaybeDistinct`. I thought just using the class directly instead of `?` would work, but `just` and `nothing` use `?` too.</del>\r\n- You have to define `isNil` for a whole type for it to work with `MaybeDistinct`, but you may want it in just one instance (in which case you could use `distinct`, but that's awkward).\r\n- Names of the classes.","created_at":"2015-04-11T02:00:44Z","updated_at":"2015-04-20T21:15:23Z","closed_at":null,"merged_at":null,"merge_commit_sha":"43acd4528b357e3797005f19971168dca00fe4ad","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/Araq/Nim/pulls/2515/commits","review_comments_url":"https://api.github.com/repos/Araq/Nim/pulls/2515/comments","review_comment_url":"https://api.github.com/repos/Araq/Nim/pulls/comments{/number}","comments_url":"https://api.github.com/repos/Araq/Nim/issues/2515/comments","statuses_url":"https://api.github.com/repos/Araq/Nim/statuses/4f25a36481e2665d39c38bbbe3665ea8e7027f48","head":{"label":"BlaXpirit:optionals","ref":"optionals","sha":"4f25a36481e2665d39c38bbbe3665ea8e7027f48","user":{"login":"BlaXpirit","id":371383,"avatar_url":"https://avatars.githubusercontent.com/u/371383?v=3","gravatar_id":"","url":"https://api.github.com/users/BlaXpirit","html_url":"https://github.com/BlaXpirit","followers_url":"https://api.github.com/users/BlaXpirit/followers","following_url":"https://api.github.com/users/BlaXpirit/following{/other_user}","gists_url":"https://api.github.com/users/BlaXpirit/gists{/gist_id}","starred_url":"https://api.github.com/users/BlaXpirit/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/BlaXpirit/subscriptions","organizations_url":"https://api.github.com/users/BlaXpirit/orgs","repos_url":"https://api.github.com/users/BlaXpirit/repos","events_url":"https://api.github.com/users/BlaXpirit/events{/privacy}","received_events_url":"https://api.github.com/users/BlaXpirit/received_events","type":"User","site_admin":false},"repo":{"id":29466969,"name":"Nim","full_name":"BlaXpirit/Nim","owner":{"login":"BlaXpirit","id":371383,"avatar_url":"https://avatars.githubusercontent.com/u/371383?v=3","gravatar_id":"","url":"https://api.github.com/users/BlaXpirit","html_url":"https://github.com/BlaXpirit","followers_url":"https://api.github.com/users/BlaXpirit/followers","following_url":"https://api.github.com/users/BlaXpirit/following{/other_user}","gists_url":"https://api.github.com/users/BlaXpirit/gists{/gist_id}","starred_url":"https://api.github.com/users/BlaXpirit/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/BlaXpirit/subscriptions","organizations_url":"https://api.github.com/users/BlaXpirit/orgs","repos_url":"https://api.github.com/users/BlaXpirit/repos","events_url":"https://api.github.com/users/BlaXpirit/events{/privacy}","received_events_url":"https://api.github.com/users/BlaXpirit/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/BlaXpirit/Nim","description":"Nim (formerly known as \"Nimrod\") is a compiled, garbage-collected systems programming language which has an excellent productivity/performance ratio. Nim's design focuses on efficiency, expressiveness, elegance (in the order of priority).","fork":true,"url":"https://api.github.com/repos/BlaXpirit/Nim","forks_url":"https://api.github.com/repos/BlaXpirit/Nim/forks","keys_url":"https://api.github.com/repos/BlaXpirit/Nim/keys{/key_id}","collaborators_url":"https://api.github.com/repos/BlaXpirit/Nim/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/BlaXpirit/Nim/teams","hooks_url":"https://api.github.com/repos/BlaXpirit/Nim/hooks","issue_events_url":"https://api.github.com/repos/BlaXpirit/Nim/issues/events{/number}","events_url":"https://api.github.com/repos/BlaXpirit/Nim/events","assignees_url":"https://api.github.com/repos/BlaXpirit/Nim/assignees{/user}","branches_url":"https://api.github.com/repos/BlaXpirit/Nim/branches{/branch}","tags_url":"https://api.github.com/repos/BlaXpirit/Nim/tags","blobs_url":"https://api.github.com/repos/BlaXpirit/Nim/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/BlaXpirit/Nim/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/BlaXpirit/Nim/git/refs{/sha}","trees_url":"https://api.github.com/repos/BlaXpirit/Nim/git/trees{/sha}","statuses_url":"https://api.github.com/repos/BlaXpirit/Nim/statuses/{sha}","languages_url":"https://api.github.com/repos/BlaXpirit/Nim/languages","stargazers_url":"https://api.github.com/repos/BlaXpirit/Nim/stargazers","contributors_url":"https://api.github.com/repos/BlaXpirit/Nim/contributors","subscribers_url":"https://api.github.com/repos/BlaXpirit/Nim/subscribers","subscription_url":"https://api.github.com/repos/BlaXpirit/Nim/subscription","commits_url":"https://api.github.com/repos/BlaXpirit/Nim/commits{/sha}","git_commits_url":"https://api.github.com/repos/BlaXpirit/Nim/git/commits{/sha}","comments_url":"https://api.github.com/repos/BlaXpirit/Nim/comments{/number}","issue_comment_url":"https://api.github.com/repos/BlaXpirit/Nim/issues/comments{/number}","contents_url":"https://api.github.com/repos/BlaXpirit/Nim/contents/{+path}","compare_url":"https://api.github.com/repos/BlaXpirit/Nim/compare/{base}...{head}","merges_url":"https://api.github.com/repos/BlaXpirit/Nim/merges","archive_url":"https://api.github.com/repos/BlaXpirit/Nim/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/BlaXpirit/Nim/downloads","issues_url":"https://api.github.com/repos/BlaXpirit/Nim/issues{/number}","pulls_url":"https://api.github.com/repos/BlaXpirit/Nim/pulls{/number}","milestones_url":"https://api.github.com/repos/BlaXpirit/Nim/milestones{/number}","notifications_url":"https://api.github.com/repos/BlaXpirit/Nim/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/BlaXpirit/Nim/labels{/name}","releases_url":"https://api.github.com/repos/BlaXpirit/Nim/releases{/id}","created_at":"2015-01-19T11:06:22Z","updated_at":"2015-04-13T16:53:57Z","pushed_at":"2015-04-20T18:31:05Z","git_url":"git://github.com/BlaXpirit/Nim.git","ssh_url":"git@github.com:BlaXpirit/Nim.git","clone_url":"https://github.com/BlaXpirit/Nim.git","svn_url":"https://github.com/BlaXpirit/Nim","homepage":"http://nim-lang.org/","size":36806,"stargazers_count":0,"watchers_count":0,"language":"Nimrod","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"devel"}},"base":{"label":"Araq:devel","ref":"devel","sha":"2b4e233510ed45c924507110e19cc94f1d849125","user":{"login":"Araq","id":287816,"avatar_url":"https://avatars.githubusercontent.com/u/287816?v=3","gravatar_id":"","url":"https://api.github.com/users/Araq","html_url":"https://github.com/Araq","followers_url":"https://api.github.com/users/Araq/followers","following_url":"https://api.github.com/users/Araq/following{/other_user}","gists_url":"https://api.github.com/users/Araq/gists{/gist_id}","starred_url":"https://api.github.com/users/Araq/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Araq/subscriptions","organizations_url":"https://api.github.com/users/Araq/orgs","repos_url":"https://api.github.com/users/Araq/repos","events_url":"https://api.github.com/users/Araq/events{/privacy}","received_events_url":"https://api.github.com/users/Araq/received_events","type":"User","site_admin":false},"repo":{"id":842037,"name":"Nim","full_name":"Araq/Nim","owner":{"login":"Araq","id":287816,"avatar_url":"https://avatars.githubusercontent.com/u/287816?v=3","gravatar_id":"","url":"https://api.github.com/users/Araq","html_url":"https://github.com/Araq","followers_url":"https://api.github.com/users/Araq/followers","following_url":"https://api.github.com/users/Araq/following{/other_user}","gists_url":"https://api.github.com/users/Araq/gists{/gist_id}","starred_url":"https://api.github.com/users/Araq/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Araq/subscriptions","organizations_url":"https://api.github.com/users/Araq/orgs","repos_url":"https://api.github.com/users/Araq/repos","events_url":"https://api.github.com/users/Araq/events{/privacy}","received_events_url":"https://api.github.com/users/Araq/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/Araq/Nim","description":"Nim (formerly known as \"Nimrod\") is a compiled, garbage-collected systems programming language which has an excellent productivity/performance ratio. Nim's design focuses on efficiency, expressiveness, elegance (in the order of priority).","fork":false,"url":"https://api.github.com/repos/Araq/Nim","forks_url":"https://api.github.com/repos/Araq/Nim/forks","keys_url":"https://api.github.com/repos/Araq/Nim/keys{/key_id}","collaborators_url":"https://api.github.com/repos/Araq/Nim/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/Araq/Nim/teams","hooks_url":"https://api.github.com/repos/Araq/Nim/hooks","issue_events_url":"https://api.github.com/repos/Araq/Nim/issues/events{/number}","events_url":"https://api.github.com/repos/Araq/Nim/events","assignees_url":"https://api.github.com/repos/Araq/Nim/assignees{/user}","branches_url":"https://api.github.com/repos/Araq/Nim/branches{/branch}","tags_url":"https://api.github.com/repos/Araq/Nim/tags","blobs_url":"https://api.github.com/repos/Araq/Nim/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/Araq/Nim/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/Araq/Nim/git/refs{/sha}","trees_url":"https://api.github.com/repos/Araq/Nim/git/trees{/sha}","statuses_url":"https://api.github.com/repos/Araq/Nim/statuses/{sha}","languages_url":"https://api.github.com/repos/Araq/Nim/languages","stargazers_url":"https://api.github.com/repos/Araq/Nim/stargazers","contributors_url":"https://api.github.com/repos/Araq/Nim/contributors","subscribers_url":"https://api.github.com/repos/Araq/Nim/subscribers","subscription_url":"https://api.github.com/repos/Araq/Nim/subscription","commits_url":"https://api.github.com/repos/Araq/Nim/commits{/sha}","git_commits_url":"https://api.github.com/repos/Araq/Nim/git/commits{/sha}","comments_url":"https://api.github.com/repos/Araq/Nim/comments{/number}","issue_comment_url":"https://api.github.com/repos/Araq/Nim/issues/comments{/number}","contents_url":"https://api.github.com/repos/Araq/Nim/contents/{+path}","compare_url":"https://api.github.com/repos/Araq/Nim/compare/{base}...{head}","merges_url":"https://api.github.com/repos/Araq/Nim/merges","archive_url":"https://api.github.com/repos/Araq/Nim/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/Araq/Nim/downloads","issues_url":"https://api.github.com/repos/Araq/Nim/issues{/number}","pulls_url":"https://api.github.com/repos/Araq/Nim/pulls{/number}","milestones_url":"https://api.github.com/repos/Araq/Nim/milestones{/number}","notifications_url":"https://api.github.com/repos/Araq/Nim/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/Araq/Nim/labels{/name}","releases_url":"https://api.github.com/repos/Araq/Nim/releases{/id}","created_at":"2010-08-16T20:38:58Z","updated_at":"2015-04-20T20:54:26Z","pushed_at":"2015-04-20T20:54:24Z","git_url":"git://github.com/Araq/Nim.git","ssh_url":"git@github.com:Araq/Nim.git","clone_url":"https://github.com/Araq/Nim.git","svn_url":"https://github.com/Araq/Nim","homepage":"http://nim-lang.org/","size":349640,"stargazers_count":1684,"watchers_count":1684,"language":"Nimrod","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":232,"mirror_url":null,"open_issues_count":475,"forks":232,"open_issues":475,"watchers":1684,"default_branch":"devel"}},"_links":{"self":{"href":"https://api.github.com/repos/Araq/Nim/pulls/2515"},"html":{"href":"https://github.com/Araq/Nim/pull/2515"},"issue":{"href":"https://api.github.com/repos/Araq/Nim/issues/2515"},"comments":{"href":"https://api.github.com/repos/Araq/Nim/issues/2515/comments"},"review_comments":{"href":"https://api.github.com/repos/Araq/Nim/pulls/2515/comments"},"review_comment":{"href":"https://api.github.com/repos/Araq/Nim/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/Araq/Nim/pulls/2515/commits"},"statuses":{"href":"https://api.github.com/repos/Araq/Nim/statuses/4f25a36481e2665d39c38bbbe3665ea8e7027f48"}}}}