go-units icon indicating copy to clipboard operation
go-units copied to clipboard

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

Open jwir3 opened this issue 1 year ago • 1 comments

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?

jwir3 avatar Aug 13 '23 22:08 jwir3

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?

Leila-Codes avatar Feb 05 '24 11:02 Leila-Codes