可以操作对象属性的静态语言?
在处理异构数据时,经常出现的问题是需要部分更改保存数据的简单对象。例如,您可能想要添加、删除或重命名属性,或者连接两个对象。这在动态语言中很容易,但我想知道静态语言是否提出了任何聪明的解决方案?
为了解决想法,是否有任何语言可以通过某种静态 mixin 语法启用类似这样的东西(C#):
var hello = new { Hello = "Hello" };
var world = new { World = "World" };
var helloWorld = hello + world;
Console.WriteLine(helloWorld.ToString());
//outputs {Hello = Hello, World = World}
这似乎是可能的,因为不使用运行时信息。是否有具有此功能的静态语言?
添加:
我正在考虑的有限版本是 F# 的 复制和更新记录表达式:
let myRecord3 = { myRecord2 with Y = 100; Z = 2 }
Something that comes up a fair amount when dealing with heterogeneous data is the need to partially change simple objects that hold data. For instance, you might want to add, drop, or rename a property, or concatenate two objects. This is easy enough in dynamic languages, but I'm wondering if there are any clever solutions proposed by static languages?
To fix ideas, are there any languages that enable, perhaps through some sort of static mixin syntax, something like this (C#):
var hello = new { Hello = "Hello" };
var world = new { World = "World" };
var helloWorld = hello + world;
Console.WriteLine(helloWorld.ToString());
//outputs {Hello = Hello, World = World}
This certainly seems possible, since no runtime information is used. Are there static languages that have this capability?
Added:
A limited version of what I'm considering is F#'s copy-and-update record expression:
let myRecord3 = { myRecord2 with Y = 100; Z = 2 }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您所描述的内容在编程语言研究中被称为“记录串联”。已经有一些关于用于记录串联的静态类型系统的工作,主要是在 Haskell 或 ML 等自动类型推断的上下文中。据我所知,它尚未对任何主流编程语言产生影响。
What you're describing is known in programming language research as record concatenation. There has been some work on static type systems for record concatenation, mostly in the context of automatic type inference a la Haskell or ML. To the best of my knowledge it has not yet made an impact on any mainstream programming languages.