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

RFC: Add an interfaces manual chapter #11794

Merged
merged 5 commits into from
Jul 20, 2015
Merged

RFC: Add an interfaces manual chapter #11794

merged 5 commits into from
Jul 20, 2015

Conversation

mbauman
Copy link
Member

@mbauman mbauman commented Jun 21, 2015

Document the iterable, indexable, and abstract array interfaces.

I'm open to suggestions on the tables. I like the way they summarize the requirements, and I think the iterable and indexable ones work well, but the array table is nearly unreadable since it needs to scroll horizontally.

@mbauman
Copy link
Member Author

mbauman commented Jun 21, 2015

There's room at the bottom top for someone else to later add information about the Equality interface (==/isequal/hash), comparison interface (</isless) and Display interface (display/show/print). :-)

@tkelman tkelman added the docs This change adds or pertains to documentation label Jun 21, 2015
@tkelman
Copy link
Contributor

tkelman commented Jun 21, 2015

This is great! Should the code blocks be doctests here?

@mbauman
Copy link
Member Author

mbauman commented Jun 21, 2015

Ah, yes, thank you, they should.

Interfaces
************

A lot of the power and extensibility in Julia comes from a collection of informal interfaces. By extending few specific methods to work for a custom type, objects of that type not only receive those functionalities, but they are also able to be used in other methods that are written to generically build upon those behaviors.
Copy link
Contributor

Choose a reason for hiding this comment

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

By extending a few

@IainNZ
Copy link
Member

IainNZ commented Jun 21, 2015

Conceptually, what separates these from a "formal" interface? That the type isn't directly annotated with something that says "Iterable"?

:func:`getindex(X, i) <getindex>` ``X[i]``, indexed element access
:func:`setindex!(X, v, i) <setindex!>` ``X[i] = v``, indexed assignment
:func:`endof(X) <endof>` The last index, used in ``X[end]``
====================================== ==================================
Copy link
Member

Choose a reason for hiding this comment

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

What happens with colons, like Squares[:]? Should that go here?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, as I was writing it I briefly considered using that as a motivating segue into the AbstractArray section. Not sure why I didn't end up doing that… I guess I got distracted. S[:] now gets lowered to getindex(S, Colon()). So it's entirely up to the type to define the appropriate method. Definitely worth a mention.

@mbauman
Copy link
Member Author

mbauman commented Jun 21, 2015

Conceptually, what separates these from a "formal" interface? That the type isn't directly annotated with something that says "Iterable"?

Yes, I think so. See #6975. I guess it gets a little blurry for the AbstractArray case, since there behaviors are much more defined by it being an AbstractArray. That is, iterables are duck-typed. AbstractArrays have their own method signature.

mbauman added 3 commits June 21, 2015 00:57
Document the iterable, indexable, and abstract array interfaces.

[av skip]
* Use sequence instead of collection in some places
* Change eltype and length descriptions
* Indentation fixes
* Talk about how defining getindex manually is a game of whackamole without support from AbstractArray
* Link to Array Indexing section

[av skip]
@johnmyleswhite
Copy link
Member

This is really awesome. Well done, @mbauman.

:func:`similar(A, ::Type{S}, dims::NTuple{Int}) <similar>` ``Array(S, dims)`` Return a mutable array with the specified element type and dimensions
========================================================== ============================================ =======================================================================================

If a type is defined as a subtype of ``AbstractArray``, it inherits a very large set of complicated behaviors including iteration and multidimensional indexing built on top of single-element access.
Copy link
Member

Choose a reason for hiding this comment

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

"Complicated" doesn't make it very appealing. :-) Maybe you could make it more obvious that you benefit from "rich" automatic fallbacks by simply defining basic methods?

@nalimilan
Copy link
Member

Really cool! Don't you think the titles of the sections could be changed to Iterable, Indexable, and AbstractArrays?

mbauman added 2 commits June 21, 2015 13:25
* complicated -> rich
* fix linearindexing description coherence between phrases by changing the first part of the phrase
* add a paragraph about how linearindexing impacts which getindex method(s) must get defined
* Fix missing *what*
* each other

[av skip]
@nalimilan
Copy link
Member

Thanks, looks good!

@mbauman
Copy link
Member Author

mbauman commented Jun 21, 2015

Thanks @nalimilan! Updated (oh, you already found it as I was writing this!). I like the words iteration and indexing much better than iterable and indexable, but if others think the latter vocabulary is clearer I'll happily change it.

I've also added more cross-links to and from other sections of the manual and standard library. In doing so, I rediscovered http://docs.julialang.org/en/latest/manual/arrays/#implementation. That page and section are written more in a descriptive how-to-use-it fashion instead of the way this is more of a how-to-implement-it. I think they can both live together nicely, but perhaps some of the information could be consolidated. Thoughts?

Also, I'm very tempted to document unsafe_getindex.

And I really cannot wait for #11242 to get merged. A syntax is really needed to talk about these things simply and clearly.

@mbauman
Copy link
Member Author

mbauman commented Jul 20, 2015

I'll merge this now. It's documentation and there are clamors for hashing/equality, comparison, and display interfaces. I don't have time to add those myself in the short term, but maybe once there's an obvious place for them to live others will be able to add it.

mbauman added a commit that referenced this pull request Jul 20, 2015
RFC: Add an interfaces manual chapter
@mbauman mbauman merged commit 067d418 into master Jul 20, 2015
@mbauman mbauman deleted the mb/interfaces branch July 20, 2015 16:34
@mbauman
Copy link
Member Author

mbauman commented Jul 20, 2015

Shoot, sorry, should have squashed.

@ScottPJones
Copy link
Contributor

There is no requirement that an AbstractArray implement a hash function, is there?
The arrays I've implemented in the past frequently didn't use hashing at all (they used a B+tree or a patricia trie).

@mbauman
Copy link
Member Author

mbauman commented Jul 20, 2015

They get the default implementation for free. Should be added.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs This change adds or pertains to documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants