Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add remake() for reconstructing immutable structs #87

Merged
merged 6 commits into from
Feb 17, 2018

Conversation

tkf
Copy link
Contributor

@tkf tkf commented Feb 17, 2018



"""
@add_kwonly function_definition
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like this could even get its own package. That's quite a handy macro.

Re-construct `thing` with new field values specified by the keyword
arguments.
"""
function remake(thing; kwargs...)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this infer well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops. I just comment it on #87 (review)

@test prob1.tspan == prob2.tspan
@test prob1.jac_prototype === prob2.jac_prototype
@test prob1.callback === prob2.callback
@test prob1.mass_matrix === prob2.mass_matrix
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably want to add some inference tests. Some might fail, in which case just @test_broken them so we can keep track of it for v0.7.

T
else
# Assume that T wants isinplace
T{isinplace(thing)}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like that it assumes the constructor has the iip type parameter but it should be fine as long as remake is used for types from DiffEqBase. Note that I can't dispatch on DEProblem to avoid this since using it means that remake won't work with SplitFunction etc.

@ChrisRackauckas
Copy link
Member

That's a very clean solution. You should consider making Kwonly.jl for that macro because it's quite nice and I could see it being used elsewhere.

@coveralls
Copy link

coveralls commented Feb 17, 2018

Coverage Status

Coverage increased (+2.5%) to 21.912% when pulling 6448f4d on tkf:remake into 4433523 on JuliaDiffEq:master.

@codecov
Copy link

codecov bot commented Feb 17, 2018

Codecov Report

Merging #87 into master will increase coverage by 2.51%.
The diff coverage is 70%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #87      +/-   ##
==========================================
+ Coverage    19.4%   21.91%   +2.51%     
==========================================
  Files          34       34              
  Lines        1134     1182      +48     
==========================================
+ Hits          220      259      +39     
- Misses        914      923       +9
Impacted Files Coverage Δ
src/problems/dae_problems.jl 100% <ø> (ø) ⬆️
src/DiffEqBase.jl 100% <ø> (ø) ⬆️
src/problems/dde_problems.jl 100% <ø> (ø) ⬆️
src/problems/sde_problems.jl 54.54% <ø> (ø) ⬆️
src/problems/rode_problems.jl 100% <ø> (ø) ⬆️
src/problems/discrete_problems.jl 33.33% <ø> (ø) ⬆️
src/problems/steady_state_problems.jl 75% <ø> (ø) ⬆️
src/problems/monte_problems.jl 0% <ø> (ø) ⬆️
src/problems/analytical_problems.jl 0% <ø> (ø) ⬆️
src/problems/noise_problems.jl 0% <0%> (ø) ⬆️
... and 4 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 4433523...6448f4d. Read the comment docs.

@tkf
Copy link
Contributor Author

tkf commented Feb 17, 2018

Thanks :) Well, it needs a bit of help from remake to "inject" isinplace type parameter so it's not quite a general solution (yet?). But I agree that making a package that does this makes sense. Until then, it can live here, I suppose.

@ChrisRackauckas
Copy link
Member

Sounds good. Then just some inference checks and it'll be done. If inference is really bad then it may need to be an @generated function?

@tkf
Copy link
Contributor Author

tkf commented Feb 17, 2018

BTW, I think current set_u0 is not working. Do you want to remove it in this PR or do you want it to be the alias of remake(prob; u0=u0)?

@ChrisRackauckas
Copy link
Member

BTW, I think current set_u0 is not working.

Yes, remove it. It's undocumented and so it's fine. Make sure we ping whoever added it. I think it was @BeastyBlacksmith ? Check blame.

@tkf
Copy link
Contributor Author

tkf commented Feb 17, 2018

It was @Datseris.

@Datseris We are removing set_u0 Use remake after this PR is merged.

@Datseris
Copy link
Contributor

thanks for letting me know. Is there any documentation string I can read? I am super stressed at the moment to read all git diff.

@tkf
Copy link
Contributor Author

tkf commented Feb 17, 2018

set_u0(prob, u) is just remake(prob; u0=u) after this PR.

@Datseris
Copy link
Contributor

okay great

@tkf tkf mentioned this pull request Feb 17, 2018
@tkf
Copy link
Contributor Author

tkf commented Feb 17, 2018

@ChrisRackauckas So I wrote the generated version (see #89) but @inferred raises an error. It looks like the constructor itself cannot be @inferreded prior to this PR (see #88). Am I using @inferred correctly?

@tkf
Copy link
Contributor Author

tkf commented Feb 17, 2018

It looks like this is it: JuliaLang/julia#25918

Example:

type Spam{T}
    Spam(x::T) where T = new{T}()
    Spam(; x::T = nothing) where T = new{T}()
end

using Base.Test

macro test_nothrow(ex)
    quote
        @test begin
            $(esc(ex))
            true
        end
    end
end

@testset "Spam" begin
    @test_nothrow @inferred Spam(nothing)
    @test_nothrow @inferred Spam()  # it fails
    # @test_nothrow @inferred Spam(x="x")
end

@ChrisRackauckas
Copy link
Member

ChrisRackauckas commented Feb 17, 2018

Ugh, keyword arguments on v0.6.2 don't even infer the default usage? They used to. I moved the last args to keyword arguments and it worked... okay then whatever. Mark as @test_broken and we'll see what happens with v0.7.

@ChrisRackauckas
Copy link
Member

Alright, then let's do this for now and leave fixing the inference for v0.7 when kwargs are fixed.

@ChrisRackauckas ChrisRackauckas merged commit ffcf747 into SciML:master Feb 17, 2018
@tkf
Copy link
Contributor Author

tkf commented Feb 18, 2018

Thank you for the merge!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Problem changing functions
4 participants