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 an is_resizable trait? #22

Open
rofinn opened this issue Aug 1, 2019 · 15 comments
Open

Add an is_resizable trait? #22

rofinn opened this issue Aug 1, 2019 · 15 comments

Comments

@rofinn
Copy link

rofinn commented Aug 1, 2019

It'd be nice if there was an abstract array interface specifying whether an array is resizable in-place.

@ChrisRackauckas
Copy link
Member

Agreed.

@oxinabox
Copy link
Member

oxinabox commented Aug 4, 2019

Is it worth distighishing this into:
isgrowable for things that can increase size (like Vector)
and
isresizable that can just shuffle the size around into different dimensions?

Or maybe it is a trait with multiple values:

  • Growable() > Resizable > FixedSize ?

@oxinabox
Copy link
Member

oxinabox commented Aug 4, 2019

Really i feel like we want to answer questions about:

  • hasmethod(resize!, ...)
  • hasmethod(push!, ...) (and maybe its flavours pushfirst!)
  • hasmethod(deleteat, ...) (and maybe its flavors pop!, popfirst, filter!)
  • maybe hasmethod(reshape,...) ?

@ChrisRackauckas
Copy link
Member

If hasmethod is a compile-time "trait", then do we still need a trait for this?

@oxinabox
Copy link
Member

oxinabox commented Aug 4, 2019

Yes, to group them. And to name them. But anyway Jameson says my plan is impossible.

@Tokazama
Copy link
Member

I created something similar for this here https://github.com/Tokazama/StaticRanges.jl/blob/f8dac65496c10ea0ec0fbf748006af105f77f62e/src/staticness.jl#L1.
It separates things into dynamic, static, fixed. Where a Vector is dynamic, a typical range is fixed, and SVector is static. Would this be a good solution to this?

@ChrisRackauckas
Copy link
Member

an isresizable trait function is all that's needed.

@tkf
Copy link
Member

tkf commented Jan 7, 2020

FYI, BangBang internally now has implements(f!, x) and possible(f!, args...) to query the capability of containers/objects. ATM only implements(setindex!, x), implements(push!, x), and implements(setproperty!, x) are defined. They are like hasmethod but manually implemented (like @oxinabox mentioned #22 (comment)). I also need to consider "element" types so that's why there is possible function that can do:

julia> possible(push!, Int[], 1)
true

julia> possible(push!, Int[], 0.5)
false

At some point, I'd like to pull this out of BangBang.jl as a separate library but I also need to deal with non-array containers and objects. So, I don't see ArrayInterface.jl as a good target. It would be great if the part of ArrayInterface.jl that is not array-specific can be factored out.

@Tokazama
Copy link
Member

Tokazama commented Jan 7, 2020

Is this compile time thing or is it all checked at run time?

Sorry if you document it elsewhere but I only read what you linked.

@tkf
Copy link
Member

tkf commented Jan 7, 2020

It's all type-level so everything should be done at compile time.

@Tokazama
Copy link
Member

Tokazama commented Jan 7, 2020

If I understand your code correctly you're using a union internally to group mutable and immutable containers. Wouldn't we want to use traits to avoid having to redefine the union type for every new type that needs this behavior?

@tkf
Copy link
Member

tkf commented Jan 7, 2020

The Union is just an implementation detail to minimize code duplication. implements is just yet another trait system. You can add anything afterwards. That's how static arrays are treated.

@Tokazama
Copy link
Member

Tokazama commented Jan 7, 2020

Okay, maybe I misunderstood the suggestion. Is your suggestion that instead of defining is_resizeable
to have define something like:

implements(::typeof(resize!), ::Type{<:Array}) = true

@tkf
Copy link
Member

tkf commented Jan 8, 2020

I'm just mentioning that I explored the API @oxinabox mentioned above #22 (comment). (I'm also suggesting to create a new package (upstream to ArrayInterface.jl) that can handle arbitrary containers and objects, not just arrays. But that's not the main topic here.)

ChrisRackauckas added a commit that referenced this issue Aug 12, 2020
New trait for collections that change sizes (issue #22)
@Tokazama
Copy link
Member

Tokazama commented Dec 3, 2020

Perhaps I should open a separate issue for this, but it would also be useful to have grow_first, grow_last, shrink_first, shrink_last. However, I don't know any of the magic behind reserving chunks of memory that this would require for arrays.

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

No branches or pull requests

5 participants