smallnest / smallnest/seq

记录:短函数字面量提案 #21498 落地后可大幅简化本库调用点样板

Open
#46 0 comments 0 reactions 1 assignee View on GitHub

@smallnest is already working on this.

Since Jun 28, 2026.

docs priority: low
Dominant language
Go
Stars
66
Forks
1
PR merge metrics
No merged PRs in 30d

Description

背景

本库是回调密集型:几乎所有中间/终结操作(Map/Filter/FlatMap/Reduce/Fold/ForEach/Sort/...)都以函数为参数。当前 Go 的函数字面量必须显式写出参数与返回类型,即便它们已能从上下文推导,导致调用点重复且冗长:

seq.From([]int{1, 2, 3, 4}).
    Filter(func(n int) bool { return n%2 == 0 }).      // int, bool 已可从 Filter 推导
    Map(func(n int) int { return n * n }).             // int, int 同上
    Reduce(0, func(acc, n int) int { return acc + n }) // acc, n, int 同上

本文档记录上游提案 golang/go#21498(short function literals)—— 若实现,可大幅消除本库调用点的这类样板。

上游提案 #21498:short function literals

golang/go#21498 —— proposal: spec: short function literals(作者 Damien Neil / neild,2017-08 提出,OPEN,milestone Proposal,带 LanguageChangeReview 标签,截至 2026 年仍 pending,无最终接受形式)。

提案核心:引入一种轻量匿名函数字面量,当编译器能从上下文(被赋值/传入的函数类型)推导时,可省略参数与返回类型。提案原文对照(Scala/Rust 先例):

compute((x, y) => x + y)   // 参数类型省略
compute(|x, y| { x + y })  // 参数与返回类型省略
语法窄结论(尚未最终定稿)

经近 1000 条评论与 LanguageChangeReview,Griesemer 在 2025-06-04 的总结评论#21498#issuecomment-2941432763)收敛到一个「可工作的窄结论」,倾向箭头形式:

  • 参数段统一为 (params)(Go 最自然的写法);
  • 用箭头区分短字面量与普通函数字面量,形如 (params) => {statements}(params) => expr
  • 用户最核心诉求是不重复参数类型,其余(如表达式体、... 处理)为附加收益;
  • 具体前缀/中缀、=> vs ->、gofmt 风格等细节仍未最终敲定

⚠️ 状态澄清:部分媒体标题(如「Go is finally getting lambdas」)较乐观,但 #21498 至今未接受。HunCoding 的客观表述是:把「Go got lambdas」标题当作进行中的讨论,而非已发布特性(来源)。本 issue 按「pending 提案」对待。

对本库的简化

#21498 落地后,上述调用点可改写为(以箭头窄结论语法示意,最终语法以提案为准):

seq.From([]int{1, 2, 3, 4}).
    Filter((n) => n%2 == 0).
    Map((n) => n * n).
    Reduce(0, (acc, n) => acc + n)

收益量化(本库现状,go1.27rc1):

  • 库内 58 处 func(...) 类型参数的高阶操作签名 —— 这些操作的调用点是主要受益面;
  • 测试中 125 处冗长 func(x int) bool {...} 字面量参数,绝大多数可缩短为单表达式箭头;
  • docs/ 示例与 README 的链式片段可读性显著提升(本库卖点正是「左到右读数据流」,每步一个 func(...) {...} 是当前最大噪声)。

注意边界:#21498 是调用点语法糖,不改变本库 API 形状或类型系统能力。它不能解决 #44(Zip 实例化循环)或 #45(约束子类型)那种需要类型系统演进的限制 —— 那两个分别依赖 golang/go#80172 与 golang/go#65394。三者互补:#21498 消除调用样板,#65394 消除子类型样板,#80172 决定 Zip 能否成方法。

验收 / 跟踪

  • 现状已记录:本库回调密集,调用点冗长 func 字面量是主要可读性噪声
  • 与上游提案的对应关系已厘清:#21498 short function literals,语法窄结论为箭头形式但未最终定稿
  • 跟踪 golang/go#21498:若被接受并落地(Go 1.x),则本库可:
    • 将测试(125+ 处)与文档示例中的 func(x T) U { ... } 改写为短字面量;
    • 评估是否在 README/docs 推荐新写法作为惯用法;
    • 库 API 本身无需改动(纯调用点收益)。
  • 若 #21498 被拒绝,则本库调用风格定型为显式 func 字面量,本 issue 关闭

Type

docs / tracking · 优先级 low · 关联上游 golang/go#21498

相关

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.