typelevel / squants

The Scala API for Quantities, Units of Measure and Dimensional Analysis
https://www.squants.com
Apache License 2.0
922 stars 122 forks source link

Manage multiple dimensions in a single method #214

Open bsoufflet opened 7 years ago

bsoufflet commented 7 years ago

The following method do not compile because of the error display in comment below. Is there a way to fix this method without doing a .asInstanceOf[Seq[T]]?

def test[T<:Quantity[T]](str: String): Seq[T] = {
    str match {
      case "temperature" => Seq(Celsius(3), Celsius(4)) // Error:type mismatch; found: squants.thermal.Temperature required: T
      case "length" => Seq(Meters(3), Meters(3), Meters(3)) // Error:type mismatch; found: squants.space.Length required: T
    }
  }

Thanks !

garyKeorkunian commented 7 years ago

The primary intention of squants is to ensure that dimensional operations are typed in accordance with the rules of dimensional analysis. This generally means that application code should know the Quantity types it is dealing with.

Your example is essentially a factory that could return any type of quantity. Once you have those quantities, what would you do with them? How would you know what you could do with them?

Perhaps you can describe your use case more. There may be another solution to what you are trying to do.

bsoufflet commented 7 years ago

Hi, you will find below some context.

I am working on an application which display different types of measure (Temperature, voltage, pressure, ...) on charts. There are 30 different types of measures with different units.

The frontend application is asking to the Scala backend for a specific data-serie (one single parameter) and the backend will return this data-serie as JSON. In the process there could be measurement unit conversion to the user unit.

I would like to have a single type of object which store a data serie. Whatever the dimension of the measure is and do the unit conversion and JSON conversion at the end.

Thanks for your help !

garyKeorkunian commented 7 years ago

If you need to unit conversions you will need the specific quantity types to do that. Once that is done, you can transform to JSON using a format that is unified among types and respond with that to the UI.

Therefore, I think the collections you are building (pre unit conversion) need to be for each specific type. I assume you are pulling those from a DB of sorts.

I don't think a generic method will work.

garyKeorkunian commented 6 years ago

@bsoufflet It's been some time since you created this issue. The recent change making Dimension.parseString public could help with your use case.