- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Re: Don't repeat yourself, how to
Wed, 2011-12-14, 12:20
Can you explain a little bit more? Why the _ at the end?
Thanks :)
2011/12/14 √iktor Ҡlang <viktor.klang@gmail.com>
Thanks :)
2011/12/14 √iktor Ҡlang <viktor.klang@gmail.com>
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
Wed, 2011-12-14, 12:51
#2
Re: Don't repeat yourself, how to
def myMethod(template:T)
def myMethod(realItem:T, templateGenerator:(T)=>T)
what exactly do you want to do? provide one method that can handle both cases? if so, just do what viktor suggested. use a default parameter that just returns the provided T. (a no-op function)
about the "_":
def x(y:Y):Z = {...}
turns into
(y:Y) => x(y)
if you write
x _
in human words: it turns a method into a function
-------- Original-Nachricht --------
> Datum: Wed, 14 Dec 2011 12:24:13 +0100
> Von: "√iktor Ҡlang"
> An: Edmondo Porcu
> CC: scala-user
> Betreff: Re: [scala-user] Don\'t repeat yourself, how to
> 2011/12/14 Edmondo Porcu
>
> > Can you explain a little bit more? Why the _ at the end?
>
>
> _ at the end "detaches" a method to form a function.
>
> Cheers,
> √
>
>
> >
> > Thanks :)
> >
> >
> >
> > 2011/12/14 √iktor Ҡlang
> >
> >>
> >>
> >> On Wed, Dec 14, 2011 at 12:14 PM, 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)
> >>>
> >>
> >> def myMethod[T](realItem:T, templateGenerator:T=>T = identity _)
> >>
> >>
> >>>
> >>>
> >>> Thank you very much
> >>> Best Regards
> >>>
> >>> Edmondo
> >>>
> >>
> >>
> >>
> >> --
> >> Viktor Klang
> >>
> >> Akka Tech Lead
> >> Typesafe - Enterprise-Grade Scala from the
> >> Experts
> >>
> >> Twitter: @viktorklang
> >>
> >>
> >
>
>
Wed, 2011-12-14, 13:11
#3
Re: Don't repeat yourself, how to
I am now trying to use the function, and I have the following error:
error: type mismatch;found : Nothing => Nothingrequired: T => TError occurred in an application involving default arguments. removeFromSpace(gigaSpace,template,beforeDelete=(items:Traversable[SecurityImpl])=>realTimeConnector.unsubscribe(items.toList),timeout=timeout);
My function signature is :
private def removeFromSpace[T <: Curve[T]](gigaSpace: GigaSpace, curve: T, templateGenerator: T => T=identity _, beforeDelete: Traversable[SecurityImpl] => Unit=(items)=>{}, timeout: Long = 1000L): List[SecurityImpl] =
What's going wrong there?
Thank you very much for your help
2011/12/14 Dennis Haupt <h-star@gmx.de>
error: type mismatch;found : Nothing => Nothingrequired: T => TError occurred in an application involving default arguments. removeFromSpace(gigaSpace,template,beforeDelete=(items:Traversable[SecurityImpl])=>realTimeConnector.unsubscribe(items.toList),timeout=timeout);
My function signature is :
private def removeFromSpace[T <: Curve[T]](gigaSpace: GigaSpace, curve: T, templateGenerator: T => T=identity _, beforeDelete: Traversable[SecurityImpl] => Unit=(items)=>{}, timeout: Long = 1000L): List[SecurityImpl] =
What's going wrong there?
Thank you very much for your help
2011/12/14 Dennis Haupt <h-star@gmx.de>
def myMethod(template:T)
def myMethod(realItem:T, templateGenerator:(T)=>T)
what exactly do you want to do? provide one method that can handle both cases? if so, just do what viktor suggested. use a default parameter that just returns the provided T. (a no-op function)
about the "_":
def x(y:Y):Z = {...}
turns into
(y:Y) => x(y)
if you write
x _
in human words: it turns a method into a function
-------- Original-Nachricht --------
> Datum: Wed, 14 Dec 2011 12:24:13 +0100
> Von: "√iktor Ҡlang" <viktor.klang@gmail.com>
> An: Edmondo Porcu <edmondo.porcu@gmail.com>
> CC: scala-user <scala-user@googlegroups.com>
> Betreff: Re: [scala-user] Don\'t repeat yourself, how to
> 2011/12/14 Edmondo Porcu <edmondo.porcu@gmail.com>
>
> > Can you explain a little bit more? Why the _ at the end?
>
>
> _ at the end "detaches" a method to form a function.
>
> Cheers,
> √
>
>
> >
> > Thanks :)
> >
> >
> >
> > 2011/12/14 √iktor Ҡlang <viktor.klang@gmail.com>
> >
> >>
> >>
> >> 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 much
> >>> Best Regards
> >>>
> >>> Edmondo
> >>>
> >>
> >>
> >>
> >> --
> >> Viktor Klang
> >>
> >> Akka Tech Lead
> >> Typesafe <http://www.typesafe.com/> - Enterprise-Grade Scala from the
> >> Experts
> >>
> >> Twitter: @viktorklang
> >>
> >>
> >
>
>
> --
> Viktor Klang
>
> Akka Tech Lead
> Typesafe <http://www.typesafe.com/> - Enterprise-Grade Scala from the
> Experts
>
> Twitter: @viktorklang
Wed, 2011-12-14, 13:41
#4
Re: Don't repeat yourself, how to
It seems that the type parameter of `identity` is not properly inferred. Use
templateGenerator: T => T = identity[T] _
instead.
2011/12/14 Edmondo Porcu <edmondo.porcu@gmail.com>
_ at the end "detaches" a method to form a function.
Cheers,
√
--
Viktor Klang
Akka Tech LeadTypesafe - Enterprise-Grade Scala from the Experts
Twitter: @viktorklang