Package term
:
type Num interface {
IsNeg() bool
Add(Num) Num
}
type Term struct {
Coeff Num
Var string
}
External package frac64
type Frac64 struct {
Numer uint64
Denom uint64
Neg bool
}
func (a Frac64) Add(b Frac64) Frac64 { // Pretend this is implemented }
Package client
type Frac Frac64
func (f Frac) IsNeg() bool { return f.Neg }
func (f Frac) Add(v Num) Num { // Call Frac64's Add here? }
How would I go about implementing Add
for Frac
so that it implements the Num
interface?
EDIT: Additional info
The external package frac64
was just an example. I do not intend to use it.
The goal here (and I should have been clearer) is for my package to expose the struct Term
that uses Num
as one of its two properties. Now, I want users of my package to be able to use big.Rat
or Frac64
or int64
or rune
or whatever they want, as long as they implement the Num
interface.
The problem I have is trying to implement the Num
interface for a struct that already has functions with the same name as the functions in Num
. This is where Frac64
comes in. I could've also used big.Rat
as an example, as it also has a function called Add
.
I can't change the implementation of Frac64
(or big.Rat
for that matter), and I also can't extend it with an Add
function as it already exists. That's why I tried to use type Frac Frac64
(or type Frac big.Rat
) and then trying to extend Frac
.
I fail to implement Num
for Frac
though, because I'm not able to call Frac64
's Add
from the Frac
's Add
function.