Skip to content

String.toInt, .toDouble should allow a default parameter #16

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

Closed
scabug opened this issue Aug 28, 2007 · 11 comments · Fixed by scala/scala#6538
Closed

String.toInt, .toDouble should allow a default parameter #16

scabug opened this issue Aug 28, 2007 · 11 comments · Fixed by scala/scala#6538

Comments

@scabug
Copy link

scabug commented Aug 28, 2007

Aladdin: [http://scala-webapps.epfl.ch/bugtracking/bugs/displayItem.do?id=1299 bug 1299], [http://scala-webapps.epfl.ch/bugtracking/contribs/display.do?id=773 contrib 773]

== Code ==

object Foo {
   // this is overly verbose
   def getInt(s : String) = try {
     s.toInt
   } catch {
     case e : NumberFormatException => 0
   }

   // I'd much rather write this
   def getInt2(s : String) = s.toInt(0)
}

== What happened ==

n/a

== What expected ==

To be able to optionally provide a default value for string-to-primitive conversion methods, much like Option.get.

@scabug
Copy link
Author

scabug commented Aug 28, 2007

Imported From: https://issues.scala-lang.org/browse/SI-16?orig=1
Reporter: @michelou

@scabug
Copy link
Author

scabug commented Mar 23, 2008

Aaron Harnly (aaronharnly) said:
Or perhaps more straightforwardly, returning an Option[Int].

@scabug
Copy link
Author

scabug commented May 14, 2009

@odersky said:
I am against the proliferation of default parameters or option results in the library.
If Java decided against this, I do not see why Scala should reach a different decision. Nothing prevents anyone from defining a method

numSafe[A](foo: => A): Option[A] =
try { Some(foo) } catch { ex: NumberFormatException => None }

to deal with these situations once and for all in a systematic manner.

@scabug
Copy link
Author

scabug commented May 14, 2009

@paulp said:
Replying to [comment:6 odersky]:

numSafe[A](foo: => A): Option[A] =
try { Some(foo) } catch { ex: NumberFormatException => None }

to deal with these situations once and for all in a systematic manner.

I agree, it's that wrapper (or something like it, and things similar to it) which I do think belong in the standard library somewhere. The mere fact that the exact same wrapping is needed in multiple places in scalac itself is a pretty reasonable argument for it, not to mention the likely requirements of the remainder of the world.

"Once and for all" + "systematic" == "let's do this once for everyone..."

@scabug
Copy link
Author

scabug commented May 14, 2009

@odersky said:
Yes, your idea of making a wrapper sounds like the right thing to do. How about `optional'? It probably could do into Predef. Or do you have a better idea?

@scabug
Copy link
Author

scabug commented May 14, 2009

@paulp said:
Replying to [comment:8 odersky]:

Yes, your idea of making a wrapper sounds like the right thing to do. How about `optional'? It probably could do into Predef. Or do you have a better idea?

I'm torn, as I think there's already too much in Predef, which I would rather see broken down. This issue is well intertwined with (at least) my hopes for a) a much easier import/export mechanism b) a generalized exception handling mechanism. So it's hard to say whether I have a better idea, it depends a bit on the appropriate level of ambition.

I would definitely prefer to call it "opt" though, since making this lightweight is key.

Just thinking out loud now, because what better place than closed trac ticket #16 could there be for me to flesh out my ideas, but:

def opt[T](f: => T)(implicit pf: PartialFunction[Either[Throwable,T]] => Boolean): Option[T] = ...

The gist being that somehow a function must be supplied which partitions both return values and exceptions into Some, None, or neither (that last being exceptions which you did not opt to catch, which will simply fly right on through.)

Now very nice wrappers can be written. This approach also generalizes the oft-rewritten

def onull[T](x: T): Option[T] = if (x == null) None else Some(x)

...but now that could be composed with other criteria or exceptions.

@scabug
Copy link
Author

scabug commented Nov 19, 2011

aunndroid said:
The issue is raised to avoid the verbose exception handling. How about provide a way to detect the exception beforehand ? Like RichChar's isDigit. In this case, isNumber.

@scabug
Copy link
Author

scabug commented May 4, 2012

@soc said:
I don't know if there is any interest left, but at least the original proposal has the problem that the style suggested is normally used for specifying radixes, not default values.

@SethTisue
Copy link
Member

there is an active discussion on this at scala/collection-strawman#431

@SethTisue
Copy link
Member

has PR now, reviewers wanted: scala/scala#6538

@SethTisue
Copy link
Member

for fixing the last open two-digit Scala issue, @martijnhoekstra officially gets to wear a big, shiny golden crown whenever he wants.

https://github.com/scala/bug/issues?q=is%3Aopen+is%3Aissue+sort%3Acreated-asc shows 16 remaining open three-digit issues, for anyone interested in earning the right to wear an only slightly smaller and slightly less jewel-encrusted crown

@scala scala deleted a comment from scabug Jun 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants