Skip to content
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

Price does not work on right side of * operator #314

Open
mkotsbak opened this issue Sep 13, 2018 · 4 comments
Open

Price does not work on right side of * operator #314

mkotsbak opened this issue Sep 13, 2018 · 4 comments

Comments

@mkotsbak
Copy link
Contributor

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?

@hunterpayne
Copy link
Contributor

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?

@mkotsbak
Copy link
Contributor Author

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.

@hunterpayne
Copy link
Contributor

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.

@mkotsbak mkotsbak changed the title Price does not work on left side of * operator Price does not work on right side of * operator Feb 28, 2019
@mkotsbak
Copy link
Contributor Author

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

garyKeorkunian added a commit that referenced this issue Sep 5, 2019
#314: add missing operations with Price, Quantity and numbers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants