To get an empty list of polynomials, you can type `Poly[]`. I wouldn't 
recommend changing the behavior of the built in reduce function though, 
that would definitely be confusing for anyone else who later wants to work 
with your module (and who will reasonably expect Base.reduce to return a 
list). I would just handle the transformation of the empty list into 
ZeroPoly elsewhere in your code. A nice thing to do might be to define your 
own reduce function in your module (so Polynomials.reduce, or 
Polynomials.polyreduce or something as opposed to Base.reduce) that wraps 
Base.reduce and provides this behavior.


On Tuesday, November 4, 2014 5:20:53 PM UTC-6, Evan Pu wrote:
>
> Hi I'm writing a simple polynomial module which requires addition of 
> polynomials.
>
> I have defined the addition by overloading the function + with an 
> additional method:
> +(p1::Poly, p2::Poly) = ...# code for the addition
>
> I would like to use + now in a reduce call, imagine I have a list of 
> polynomials [p1, p2, p3],
> calling reduce(+, [p1, p2, p3]) behaves as expected, giving me a 
> polynomial that's the sum of the 3
>
> however, I would also like to cover the edge cases where there's only a 
> single polynomial or no polynomial.
> I would like the following behaviours, :
>
> # reducing with a single polynomial list gives just the polynomial back
> reduce(+, [p1]) 
> > p1
>
> # reducing with an empty list gives back the 0 polynomial
> reduce(+, [])
> > ZeroPoly
>
> How might I add such functionality?
> For the empty list case, how might I annotate the type so Julia is aware 
> that [] means an empty list of polynomials rather than an empty list of Any?
>

Reply via email to