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 manual section on method design and more advanced ambiguity resolution #18111

Merged
merged 1 commit into from
Feb 20, 2017

Conversation

timholy
Copy link
Member

@timholy timholy commented Aug 18, 2016

I think many people struggle with how to resolve ambiguities, and this really gets into how one should design Method hierarchies in Julia. For some time I've thought we could use a bit more documentation on this topic; here's an attempt to start that conversation.

There are many caveats to this:

  • it's surely incomplete
  • it ventures opinions that may or may not be defensible
  • it may not be in the right place (it may make use of some concepts not yet introduced)
  • it may not strike the right balance (it has a feel somewhere between "high level design patterns" and something more along the lines of the cookbook approach of "performance tips")

In other words, it's just about perfect for generating discussion on the topic 😉.

@musm
Copy link
Contributor

musm commented Aug 18, 2016

Thanks.

What's your opinion on including some of the following points or thoughts.
From steveng

my understanding is that if you write a function f(T::Type), then T is just a value that is determined at runtime (the same version of f is compiled for all values of T), whereas if you write f{T}(::Type{T}) then T is part of the type signature of the function — hence it is known at compile time and a specialized version of f is compiled for every T.

Ref 17794

This statement only apply to types if my understanding is correct? In other words, there is no difference between the following two functions
fun1{T<:Number}(x::T) = x
fun2(x::Number) = x
both will specialize upon calling the function on different subtypes of Number


- avoid defining either of these methods: instead, rely on a generic method ``-{T}(A::AbstractArray{T}, b)`` and make sure this method is implemented with generic calls (like ``similar`` and ``-``) that do the right thing for each container type and element type *separately*.

- if at least one of these must be defined, prioritize dispatch on the container
Copy link
Member

Choose a reason for hiding this comment

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

I don't understand what you mean by this point.

Copy link
Member Author

Choose a reason for hiding this comment

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

That is to say, where possible don't dispatch on T, but it's OK (often necessary) to dispatch on MyArray.

Copy link
Contributor

Choose a reason for hiding this comment

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

Note quite clear to me what you mean, maybe add example?

@kshyatt kshyatt added the docs This change adds or pertains to documentation label Aug 18, 2016
@timholy
Copy link
Member Author

timholy commented Aug 18, 2016

@musm, that's right. The limitation regarding types (T::Type vs ::Type{T}) may be more about where Julia is now than anything truly fundamental; in other words, I can easily see it getting to the point where it doesn't matter for types, either.


This is often the right strategy; however, there are circumstances
where following this advice blindly can be counterproductive. In
particular, the more methods you have, the more possibilities there
Copy link
Contributor

Choose a reason for hiding this comment

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

Just to be on the save side, I'd write "...,the more methods a generic function has,..."

@mauro3
Copy link
Contributor

mauro3 commented Aug 19, 2016

Good addition, thanks for writing down your insights.

@Sacha0
Copy link
Member

Sacha0 commented Feb 18, 2017

The material in this pull request is insightful. Any hope of reviving it? Thanks!

@timholy timholy force-pushed the teh/docs_methods_ambiguities branch from 6cc9ad0 to 06a528c Compare February 20, 2017 11:06
@timholy
Copy link
Member Author

timholy commented Feb 20, 2017

Comments addressed and rebased.

@timholy timholy changed the title RFC: add manual section on method design and more advanced ambiguity resolution Add manual section on method design and more advanced ambiguity resolution Feb 20, 2017
@timholy timholy merged commit 75cfcb9 into master Feb 20, 2017
@timholy timholy deleted the teh/docs_methods_ambiguities branch February 20, 2017 23:34
@tkelman
Copy link
Contributor

tkelman commented Feb 20, 2017

the manual isn't in rst any more, bad rebase adding that file

@timholy
Copy link
Member Author

timholy commented Feb 20, 2017

Indeed. See #20702.

myfilter(A, kernel) = myfilter(A, kernel, Replicate()) # default boundary conditions

function myfilter(A, kernel, ::Replicate)
Apadded = pad(A, kernel)
Copy link
Contributor

Choose a reason for hiding this comment

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

would it make more sense for this to be the same padding function replicate_edges(A, size(kernel)) as above?

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.

7 participants