douhengdao4499 2019-07-15 12:42
浏览 60
已采纳

Golang接口方法链接

I have an interface Cells with several methods

type Cells interface{
    Len() int
    //....
}

Concrete implementations of this are StrCells, IntCells, FloatCells and BoolCells, all of which have the above methods implemented.

E.g.:

type StrCells []string
func (sC StrCells) Len() int {return len(sC)}
//...

type IntCells []int
func (iC IntCells) Len() int {return len(iC)}
//...

//....

For two of the concrete types - IntCells and FloatCells - I want to implement specific functions only applicable to those types.

I create a new interface NumCells which embedds Cells

type NumCells interface{
    Cells
    Add(NumCells) interface{} // should return either IntCells or FloatCells 
}

Here is my implementation of Add() for IntCells:

func (iC IntCells) Add(nC NumCells) interface{} {
    if iC.Len() != nC.Len() {
        // do stuff
    }
    switch nC.(type) {
    case IntCells:
        res := make(IntCells, iC.Len())
        for i, v := range iC {
            res[i] = v + nC.(IntCells)[i]
        }
        return res
    case FloatCells:
        res := make(FloatCells, iC.Len())
        for i, v := range iC {
            res[i] = float64(v) + nC.(FloatCells)[i]
        }
        return res
    default:
        // to come
        return nil
    }

}

Here is my question / problem

The function works, however, I actually want the function to return NumCells (i.e. either IntCells or FloatCells) so I could do method chaining like this

a := columns.IntCells(1, 2, 4, 2)
b := columns.IntCells{2, 3, 5, 3}
c := columns.FloatCells{3.1, 2, 2.4, 3.2}
d := a.Add(b).Add(c)

This is not possible if Add() returns an interface{}. However, I am not able to make the function work otherwise.

  • 写回答

1条回答 默认 最新

  • douchao0358 2019-07-15 13:02
    关注

    It works if you define your NumCells interface this way:

    type NumCells interface{
        Cells
        Add(NumCells) NumCells // returns either IntCells or FloatCells
    }
    

    You then need both IntCells and FloatCells to implement Add and return one of those types.

    Here's a working playground, using method chaining and printing the result:

    https://play.golang.org/p/W7DzcB4A3NH

    As stated in the comments, when using interfaces one usually wants to make each type agnostic of the rest of the implementations and just use the interface without type switches.

    One way to avoid those type switches in the implementations of Add might be to add another method in the NumCells to return a particular position as a float64.

    type NumCells interface{
        Cells
        Add(NumCells) NumCells // returns either IntCells or FloatCells
        GetCell(index int) float64
    }
    

    So that then you can get the value without needing to assert the particular type.

    Since IntCells cannot accommodate float64 values, it'd still need to create a FloatCells to return it, if we want to avoid IntCells doing that we'd need to abstract the creation of the objects somehow, using a factory pattern or similar.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配