{"action":"created","issue":{"url":"https://api.github.com/repos/ceylon/ceylon/issues/5796","labels_url":"https://api.github.com/repos/ceylon/ceylon/issues/5796/labels{/name}","comments_url":"https://api.github.com/repos/ceylon/ceylon/issues/5796/comments","events_url":"https://api.github.com/repos/ceylon/ceylon/issues/5796/events","html_url":"https://github.com/ceylon/ceylon/issues/5796","id":120571281,"number":5796,"title":"confusion about parseInteger","user":{"login":"ePaul","id":645859,"avatar_url":"https://avatars.githubusercontent.com/u/645859?v=3","gravatar_id":"","url":"https://api.github.com/users/ePaul","html_url":"https://github.com/ePaul","followers_url":"https://api.github.com/users/ePaul/followers","following_url":"https://api.github.com/users/ePaul/following{/other_user}","gists_url":"https://api.github.com/users/ePaul/gists{/gist_id}","starred_url":"https://api.github.com/users/ePaul/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ePaul/subscriptions","organizations_url":"https://api.github.com/users/ePaul/orgs","repos_url":"https://api.github.com/users/ePaul/repos","events_url":"https://api.github.com/users/ePaul/events{/privacy}","received_events_url":"https://api.github.com/users/ePaul/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":13,"created_at":"2015-12-05T16:45:43Z","updated_at":"2015-12-05T17:43:51Z","closed_at":null,"body":"I just tried to solve a [Codegolf task][2], paraphrased:\r\n\r\n> Given a string, parse it in all base-n representations (n in 2..36) where that makes sense (i.e. no digit >= n), and add all the results together.\r\n\r\nThis looks like a straightforward implementation:\r\n\r\n```ceylon\r\nInteger baseSum(String string) =>\r\n        sum((2..36).map((radix) => parseInteger(string, radix) else 0));\r\n```\r\n\r\n(This function tries to parse the string in each basis, uses 0 where that doesn't succeed, and sums the results up.)\r\n\r\n\r\nIt works for most examples, but when passed `\"2T\"`, it returns `2000000000665` instead of the wanted `665`.\r\n\r\nIt turns out that `parseInteger(string, radix)` has some special handling when `radix` is 10 – then the suffixes \"kMGTP\" have their SI prefix meaning, so this is \"2 Tera + 665\".\r\n\r\n[The documentation seems to support this][2]:\r\n\r\n> The syntax accepted by this function is the same as the syntax for\r\n> an Integer literal in the Ceylon language except that it may optionally\r\n> begin with a sign character (+ or -) and may not contain grouping\r\n> underscore characters.\r\n\r\nBut that actually is also not true ... while the [\"Numeric literals\" section][3] in the Ceylon language spec allows these suffixes, it also allows some prefixes.\r\n\r\nTry to guess what this code prints:\r\n\r\n```ceylon\r\nprint(2T);\r\nprint(parseInteger(\"2T\"));\r\nprint(parseInteger(\"2T\", 10));\r\nprint($100);\r\nprint(parseInteger(\"$100\"));\r\nprint(parseInteger(\"$100\", 2));\r\nprint(#100);\r\nprint(parseInteger(\"#100\"));\r\nprint(parseInteger(\"#100\", 16));\r\nprint(2_000);\r\nprint(parseInteger(\"2_000\"));\r\nprint(parseInteger(\"2_000\", 10));\r\n```\r\n\r\nThe output (with Ceylon 1.2):\r\n\r\n```\r\n2000000000000\r\n2000000000000\r\n2000000000000\r\n4\r\n<null>\r\n<null>\r\n256\r\n<null>\r\n<null>\r\n2000\r\n<null>\r\n<null>\r\n```\r\n\r\nSo `parseInteger` doesn't **actually** support the integer literal syntax, just the part of it which corresponds to decimal literals (and even that not completely, as the grouping using `_` is also not supported).\r\n\r\n`parseInteger` also behaves as expected (by me) for all bases other than 10, and just has this strange exception for accepting these SI-prefix-like suffixes.\r\n\r\nI could imagine this alternative (\"more sensible\", in my eyes) behavior, if we want to support both \"literal parsing\" and \"arbitrary base parsing\":\r\n\r\n* When given an explicit radix parameter (whether 10 or not), just parse the string in this base. I.e. the SI suffixes would not be allowed.\r\n* When given no radix parameter (or some special value indicating this), parse the string as a Ceylon Integer literal (including both decimal literals with possible SI suffixes, and also binary and hexadecimal literals with their `$` and `#` prefixes).\r\n\r\nSo the type of `parseInteger` could be `Integer(String,Integer|\\IceylonLiteral=)`, with a `ceylonLiteral` object indicating that the second version should be used (and this would be the same as not passing the parameter).\r\n\r\nI would guess there are not that many programs actually relying on the current behavior compared to this one.\r\n\r\n[1]: http://codegolf.stackexchange.com/questions/65748/find-the-sum-of-all-possible-base-representations\r\n[2]: http://modules.ceylon-lang.org/repo/1/ceylon/language/1.2.0/module-doc/api/index.html#parseInteger\r\n[3]: http://ceylon-lang.org/documentation/1.2/spec/html/lexical.html#numericliterals"},"comment":{"url":"https://api.github.com/repos/ceylon/ceylon/issues/comments/162228731","html_url":"https://github.com/ceylon/ceylon/issues/5796#issuecomment-162228731","issue_url":"https://api.github.com/repos/ceylon/ceylon/issues/5796","id":162228731,"user":{"login":"gavinking","id":579974,"avatar_url":"https://avatars.githubusercontent.com/u/579974?v=3","gravatar_id":"","url":"https://api.github.com/users/gavinking","html_url":"https://github.com/gavinking","followers_url":"https://api.github.com/users/gavinking/followers","following_url":"https://api.github.com/users/gavinking/following{/other_user}","gists_url":"https://api.github.com/users/gavinking/gists{/gist_id}","starred_url":"https://api.github.com/users/gavinking/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gavinking/subscriptions","organizations_url":"https://api.github.com/users/gavinking/orgs","repos_url":"https://api.github.com/users/gavinking/repos","events_url":"https://api.github.com/users/gavinking/events{/privacy}","received_events_url":"https://api.github.com/users/gavinking/received_events","type":"User","site_admin":false},"created_at":"2015-12-05T17:43:51Z","updated_at":"2015-12-05T17:43:51Z","body":"Well, let me walk that back. After [checking wikipedia](https://en.wikipedia.org/wiki/Scientific_notation#Other_bases), I guess I *can* imagine supporting it.\r\n\r\nPerhaps we should enhance the actual language to support scientific notation for binary and hexadecimal literals?"}}