You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无法访问defrecord对象内部字段:获取:coordinates返回nil求助

问题排查与解决

核心原因

你看到的输出[#grid_generator.core.Cell{:coordinates [1.0 1.0], :value 3.0}]是一个Clojure向量,而非单个Cell对象。也就是说sample-cell变量指向的是这个向量,不是向量内部的Cell实例,直接用(:coordinates sample-cell)自然会返回nil——因为向量本身没有:coordinates这个键。

解决步骤

  1. 验证变量类型:执行(type sample-cell),结果应该是clojure.lang.PersistentVector,确认它是向量。
  2. 取出向量内的Cell实例:
    (def real-cell (first sample-cell))
    
    或者直接访问:
    (:coordinates (first sample-cell))
    
  3. 现在访问字段就能得到正确结果了:
    (:coordinates real-cell) ; 返回 [1.0 1.0]
    (get-in real-cell [:coordinates]) ; 同样有效
    

额外提示

如果后续你期望sample-cell直接是单个Cell对象,需要检查生成它的代码,确认是否误将单个实例包装进了向量里。

内容的提问来源于stack exchange,提问作者Braden Christopher

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 13:02:02