This page is no longer maintained — Please continue to the home page at www.scala-lang.org

postfix operator

3 replies
jukz
Joined: 2009-11-05,
User offline. Last seen 32 weeks 5 days ago.

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

 

Tony Morris 2
Joined: 2009-03-20,
User offline. Last seen 42 years 45 weeks ago.
Re: postfix operator

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***
>

dcsobral
Joined: 2009-04-23,
User offline. Last seen 38 weeks 5 days ago.
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:

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.
ichoran
Joined: 2009-08-14,
User offline. Last seen 2 years 3 weeks ago.
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

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland