squants icon indicating copy to clipboard operation
squants copied to clipboard

Price does not work on right side of * operator

Open mkotsbak opened this issue 7 years ago • 4 comments

import scala.language.postfixOps
import squants.energy.EnergyConversions._
import squants.energy.PowerConversions._
import squants.information.InformationConversions._
import squants.market.MoneyConversions._
import squants.space.LengthConversions._
import squants.time.TimeConversions._

val price  = 1.USD / 1.kWh
val energy = 4.MWh

val works = price * energy
val compileError = energy * price

ScalaFiddle.scala:13: error: overloaded method value $times with alternatives: ( that: time.this.Frequency)energy.this.Power ( that: scala.this.Double)energy.this.Energy cannot be applied to (market.this.Price[energy.this.Energy]) val compileError = energy * price ^

Am I missing any imports here or is there something missing on the operator * implementation of the unit types?

mkotsbak avatar Sep 13 '18 15:09 mkotsbak

There is no method with the signature:

def*(that: A): Money

in Price. Maybe there should be? Or maybe the compiler doesn't like that syntax?

hunterpayne avatar Jan 18 '19 23:01 hunterpayne

Nope, what is missing is this:

diff --git a/shared/src/main/scala/squants/Quantity.scala b/shared/src/main/scala/squants/Quantity.scala
index 86439d5..2b34ee2 100644
--- a/shared/src/main/scala/squants/Quantity.scala
+++ b/shared/src/main/scala/squants/Quantity.scala
@@ -64,6 +64,8 @@ abstract class Quantity[A <: Quantity[A]] extends Serializable with Ordered[A] {
   def times(that: Double): A = unit(this.value * that)
   def *(that: Double): A = times(that)
 
+  def *(that: Price[A]): Money = that * this

I can prepare a PR.

mkotsbak avatar Feb 27 '19 20:02 mkotsbak

I think what you propose should be added @mkotsbak but you asked for Price * A and it seems you are providing A * Price. Both should work and the override you propose provides one of them. If you make a PR, please add tests (and overrides) that cover both Price * A and A * Price. That will likely make the team happier and more likely for them to approve your PR quickly.

hunterpayne avatar Feb 27 '19 20:02 hunterpayne

Ah, sorry, title was wrong :) Jupp of course I added tests first..Numbers at left side of * Price should probably also be supported.

mkotsbak avatar Feb 28 '19 07:02 mkotsbak