dss087358 2019-02-21 15:05
浏览 45

没有方法的多态性

Note: I'm editing this question to a concrete example of why I want to do this, which is why some of the answers might no longer make sense in context.

I am writing a bit of code that passes data from an input. The data is in the form of tags that have an identifier of what kind of data they contain and then the data.

Unfortunately I have no control over the input and don't know in advance what tags will be in it, one might be an int another might be a string, yet another might be an array of ints.

The problem arises when I need to handle all tags like the same type, for instance if I have a slice of tags, of a function that either accepts or returns a tag.

The solutions I have so far seen to this is to define the slices/functions with an empty interface which would allow me to do so, however that is kinda undesirable as it would not tell anything to other people using the package about what types are expected and also kinda defies the point of having a typed language in the first place.

Interfaces does however seem to be the solution here, and i would love to have a Tag interface to pass around, that does require though that I define methods on them, and there are really no methods they need.

My current solution looks like this

type Tag interface{
    implementTag()
}

type TagInt int
func (tag TagInt) implementTag() {}

type TagString string
func (tag TagInt) implementTag() {}

While this does indeed work and solves my problem, having to define dummy methods just for that feels very wrong.

So my question sums up in this: Are there any way that I can define that something is a Tag without having to define dummy methods?

  • 写回答

3条回答 默认 最新

  • doudao9915 2019-02-21 15:07
    关注

    You can use []interface{} for a "slice of any type" but then it's up to you to use type assertions and/or type switches to discover the actual runtime types of that slice's members.

    Learn more about empty interfaces in the Tour of Go


    And now want to make a slice that can hold both t1 and t2 but nothing else.

    This is quite an unusual requirement and you're unlikely to need this in Go. But you could also do your own discriminated union with:

    type item struct {
      typeSelector int
      t1Value t1
      t2Value t2
    }
    

    And then use []item, checking typeSelector at runtime to see which value is populated.

    Alternatively you could even use *t1 and *t2 and have nil signify "no value in this field".

    评论

报告相同问题?

悬赏问题

  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源