Tcl 和记录(结构)
TCLLIB 中的 struct::record 包提供了模拟记录类型的方法。但记录实例是当前命名空间中的命令,而不是当前作用域中的变量。这意味着记录实例没有垃圾收集。将记录实例的名称传递给过程意味着通过引用而不是通过值传递它,可以将记录的字符串表示形式作为参数传递,但它需要在过程中创建另一个实例,配置它并手动删除,这很烦人。我想知道这个设计背后的基本原理。一个简单的替代方案是提供 Lisp 风格的记录 - 一组构造、访问和修改过程并将记录表示为列表。
Package struct::record from TCLLIB provides means for emulating record types. But record instances are commands in the current namespace and not variables in the current scope. This means there is no garbage collection for record instances. Passing name of the record instance to a procedure means passing it by reference not by value, it is possible to pass string representation of the record as parameter but it requires to create another instance in the procedure, configure it and delete by hand, it's annoying. I wonder about the rationale behind this design. A simple alternative is provide a lisp-style records - a set of construction, access and modification procedures and represent records as lists.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在我看来,struct::record 实现是一个 oo 风格的实现。如果您正在寻找数据风格的实现(例如 lisp),其中命令与数据完全分离,您可能需要查看 dict 命令。
我会注意到,oo 风格和数据风格确实不是很好的描述,但它们是我能立即想到的最好的描述。
The struct::record implementation is, from my viewpoint, an oo-style implementation. If you're searching for a data-style implementation (like lisp) where the commands are totally separate from the data, you might want to look at the dict command.
I'll note that oo-style and data-style are really not good descriptions, but they were the best I could think of offhand.
你肯定可以“用 Lisp 方式”做到这一点。
这效果很好。 (用C编写,也可以使其更高效。)请注意,作为通用数据结构,似乎很多人更喜欢Tcl 8.5的字典。使用它们的方法有很多;这里有一个:
对于整个结构与对象的争论,Tcl 倾向于将对象视为带有操作的状态(导致自然表示为命令,一个相当重量级的概念),而结构是纯值(因此是轻量级的)。在写了相当多的文章之后,我真的不知道一般来说什么是最好的;我根据具体情况进行工作。如果您要使用“结构”,还请考虑是否应该拥有表示跨多个结构的字段的集合(相当于在数据库中使用按列存储而不是按行存储),因为这可以在某些情况下实现更有效的处理案例。
还可以考虑使用数据库; SQLite 与 Tcl 集成得非常好,相当高效,并且如果您不想处理磁盘文件,还支持内存数据库。
You most certainly can do it “the Lisp way”.
That works quite well. (Write it in C and you can make it more efficient too.) Mind you, as a generic data structure, it seems that many people prefer Tcl 8.5's dictionaries. There are many ways to use them; here's one:
As for the whole structures versus objects debate, Tcl tends to regard objects as state with operations (leading to a natural presentation as a command, a fairly heavyweight concept) whereas structures are pure values (and so lightweight). Having written a fair chunk on this, I really don't know what's best in general; I work on a case-by-case basis. If you are going with “structures”, also consider whether you should have collections that represent fields across many structures (equivalent to using column-wise storage instead of row-wise storage in a database) as that can lead to more efficient handling in some cases.
Also consider using a database; SQLite integrates extremely well with Tcl, is reasonably efficient, and supports in-memory databases if you don't want to futz around with disk files.
我不会回答你的问题,因为我已经很多年没有使用 Tcl 了,也从来没有使用过这种结构,但我可以给你两个可能的地方的路径,这两个地方很可能为你提供一个很好的答案
当时我使用 Tcl 他们事实证明这是无价的资源。
I will not answer your question, because I was not been using Tcl for many years and I never use this kind of struct, but I can give you the path to two possible places that are very plausible to provide a good answer for you:
At the time I used Tcl they proved to be invaluable resources.