Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@ type DozenWidgets struct{}

func (DozenWidgets) ToBase(v float64) float64 { return v * 12 }
func (DozenWidgets) FromBase(v float64) float64 { return v / 12 }
func (DozenWidgets) Dimension() Widget { return Widget{} }

widgets := quant.New[Widget](2, DozenWidgets{}).To(DozenWidgets{}) // 2
```
Expand Down
1 change: 1 addition & 0 deletions quantity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,7 @@ type testDozenWidgets struct{}

func (testDozenWidgets) ToBase(v float64) float64 { return v * 12 }
func (testDozenWidgets) FromBase(v float64) float64 { return v / 12 }
func (testDozenWidgets) Dimension() testWidgets { return testWidgets{} }

func TestCustomDimensionAndUnit(t *testing.T) {
q := quant.New[testWidgets](2, testDozenWidgets{})
Expand Down
7 changes: 7 additions & 0 deletions unit.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type Dimension interface {
type Unit[D Dimension] interface {
ToBase(float64) float64
FromBase(float64) float64
Dimension() D
}

// BuiltinUnit is the concrete unit type used by quant's built-in units.
Expand All @@ -30,6 +31,12 @@ func (u BuiltinUnit[D]) FromBase(v float64) float64 {
return v/u.factor - u.offset
}

// Dimension identifies the dimension a built-in unit belongs to. It is part
// of Unit's method set so a unit cannot be passed to a different dimension.
func (u BuiltinUnit[D]) Dimension() D {
return *new(D)
}

type scaleUnit[D Dimension] = BuiltinUnit[D]

const (
Expand Down
Loading