OvermindDL1/protocol_ex

Default arguments

jjl opened this issue · 3 comments

jjl commented

It would be nice to be able to define a function in a protocol that uses optional arguments with \\

I've thought of that but the issue is such way to consider things. First, should defaults just be allowed on the protocol? Or second, should I allow implementations to have their own default args, which may have some corner cases that I considered a while back but can't remember currently. Is the first sufficient?

jjl commented

I was considering only protocols having default args here. I imagine it would work like this:

defprotocolEx FooBar, %Foo{}, for: Bar do
  def bar(x, y \\ nil)
end

Would be the equivalent of this:

defprotocolEx FooBar, %Foo{}, for: Bar do
  def bar(x), do: bar(x,nil)
  def bar(x, y)
end

Added it in, and I allowed protocols to override the defaults as well if so wanted. :-)

I did not implement the second style (allowing implementations to have defaults without the protocol having it set up first, it breaks the interface I'd think).