This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Don't repeat yourself, how to

2 replies
edmondo1984
Joined: 2011-09-14,
User offline. Last seen 28 weeks 4 days ago.
Dear all, 
I need to provide a certain number of methods which should available in two signatures:
def myMethod(template:T)
def myMethod(realItem:T, templateGenerator:(T)=>T)
Of course one can do the following
def myMethod(realItem:T, templateGenerator:(T)=>T)  = myMethod(templateGenerator(realItem));

Is there a best way to do it, with implicit conversions? (My parameter list is tipically much longer)

Thank you very muchBest Regards
Edmondo
Viktor Klang
Joined: 2008-12-17,
User offline. Last seen 1 year 27 weeks ago.
Re: Don't repeat yourself, how to


On Wed, Dec 14, 2011 at 12:14 PM, Edmondo Porcu <edmondo.porcu@gmail.com> wrote:
Dear all, 
I need to provide a certain number of methods which should available in two signatures:
def myMethod(template:T)
def myMethod(realItem:T, templateGenerator:(T)=>T)
Of course one can do the following
def myMethod(realItem:T, templateGenerator:(T)=>T)  = myMethod(templateGenerator(realItem));

Is there a best way to do it, with implicit conversions? (My parameter list is tipically much longer)

def myMethod[T](realItem:T, templateGenerator:T=>T = identity _)
 


Thank you very muchBest Regards
Edmondo



--
Viktor Klang

Akka Tech LeadTypesafe - Enterprise-Grade Scala from the Experts

Twitter: @viktorklang
Tony Morris 2
Joined: 2009-03-20,
User offline. Last seen 42 years 45 weeks ago.
Re: Don't repeat yourself, how to

On 14/12/11 21:14, Edmondo Porcu wrote:
> Dear all,
>
> I need to provide a certain number of methods which should available in two
> signatures:
>
> def myMethod(template:T)
>
> def myMethod(realItem:T, templateGenerator:(T)=>T)
>
> Of course one can do the following
>
> def myMethod(realItem:T, templateGenerator:(T)=>T) =
> myMethod(templateGenerator(realItem));
>
>
> Is there a best way to do it, with implicit conversions? (My parameter list
> is tipically much longer)
>
>
> Thank you very much
> Best Regards
>
> Edmondo
>

def myMethod(realItem: T, templateGenerator: T => T = t => t)

Don't overload methods, not ever.

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland