Handling structs without the default constructor via `Expr(:new,...` ?

未关闭
#406 3 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
45/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
停滞
技术栈
julia

调研方向

从 CustomType MWE 开始,阅读 src/differentials/composite.jl 中的 read 构造,尤其是链接的部分。然后检查通过 FiniteDifferences 构造该类型的 CRTU 测试,并将其行为与 issue 中所描述的自定义构造函数处理进行比较。当相关的 CRTU 用例不再因这种构造函数模式而失败时,即表示完成。

由索引模型根据 Issue 内容生成。

描述

In FiniteDifferences, this PR allowed custom constructors to be handled as part of to_vec: https://github.com/JuliaDiff/FiniteDifferences.jl/pull/181 here

However iiuc construct in CRC here doesn't handle this. In this case however, the constructor requirements are mentioned in the docstring.

However, tests in CRTU will construct the new type as part of the call to FiniteDifferences but then fail later as the constructor is not found in the call to construct.

Allowing this here, or otherwise, it would be easier if these behaviours were consistent in terms of custom types as part of the CTRU testing pipeline.

EDIT: MWE:

struct CustomType{T}
           x
           function CustomType(X::T) where {T}
               new{T}(X)
           end
end

# This misses the constructor:
CustomType{Int64}(5) # ERROR: MethodError: no method matching CustomType{Int64}(::Int64)

val = CustomType(5) # CustomType{Int64}(5)
diff = Tangent{typeof(val)}(x = 5) # Tangent{CustomType{Int64}}(x = 5,)

val + diff #ERROR: MethodError: no method matching CustomType{Int64}(::Int64)

# FiniteDifferences handles this in the vec to x:

to_vec(CustomType(5))[2]([6.0]) #CustomType{Int64}(6.0)

Defining CustomType{T}(x) where {T} = CustomType(x) allows this pass.

cc @mzgubic

主要语言
Julia
星标
267
派生
66
平均合并
1 天 1 小时
30 天内合并 PR
9

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

JuliaDiff/ChainRulesCore.jl 的其他 Issue

查看 JuliaDiff/ChainRulesCore.jl 的全部 Issue

相似的 Issue

更多 Julia Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。