- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
postfix operator
Mon, 2009-11-16, 10:18
I cannot define an factorial (“!”) Operator. Ideas?
final class Real(val value:Double) {
def ! :Real=new Real(this.value+1) //FIXME
override def toString=value.toString
}
object Real {
def createReal(x : Double):Real=new Real(x)
def ^(thi : Double, that : Double):Double=thi+that // FIXME
implicit def wrapDoubleToReal(x: Double):Real = Real.createReal(x)
implicit def wrapDoubleToReal(x: Int):Real = Real.createReal(x)
}
implicit def wrap(x: Double):myDouble = new myDouble(x)
import Real._
var t=createReal(2)
t=t ! // does not compile
t=t.! // does work
Mon, 2009-11-16, 14:07
#2
Re: postfix operator
It sure does compile here, aside from the non-existent myDouble. What _is_ the error you are getting?
Also, please not that when a line terminates in a parameterless operator in postfix notation, the next line must be empty, otherwise Scala will try to interpret that operator as an infix operator. That's way up there on my list of annoyances with Scala -- no way to force a postfix operator to be compiled as a postfix operator without resorting to parenthesis.
On Mon, Nov 16, 2009 at 7:18 AM, Kubitz, Jörg <jkubitz [at] zeb [dot] de> wrote:
--
Daniel C. Sobral
Veni, vidi, veterni.
On Mon, Nov 16, 2009 at 7:18 AM, Kubitz, Jörg <jkubitz [at] zeb [dot] de> wrote:
I cannot define an factorial (“!”) Operator. Ideas?
final class Real(val value:Double) {
def ! :Real=new Real(this.value+1) //FIXME
override def toString=value.toString
}
object Real {
def createReal(x : Double):Real=new Real(x)
def ^(thi : Double, that : Double):Double=thi+that // FIXME
implicit def wrapDoubleToReal(x: Double):Real = Real.createReal(x)
implicit def wrapDoubleToReal(x: Int):Real = Real.createReal(x)
}
implicit def wrap(x: Double):myDouble = new myDouble(x)
import Real._
var t=createReal(2)
t=t ! // does not compile
t=t.! // does work
--
Daniel C. Sobral
Veni, vidi, veterni.
Mon, 2009-11-16, 17:37
#3
Re: postfix operator
Not sure what's going wrong: factorial-using-! seems to work for me. (Here I really did only implement factorial, not the gamma function, although the latter would make more sense on doubles.)
Try adding a ; to the end of the line where you're using it? Maybe it's trying to run on to the next line?
--Rex
scala> class FactorialDouble(d:Double) {
| def ! = fac(d)
| private def fac(e:Double):Double = if (e<=1.0) 1.0 else e*(fac(e-1.0))
| }
defined class FactorialDouble
scala> implicit def factorializeMyDouble(d:Double) = new FactorialDouble(d)
factorializeMyDouble: (d: Double)FactorialDouble
scala> 5.0!
res0: Double = 120.0
scala> 5.0 !
res1: Double = 120.0
scala> val d=5.0;
d: Double = 5.0
scala> d!
res2: Double = 120.0
Try adding a ; to the end of the line where you're using it? Maybe it's trying to run on to the next line?
--Rex
scala> class FactorialDouble(d:Double) {
| def ! = fac(d)
| private def fac(e:Double):Double = if (e<=1.0) 1.0 else e*(fac(e-1.0))
| }
defined class FactorialDouble
scala> implicit def factorializeMyDouble(d:Double) = new FactorialDouble(d)
factorializeMyDouble: (d: Double)FactorialDouble
scala> 5.0!
res0: Double = 120.0
scala> 5.0 !
res1: Double = 120.0
scala> val d=5.0;
d: Double = 5.0
scala> d!
res2: Double = 120.0
Hi Kubitz, consider perhaps call it unary_! which is a special-case name.
final class Real(val value:Double) {
def ! :Real=new Real(this.value+1) //FIXME
def unary_! = this.!
override def toString=value.toString
}
object Real {
def createReal(x : Double):Real=new Real(x)
def ^(thi : Double, that : Double):Double=thi+that // FIXME
implicit def wrapDoubleToReal(x: Double):Real = Real.createReal(x)
implicit def wrapDoubleToReal(x: Int):Real = Real.createReal(x)
val g = createReal(2)
val h = g.!
val i = ! g
}
Kubitz wrote:
>
> I cannot define an factorial (“!”) Operator. Ideas?
>
> final class Real(val value:Double) {
>
> def ! :Real=new Real(this.value+1) //FIXME
>
> override def toString=value.toString
>
> }
>
> object Real {
>
> def createReal(x : Double):Real=new Real(x)
>
> def ^(thi : Double, that : Double):Double=thi+that // FIXME
>
> implicit def wrapDoubleToReal(x: Double):Real = Real.createReal(x)
>
> implicit def wrapDoubleToReal(x: Int):Real = Real.createReal(x)
>
> }
>
> implicit def wrap(x: Double):myDouble = new myDouble(x)
>
> import Real._
>
> var t=createReal(2)
>
> * **t**=**t** **_!_** // does not compile***
>
> * **t**=**t.**_!_** // does work***
>