{"action":"created","issue":{"url":"https://api.github.com/repos/purescript/purescript/issues/1928","repository_url":"https://api.github.com/repos/purescript/purescript","labels_url":"https://api.github.com/repos/purescript/purescript/issues/1928/labels{/name}","comments_url":"https://api.github.com/repos/purescript/purescript/issues/1928/comments","events_url":"https://api.github.com/repos/purescript/purescript/issues/1928/events","html_url":"https://github.com/purescript/purescript/issues/1928","id":138953822,"number":1928,"title":"Syntax to improve readability for exceptional cases","user":{"login":"kRITZCREEK","id":6189397,"avatar_url":"https://avatars.githubusercontent.com/u/6189397?v=3","gravatar_id":"","url":"https://api.github.com/users/kRITZCREEK","html_url":"https://github.com/kRITZCREEK","followers_url":"https://api.github.com/users/kRITZCREEK/followers","following_url":"https://api.github.com/users/kRITZCREEK/following{/other_user}","gists_url":"https://api.github.com/users/kRITZCREEK/gists{/gist_id}","starred_url":"https://api.github.com/users/kRITZCREEK/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kRITZCREEK/subscriptions","organizations_url":"https://api.github.com/users/kRITZCREEK/orgs","repos_url":"https://api.github.com/users/kRITZCREEK/repos","events_url":"https://api.github.com/users/kRITZCREEK/events{/privacy}","received_events_url":"https://api.github.com/users/kRITZCREEK/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/purescript/purescript/labels/enhancement","name":"enhancement","color":"84b6eb"},{"url":"https://api.github.com/repos/purescript/purescript/labels/syntax","name":"syntax","color":"5319e7"}],"state":"open","locked":false,"assignee":null,"milestone":{"url":"https://api.github.com/repos/purescript/purescript/milestones/8","html_url":"https://github.com/purescript/purescript/milestones/Ideas","labels_url":"https://api.github.com/repos/purescript/purescript/milestones/8/labels","id":714450,"number":8,"title":"Ideas","description":"Ideas which may or may not be implemented, for discussion.","creator":{"login":"paf31","id":630306,"avatar_url":"https://avatars.githubusercontent.com/u/630306?v=3","gravatar_id":"","url":"https://api.github.com/users/paf31","html_url":"https://github.com/paf31","followers_url":"https://api.github.com/users/paf31/followers","following_url":"https://api.github.com/users/paf31/following{/other_user}","gists_url":"https://api.github.com/users/paf31/gists{/gist_id}","starred_url":"https://api.github.com/users/paf31/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/paf31/subscriptions","organizations_url":"https://api.github.com/users/paf31/orgs","repos_url":"https://api.github.com/users/paf31/repos","events_url":"https://api.github.com/users/paf31/events{/privacy}","received_events_url":"https://api.github.com/users/paf31/received_events","type":"User","site_admin":false},"open_issues":73,"closed_issues":42,"state":"open","created_at":"2014-07-08T20:33:33Z","updated_at":"2016-03-07T21:28:10Z","due_on":null,"closed_at":null},"comments":20,"created_at":"2016-03-07T11:14:06Z","updated_at":"2016-03-08T11:44:56Z","closed_at":null,"body":"## Syntax extension proposal\r\n\r\nTaking a cue from Idris which in turn was inspired by Perl's [or die syntax](https://youtu.be/rXXn4UunOkE?t=1851), I suggest adding syntax for handling exceptional cases in do blocks.\r\n\r\nTurning this situation:\r\n```purescript\r\nmain = do\r\n  c <- getCanvasById \"canvas\"\r\n  case c of\r\n    Nothing -> throwError \"Canvas Element could not be found\"\r\n    Just c' -> do\r\n       ctx <- getContext2D c'\r\n       ...\r\n```\r\n... into this (which still passes the totality checker):\r\n```purescript\r\nmain = do\r\n  Just c <- getCanvasById \"canvas\" | Nothing => throwError \"Canvas Element could not be found\"\r\n  ctx <- getContext2D c\r\n  ...\r\n```\r\n\r\nThe general idea is to reduce the indentation level incurred by exceptional cases that just return with a single statement.\r\n\r\nIn case the line gets too long I suggest we allow breaking the exceptional case onto it's own line with indentation:\r\n\r\n```purescript\r\nmain = do\r\n  Just c <- getCanvasById \"canvas\" \r\n    | Nothing => throwError \"Canvas Element could not be found\"\r\n  ctx <- getContext2D c\r\n  ...\r\n```\r\n\r\nAnother consideration to make is if we wanted to allow multiple cases to be specified:\r\n\r\n```purescript\r\nmain = do\r\n  Just (Just c) <- getCanvasById \"canvas\"\r\n    | (Just Nothing) => throwError \"Element was not a Canvas element\"\r\n    | Nothing        => throwError \"Canvas Element could not be found\"\r\n  ctx <- getContext2D c\r\n  ...\r\n```\r\n\r\nRight now I think it would be better to only allow one alternative to reduce the urge to cram more extensive errorhandling into oneliners. The main purpose is to reduce partiality without taking a hit on readability."},"comment":{"url":"https://api.github.com/repos/purescript/purescript/issues/comments/193746289","html_url":"https://github.com/purescript/purescript/issues/1928#issuecomment-193746289","issue_url":"https://api.github.com/repos/purescript/purescript/issues/1928","id":193746289,"user":{"login":"garyb","id":693642,"avatar_url":"https://avatars.githubusercontent.com/u/693642?v=3","gravatar_id":"","url":"https://api.github.com/users/garyb","html_url":"https://github.com/garyb","followers_url":"https://api.github.com/users/garyb/followers","following_url":"https://api.github.com/users/garyb/following{/other_user}","gists_url":"https://api.github.com/users/garyb/gists{/gist_id}","starred_url":"https://api.github.com/users/garyb/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/garyb/subscriptions","organizations_url":"https://api.github.com/users/garyb/orgs","repos_url":"https://api.github.com/users/garyb/repos","events_url":"https://api.github.com/users/garyb/events{/privacy}","received_events_url":"https://api.github.com/users/garyb/received_events","type":"User","site_admin":false},"created_at":"2016-03-08T11:44:56Z","updated_at":"2016-03-08T11:44:56Z","body":"I wasn't suggesting `LambdaCase` solves this, just someone else mentioned it so I thought I'd explain the plans for that. Although if you were using `throwError` like your original example it would help, as you could do away with the block in the `Just`, as per @AppShipIt's example.\r\n\r\n:-1: from me as I think this is can already be avoided using a few different approaches as it is (`where`, transformers, prisms, etc)."}}