In the below code, we are using type definition syntax in Line 5,6,7:
package math
// Constructors and Selectors
type RationalNumber []int // Line 5
type Numerator int
type Denominator int
// Constructor - Construct a rational number x that represents n/d
func NewRationalNumber(n int, d int) RationalNumber {
g := gcd(n, d)
return []int{n / g, d / g} // Line 12
}
//Selector
func numer(x RationalNumber) Numerator {
return x[0] // Line 17
}
//Selector
func denom(x RationalNumber) Denominator {
return x[1]
}
return []int{n / g, d / g} does not give error,
where as,
return x[0] & return x[1] give error:
math/numbers.go:17:10: cannot use x[0] (type int) as type Numerator in return argument
math/numbers.go:22:10: cannot use x[1] (type int) as type Denominator in return argument
For given 3 type definitions in Line 5,6,7, I understand the reason behind error on Line 17, but,
Why Line 12 does not give similar error?
How to resolve this error? without changing the signature of functions?