泛化后的加等于
Generalised plusequals

原始链接: https://leontrolski.github.io/alt.html

这个语言概念旨在为深度嵌套的不可变数据结构提供简洁、非修改的更新。核心思想围绕着 `alt` 关键字,它推广了运算符赋值。与其写 `x += 1`,不如写 `alt x + 1`。 真正的力量体现在自定义中缀运算符,如 `.=` 和 `]=`,它们能够优雅地更新对象属性和列表元素,*而不会*修改原始数据。例如,`alt l[1][1].age.=9` 更新嵌套猫的年龄,而不会发生变异。 这避免了为等效的不可变操作所需的冗长临时变量赋值。该语言还引入了使用波浪号 (`~`) 的中缀函数应用,例如 `alt l~push~5` 用于将元素添加到列表末尾。 作者设想一种在外观上是不可变的语言,但在逻辑上等效时,利用编译时的可变结构来提高性能,其灵感来自 Swift 和 Rust 的内存管理方法。

Hacker News 新闻 | 过去 | 评论 | 提问 | 展示 | 招聘 | 提交 登录 广义的等于号 (leontrolski.github.io) 6 分,由 leontrolski 1小时前发布 | 隐藏 | 过去 | 收藏 | 1 条评论 帮助 flebron 13分钟前 [–] 网站询问他们用 Haskell 做什么。答案是属性修改和读取,以及非常强大的遍历结构,使用 lenses (https://hackage.haskell.org/package/lens , 教程在 https://hackage.haskell.org/package/lens-tutorial-1.0.5/docs...)。回复 考虑申请 YC 2026 夏季班!申请截止至 5 月 4 日 指南 | 常见问题 | 列表 | API | 安全 | 法律 | 申请 YC | 联系 搜索:
相关文章

原文
leontrolski - Generalised plusequals

2026-04-24

I have a quarter baked language I've been working on. It's mostly crap, but a syntax idea fell out that I think is pretty neat.

The following are equivalent in many languages:

x = x + 1
x += 1

Reassigning is cool as it's lexically scoped so easy to reason about (mutation is bad as it isn't).

The new idea is that instead of special symbols (in this case +=) we generalise with a keyword (alt) that affects all infix operators, so the following are equivalent:

x = x + 1
alt x + 1

Who cares? It's not even shorter right?

The fun starts when we introduce a couple of new infix operators - namely ]= and .=

Let's compare them to the Python equivalents:

l = l[4]=999
l = l[:4] + [999] + l[5:]

and:

x = x.n.=2
x = dataclasses.replace(x, n=2)

Now let's set up a couple of examples of nested data:

cat = Cat(age=3)
l = [1, [2, cat], 4]

If we want to reassign to make an older cat, we can do:

alt cat.age.=8

The more interesting example is reassigning the deeply nested l to make the cat inside older, without mutating the original cat:

alt l[1][1].age.=9

this will leave us with l equal to:

[1, [2, Cat(age=9)], 4]

What was the alt statement sugar for, such that we didn't mutate the original cat? Well, the rather ungainly:

_1 = l[1]       # _1 = [2, Cat(age=3)]
_2 = l[1][1]    # _2 = Cat(age=3)
_2 = _2.age.=9  # _2 = Cat(age=9)
_1 = _1[1]=_2   # _1 = [2, Cat(age=9)]
l  = l[1]=_1    # l  = [1, [2, Cat(age=9)], 4]

My new language has a nice(?) feature - you can infix plain binary functions with tildes - this means you can do fun stuff like:

alt l~push~5

to leave l equal to:

[1, [2, Cat(age=9)], 4, 5]
  • I don't like mutations, I do like being able to succinctly update deeply nested data. This syntax lets you achieve cuteness in a language with only immutable datastructures.
  • What do they do in Haskell? Are there any other equivalent syntaxes lying around?
  • The rough plan for my language was - pretend all the datastructures are immutable, but at compile time, if the structure of the program is such that using a mutable datastructure would be equivalent, use one for performance. (This is kind of the reverse of mutable value semantics in Swift(?), and would share some implementation details with the borrow checker in Rust).
  • In some ways, this is the reverse of some older thoughts.
  • In my langauge, I also added a generalised version of Rust's ? operator.
联系我们 contact @ memedata.com