bcicen / go-units

Go library for converting between various units of measurement
MIT License
84 stars 22 forks source link

Question: Is there a way to separate out a specific type of unit? #29

Open jwir3 opened 1 year ago

jwir3 commented 1 year ago

For example, if I wanted to be more constrained about the specific type of unit I wanted:

func MakeComputation(value float64, temp units.Temperature) {
... 
}

In this particular example, unit.Temperature would refer to any of unit.Celsius, unit.Fahrenheit, or unit.Kelvin. Is there a way to make this slightly more specific like this?

Leila-Codes commented 9 months ago

I'm not sure if I understand the question or requirement sorry @jwir3, I should think if you're writing your own function, you can absolutely enforce any specific type on it?

func MakeComputation(value float64, temp units.Celcius) {
... 
}

You could even write a generic function in later Go versions...

func MakeComputation[T units.Temperature](value float64, unit T) {
...
}

Can you perhaps further expand/explain your requirement?