Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

num.parse #8237

Closed
floitschG opened this issue Jan 31, 2013 · 8 comments
Closed

num.parse #8237

floitschG opened this issue Jan 31, 2013 · 8 comments
Assignees
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. type-enhancement A request for a change that isn't a bug

Comments

@floitschG
Copy link
Contributor

There is int.parse, double.parse but no num.parse.

It should return an int if the string could be the output of ints (or if it's a hexadecimal literal), and otherwise return a double.

@lrhn
Copy link
Member

lrhn commented Jan 31, 2013

I would make it parse any number literal allowed in the lexical grammar, and give the corresponding value.
On the other hand, I'm not convinced that it's really preferable to allow too much in a parse method. You should be wary of use-cases where you need to parse something, but don't actually know the format.


Removed Type-Defect label.
Added Type-Enhancement label.

@floitschG
Copy link
Contributor Author

Basically I want:
foo(num x) {
  identical(num.parse(x.toString()));
}

@floitschG
Copy link
Contributor Author

Semantically I propose:

num.parse(x, errorHandler) {
  bool isInt = true;
  num result = int.parse(x, onError(() { isInt = false; return 0; }));
  if (isInt) return result;
  bool isDouble = true;
  result = double.parse(x, (
) { isDouble = false; return 0.0; }));
  if (isDouble) return result;
  return errHandler(x);
}


Added Accepted label.

@DartBot
Copy link

DartBot commented May 1, 2013

This comment was originally written by @butlermatt


I'd like to see this as well. Florian's comment #­2 is what I would expect as well. Since the 'best practice' seems to be to be to prefer 'num' unless you explicitly require an int/double, I would expect that a num.parse() exist.

With this missing it actually seems to enforce that you should use the explicit subtypes rather than num itself.

@floitschG
Copy link
Contributor Author

Added Ready-to-implement label.

@lrhn
Copy link
Member

lrhn commented Nov 25, 2013

Set owner to @lrhn.
Added Started label.

@lrhn
Copy link
Member

lrhn commented Nov 25, 2013

Issue #1981 has been merged into this issue.

@lrhn
Copy link
Member

lrhn commented Dec 2, 2013

Added Fixed label.

@floitschG floitschG added Type-Enhancement area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. labels Dec 2, 2013
@kevmoo kevmoo added type-enhancement A request for a change that isn't a bug and removed type-enhancement labels Mar 1, 2016
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

4 participants