JuliaDiff / JuliaDiff/ChainRulesCore.jl

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

Open
#406 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Julia
Stars
267
Forks
66
PR merge metrics
No merged PRs in 30d

Description

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

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the CustomType MWE and read construct in src/differentials/composite.jl, especially the linked section. Then inspect the CRTU tests that construct the type through FiniteDifferences and compare that behavior with the custom-constructor handling described in the issue. Done means the relevant CRTU case no longer fails for this constructor pattern.

Written by the indexing model from the issue text.

Assessment

Tech stack
julia
Domain
backend-api-design
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.