-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
create generic functions with no methods #8283
Comments
Having this feature would also go well with the new help system #8791: so the |
Here's another relevant thread: I'm not sure if this can address the doc issue, but for the issue in Sam's example code, this would do: module Persons
export Person, something, say_something
abstract Person
say_something(person::Person) = println(something(person))
end
using Persons
import Persons.something
type Boy <: Person end
type Girl <: Person end
something(::Boy) = "I'm a boy." # <== Persons.something generic function created now
something(::Girl) = "I'm a girl."
say_something(Boy())
say_something(Girl()) Not sure how good an idea that is, but the main point is that all we really need is a way of knowing that |
Just exporting But we should probably forget about all the complications of signature specification, which I laid out about, and just have declaration, doc + a helpful error message when called without an implemented method. |
I agree. I was mostly just trying to think of ways to avoid adding new keywords. |
a macro couldn't work? |
The syntax
might work. Might be possible even without the semicolon; that also gives a syntax error now. |
I thought of that too, but it's weird to have one place in the language where trailing |
What about function foo end ? |
Although the semicolon is not necessary (updated my comment), |
I'd be down with that. It keeps the fact that |
+1 |
+1, especially since indentation in ESS/Julia is kind of a mess right now and introducing yet another special case would be cumbersome, whereas |
Excellent, thanks! This probably impacts on how function documentation is made: @MichaelHatherly @one-more-minute |
Thanks Jeff!
It's likely to impact |
Currently, a generic function can only be created by making a method. However, it may be useful to declare generic functions without having to provide an implementation. For instance Common Lisp provides this functionality with its
defgeneric
macro. Examples where this could be useful:Base
: https://github.com/JuliaLang/julia/blob/master/base/graphics.jlBase
the implied meaning of generic functions is fairly set, and their signature is consistent. Examples aresize
,copy
,isequal
, etc. However, there is no place where the generic function is defined. Thus there is no place in the code to specify & document what its meaning is and what conventions/rules its call signature should follow.plot
function could be defined somewhere, inBase
or in some umbrella-plotting package (quite a few github topical groups have sprung up recently, probably they could profit from such definitions). See issue Common pool for methods as a way to solve common names in different packages #2327 or thread: https://groups.google.com/d/msg/julia-users/zyVU-tty-qk/lg1IW2qPSGEJThis issue touches on: documentation (e.g. #3988), function types (e.g. #1090), interface-oriented programming (e.g. #6975), and method ambiguity warnings (#6190 (comment)). In particular, @StefanKarpinski's comment on how to potentially specify (interfaces)[https://github.com//issues/6975#issuecomment-44502467] also has syntax to define generic functions within an interface specification.
Declaring a generic function should:
Implementations:
The most straight forward implementation would just create the generic function and allow documentation to be associated with it:
More complicated would be to allow to constrain the call signature. Inspired by Common Lisp, the syntax could look like:
The names of the arguments would be dummy-names useful for documentation and consistency in method implementations.
a
would be a required argument,b
would be optional, likewise for keyword arguments, and...
would allow any number of other optional arguments (the[]
and...
couldn't be allowed at the same time).Example
size
:Thus, the generic declarations could follow largely what is defined in the standard library documentation https://github.com/JuliaLang/julia/tree/master/doc/stdlib in a more formal way.
To think about:
The text was updated successfully, but these errors were encountered: