Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tuple: 增加 Pair 和 Triple 两种类型 #164

Closed
wants to merge 3 commits into from

Conversation

KirinRyuuri
Copy link
Contributor

主要参考 Kotlin
支持基本操作、ToString()、ToList()、Copy()
支持原始值为 nil 的修改( Kotlin 不支持原始值为 null 时候对值的修改 )

@flycash flycash linked an issue Mar 7, 2023 that may be closed by this pull request
Copy link
Contributor

@flycash flycash left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我其实这里有一个拿捏不定的主意的地方是,我觉得 Pair 和 Triple 可以考虑采用泛型来设计。比如说 Pair<F any, S any>。
我从个人使用角度出发来说,用泛型能够让我避免一些类型转换的问题。Triple 也是类似。
但是使用泛型的坏处就是写起来有点累赘,比如说:

Pair<int, string>

当然,如果我真就是随便放,那么我可以用Pair<any, any>
你觉得泛型设计是否有必要。
@longyue0521 可以一并看看

tuple/pair/pair.go Outdated Show resolved Hide resolved
tuple/triple/triple.go Outdated Show resolved Hide resolved
tuple/pair/example_test.go Show resolved Hide resolved
@flyhigher139
Copy link
Collaborator

我其实这里有一个拿捏不定的主意的地方是,我觉得 Pair 和 Triple 可以考虑采用泛型来设计。比如说 Pair<F any, S any>。 我从个人使用角度出发来说,用泛型能够让我避免一些类型转换的问题。Triple 也是类似。 但是使用泛型的坏处就是写起来有点累赘

我个人倾向于用泛型,不用泛型的化,感觉存在必要性不大,至少与直接用slice区别不大,用泛型,业务意义上可读性更好一点

@longyue0521
Copy link
Collaborator

如果真的要实现那么应该用泛型,但我对这两个类型以库而不是语言特性的形式来实现后其使用前景感到担忧——用户会在哪些场景用它们?使用频率是怎样的?。

Pair,Tuple是一种聚焦于元素间顺序的集合抽象,元素的类型可以相同也可以不同;在code review时看到这种类型,我都要回溯代码找到它们创建出来的地方才可能知道顺序First,Second,Third所代表的具体含义是什么!因为他们的可读性很差,往往被乱用!

作为语言特性,下面这些场景中有用:

func A( x, y, z int)(string, bool, error)
func B(a string, b bool, err error, c)
// 将A的返回值(Truple)直接作为B的全部或者部分输入来简化代码
// 虽然A的返回值(Truple)擦除了元素的具体含义,但是B的方法签名可以很好地补充这一点
B(A(), C)

// channel收发数据时
可以用匿名结构体chan struct{ string;bool}来达到同样的效果不一定要用pair和Tuple

但Go官方团队已明确拒绝了该提议 golang/go#32941

作为程序库,实现过程中要考虑封装性、不可变性,那么Pair和Tuple中的属性必须全小写——因为你不能指望用户自律。同时提供NewPair()、First()、Second()以及可能的CopyWithFirst(first F) Pair , CopyWithSecond(second S) Pair等方法,这样才能做到真正的不可改类型,再加上泛型就会觉得API难用,还不如直接在局部创建一个具有业务含义的结构体。

@flycash
Copy link
Contributor

flycash commented Mar 8, 2023

作为语言特性,下面这些场景中有用:

我觉得 Pair 在实际中还是说会用得上的。就是比如说我需要绑定两个元素,它们是成对存在的。目前的的代码写起来都是使用List<K> keys, List<V> vals或者map。在使用两个 list 的时候需要判断下标问题,使用 map 则需要判断 key 在不在的问题,而且 key 还不能重复。

那么Pair和Tuple中的属性必须全小写——因为你不能指望用户自律
我并不追求说把 Pair 或者 Tuple 做成不可变对象。

或者说,我更加觉得我就是需要一个 Pair 来作为数据载体,它上面什么方法都没有都可以。

@longyue0521
Copy link
Collaborator

目前的的代码写起来都是使用List<K> keys, List<V> vals或者map。在使用两个 list 的时候需要判断下标问题,使用 map 则需要判断 key 在不在的问题,而且 key 还不能重复。

就用这两个例子当Example来指导用户吧,告诉用户Pair/Tuple的恰当使用场景,先在注释里写一下没有pair这两个场景要怎么搞,再用代码写一下有了pair怎么搞。

Copy link
Contributor

@flycash flycash left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

按照你最开始的设计,尤其是 Copy 方法,以及 @longyue0521 的建议,我们倾向于把 Pair 和 Triple 做成不可变的。那么你需要修改:

  • 将字段都变成私有的,同时暴露 First(), Second() 这种访问方法;
  • 暴露 NewPair 这种方法,我们预期用户会使用这个方法来初始化 Pair 实例;

此外:

  • 补充单元测试,尤其是 Copy 的方法,要覆盖各个 if 分支
  • 解决文件冲突
  • 将example_test 文件名改为 pair_test,triple_test。你也可以考虑改为 pair_example_test,因为这样子 pair 和它对应的测试文件、例子文件就很接近,找起来很方便;

@KirinRyuuri
Copy link
Contributor Author

收到

最开始没用泛型是发现泛型用起来麻烦,今天试了下好像没有那么麻烦

Copy link
Contributor

@Stone-afk Stone-afk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mark

@flycash
Copy link
Contributor

flycash commented Mar 13, 2023

mark

mark 个屁呀,至少得给出点建设性的意见吧=。=

@flycash
Copy link
Contributor

flycash commented Mar 26, 2023

你要是还继续修改的话,就重新提一个合并请求。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Tuple: 新增 Pair 和 Triple 两种类型
5 participants