Skip to content

WIP add constructors for base array wrappers #36

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

Merged
merged 2 commits into from
Feb 10, 2021

Conversation

rafaqz
Copy link
Member

@rafaqz rafaqz commented Jan 2, 2021

Some constructorof methods for Base and LinearAlgebra array wrappers, as discussed in #34.

Probably we shouldn't add a constructor for Tridiagonal because of the undef field - seems it would break Accessors.jl/Flatten.jl etc?

I'm not sure what to do about it long-term, possibly a PR to base using nothing instead of undef for du2? How many other structs have undef fields?

Closes #34

There are probably a bunch of other base objects that need constuctorof methods. Feel free to list more for me to add to this PR.

@codecov-io
Copy link

codecov-io commented Jan 2, 2021

Codecov Report

Merging #36 (dce6a09) into master (60cfb38) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##            master       #36   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            1         2    +1     
  Lines           38        60   +22     
=========================================
+ Hits            38        60   +22     
Impacted Files Coverage Δ
src/ConstructionBase.jl 100.00% <ø> (ø)
src/nonstandard.jl 100.00% <100.00%> (ø)

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 60cfb38...dce6a09. Read the comment docs.

Copy link
Member

@jw3126 jw3126 left a comment

Choose a reason for hiding this comment

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

I think we can have this. It is a bit tough since it forces us to decide more precisely whether we want constructorof(obj)(public_fields...) or constructorof(public_and_private_fields...).

I am not sure, what's the right decision there, but I am okay with merging this and we will see later how it plays out.
Alternatively, we could also include a propertyvalues function, which by default returns all the fields, and specify

constructorof(obj)(propertyvalues(obj)...) == obj

@rafaqz
Copy link
Member Author

rafaqz commented Jan 4, 2021

I was thinking about a propertyvalues method as well. Then we could override it for Tridiagonal and the few other objects that have undef fields. I cant think of another way to support Tridiagonal.

@jw3126
Copy link
Member

jw3126 commented Jan 4, 2021

Ok lets go this path. How about a slight tweak. Instead of propertyvalues we could return a NamedTuple:

@generated function properties(obj)
    fnames = fieldnames(obj)
    fvals = map(fnames) do fname
        Expr(:call, :getproperty, :obj, QuoteNode(fname))
    end
    fvals = Expr(:tuple, fvals...)
    :(NamedTuple{$fnames}($fvals))
end

struct S;a;b;end
s = S(1,2)
properties(s)
(a = 1, b = 2)

You can still splat it into a function call, like it was a tuple.

S(properties(s)...)
S(1,2)

But it has nicer symmetry with setproperties, is easier to read etc. For me turning a struct into a NamedTuple is a task that comes up every now and then.

@rafaqz
Copy link
Member Author

rafaqz commented Jan 5, 2021

That looks good to me. Maybe we can call it propertiesof to match constructorof. Or getproperties. Idk maybe just properties is fine too.

@jw3126
Copy link
Member

jw3126 commented Jan 5, 2021

getproperties sounds fine.

@rafaqz
Copy link
Member Author

rafaqz commented Feb 7, 2021

This is updated to use the changes in the getproperties PR, and can be merged once that is rebased into master.

@rafaqz rafaqz marked this pull request as ready for review February 7, 2021 02:47
@rafaqz
Copy link
Member Author

rafaqz commented Feb 8, 2021

This is ready to rebase.

@rafaqz
Copy link
Member Author

rafaqz commented Feb 10, 2021

Made some changes to the tests if you want to hit merge if you're happy with them. I guess we should do a minor version bump after that.

@jw3126 jw3126 merged commit 97a0afb into JuliaObjects:master Feb 10, 2021
@rafaqz rafaqz deleted the add_constructors branch February 10, 2021 12:42
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.

Constructors for common base types?
3 participants