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

abstract method param question

3 replies
scalanewb
Joined: 2010-01-17,
User offline. Last seen 42 years 45 weeks ago.

Hi.

I have a helper method:

def handleEntity[T <: Entity](exceptions: Class[_]*)(body: => T) = {
try {
val entity = body
entity.callSomeMethod
Some(entity)
} catch {
case e if (exceptions.contains(e.getClass)) => None
}
}

called with:

handleEntity[MyEntity](classOf[Exception]) { loadEntitySomehow }

the main point of which is to call the 'callSomeMethod' on the entity passed
in (for example loaded from ORM), it incidentally wraps things up in
exception handling too.

I would now like to add a new method which does the same thing but for a
collection (java.util.List) of T.

I am unsure of the syntax, and structures to work with a collection of T in
the method signature, and abstract type param definitions.

Thanks for your help.

scalanewb
Joined: 2010-01-17,
User offline. Last seen 42 years 45 weeks ago.
Re: abstract method param question

Sorry, I forgot to say, I am using Scala 2.8.0-Beta1-RC2, if that makes any
difference?

scalanewb wrote:
>
> Hi.
>
> I have a helper method:
>
> def handleEntity[T <: Entity](exceptions: Class[_]*)(body: => T) = {
> try {
> val entity = body
> entity.callSomeMethod
> Some(entity)
> } catch {
> case e if (exceptions.contains(e.getClass)) => None
> }
> }
>
> called with:
>
> handleEntity[MyEntity](classOf[Exception]) { loadEntitySomehow }
>
> the main point of which is to call the 'callSomeMethod' on the entity
> passed in (for example loaded from ORM), it incidentally wraps things up
> in exception handling too.
>
> I would now like to add a new method which does the same thing but for a
> collection (java.util.List) of T.
>
> I am unsure of the syntax, and structures to work with a collection of T
> in the method signature, and abstract type param definitions.
>
> Thanks for your help.
>
>

bmaso
Joined: 2009-10-04,
User offline. Last seen 2 years 40 weeks ago.
Re: abstract method param question
Wow, I can't figure anything out to make a sequence of by-name parameters...

Of course if you use a sequence of zero-argument closures then its pretty easy, but you get all those ugly "()" you have to type over and over.

Best regards,
Brian Maso
(949) 395-8551
brian [at] blumenfeld-maso [dot] com
twitter: @bmaso
skype: brian.maso
LinkedIn: http://www.linkedin.com/in/brianmaso

On Mon, Jan 18, 2010 at 3:44 PM, scalanewb <rossajmcd [at] gmail [dot] com> wrote:

Sorry, I forgot to say, I am using Scala 2.8.0-Beta1-RC2, if that makes any
difference?



scalanewb wrote:
>
> Hi.
>
> I have a helper method:
>
> def handleEntity[T <: Entity](exceptions: Class[_]*)(body: => T) = {
>   try {
>     val entity = body
>     entity.callSomeMethod
>     Some(entity)
>   } catch {
>     case e if (exceptions.contains(e.getClass)) => None
>   }
> }
>
> called with:
>
> handleEntity[MyEntity](classOf[Exception]) { loadEntitySomehow }
>
> the main point of which is to call the 'callSomeMethod' on the entity
> passed in (for example loaded from ORM), it incidentally wraps things up
> in exception handling too.
>
> I would now like to add a new method which does the same thing but for a
> collection (java.util.List) of T.
>
> I am unsure of the syntax, and structures to work with a collection of T
> in the method signature, and abstract type param definitions.
>
> Thanks for your help.
>
>

--
View this message in context: http://old.nabble.com/abstract-method-param-question-tp27213642p27218698.html
Sent from the Scala - User mailing list archive at Nabble.com.


Johannes Rudolph
Joined: 2008-12-17,
User offline. Last seen 29 weeks 19 hours ago.
Re: abstract method param question

What works:

def x[T](xs: (() => T)*) = () => xs map (_())
implicit def lazyToFunc[T](x: => T):() => T = () => x

def a = {println("test");"a"}

> val f = x(a,a,a,"b","c")
f: () => Seq[java.lang.String] =

> f()
test
test
test
res0: Seq[java.lang.String] = ArrayBuffer(a, a, a, b, c)

What doesn't:

scala> val f = x({println("test");"a"},"b","c")
test
f: () => Seq[java.lang.String] =

So there is something strange going on here:
this seems to be translated into

x({println("test");lazyToFunc("a")}, //...

which is not what one would expect. So it seems that this is the work
of the type inferer: It assumes a type for the first parameter, which
is a block expression and then goes into the block and tries to make
the block result confer to the type expected from the outside. It
recognizes "a" as the result and then applies the implicit at that
position.

To make it work even with blocks you need to use type ascription:

val f = x({println("test");"a"}:String,"b","c")

On Tue, Jan 19, 2010 at 1:12 AM, Brian Maso wrote:
> Wow, I can't figure anything out to make a sequence of by-name parameters...
>
> Of course if you use a sequence of zero-argument closures then its pretty
> easy, but you get all those ugly "()" you have to type over and over.
>
> Best regards,
> Brian Maso
> (949) 395-8551
> brian [at] blumenfeld-maso [dot] com
> twitter: @bmaso
> skype: brian.maso
> LinkedIn: http://www.linkedin.com/in/brianmaso
>
> On Mon, Jan 18, 2010 at 3:44 PM, scalanewb wrote:
>>
>> Sorry, I forgot to say, I am using Scala 2.8.0-Beta1-RC2, if that makes
>> any
>> difference?
>>
>>
>>
>> scalanewb wrote:
>> >
>> > Hi.
>> >
>> > I have a helper method:
>> >
>> > def handleEntity[T <: Entity](exceptions: Class[_]*)(body: => T) = {
>> >   try {
>> >     val entity = body
>> >     entity.callSomeMethod
>> >     Some(entity)
>> >   } catch {
>> >     case e if (exceptions.contains(e.getClass)) => None
>> >   }
>> > }
>> >
>> > called with:
>> >
>> > handleEntity[MyEntity](classOf[Exception]) { loadEntitySomehow }
>> >
>> > the main point of which is to call the 'callSomeMethod' on the entity
>> > passed in (for example loaded from ORM), it incidentally wraps things up
>> > in exception handling too.
>> >
>> > I would now like to add a new method which does the same thing but for a
>> > collection (java.util.List) of T.
>> >
>> > I am unsure of the syntax, and structures to work with a collection of T
>> > in the method signature, and abstract type param definitions.
>> >
>> > Thanks for your help.
>> >
>> >
>>
>> --
>> View this message in context:
>> http://old.nabble.com/abstract-method-param-question-tp27213642p27218698...
>> Sent from the Scala - User mailing list archive at Nabble.com.
>>
>
>

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