我有一个单链表类型,如下所示:
struct point { int x, y; };
struct point_list {
struct point value;
const struct point_list *next;
};
Run Code Online (Sandbox Code Playgroud)
我想静态初始化这些列表之一,可能有几十个条目。我想用一致的缩进每行写一个项目,以便于编辑列表。
到目前为止,我想出的最好的是:
const struct point_list *const my_list =
&(const struct point_list) { .value = { 1, 2 }, .next =
&(const struct point_list) { .value = { 3, 4 }, .next =
&(const struct point_list) { .value = { 5, 6 }, .next =
NULL
}}};
Run Code Online (Sandbox Code Playgroud)
但缺点是:
有没有更好的办法?
如果我们有递归宏,也许这样的事情可以工作:
const struct point_list *const my_list = POINT_LIST(
((struct point) { …
Run Code Online (Sandbox Code Playgroud) 在 Haskell 中,有没有办法测试两个 IORef 是否相同?我正在寻找这样的东西:
IORef a -> IORef a -> IO Bool
Run Code Online (Sandbox Code Playgroud)
例如,如果您想可视化由 IORef 组成的图形,这将非常有用。我不认为它会破坏引用透明度,因为 IORefs 有一个有意义的身份(结果可能是在 IO 中,无论如何)。而且我认为作为指针比较,有效地实现这一点并不难。
这在某处可用吗?或者如果没有,为什么不呢?
(编辑:我刚刚从不同的 SO question 中找到System.Mem.StableName,这看起来很有帮助。)