spire.macros.Checked#checked Usage?

14 views
Skip to first unread message

Kevin Meredith

unread,
Jun 23, 2015, 9:43:04 PM6/23/15
to type...@googlegroups.com
I'm trying to understand spire.macros.Checked:

  /**
   * Performs overflow checking for Int/Long operations.
   * 
   * If no errors are detected, the expected result will be
   * returned. If an error is detected, an ArithmeticOverflowException
   * will be thrown.
   */
  def checked[A](n: A): A = macro checkedImpl[A]

  def checkedImpl[A: c.WeakTypeTag](c: Context)(n: c.Expr[A]): c.Expr[A] = {
    import c.universe._
    tryOrElseImpl[A](c)(n)(c.Expr[A](q"throw new spire.macros.ArithmeticOverflowException()"))
  }

Could you please give an example of how to use `checked`? Naively, I'm also curious if this exception gets thrown at compile-time or run-time.

But it's not possible (or perhaps it's quite hard) to write a macro that performs an overflow check for String#toDouble, is that true?
Reply all
Reply to author
Forward
0 new messages