- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
diverging implicits issue
Sun, 2010-06-27, 04:50
I have a class:
class A(val options: Seq[String])
I want to define Equiv for A:
implicit def equivA(implicit seqEq: Equiv[Seq[String]]): Equiv[A] = ...
However, equivA can't be used implicitly because Equiv[Seq[String]] dominates Equiv[A] :
complexity( Equiv[Seq[String]]) == 6
and
complexity( Equiv[A]) == 4
So, the compiler produces a diverging implicit error (and I'm pretty sure this is the right behavior according to the specification).
This is also a problem with enumerations or nested classes:
class B(val value: SomeEnum.Value)
because
complexity( Equiv[SomeEnum.Value] ) > complexity( Equiv[B] )
I've only gotten around it by explicitly calling equivA, but this is verbose for larger hierarchies. Or, for large hierarchies I've also done:
class Wrap(val options: Seq[String])
and directly defined
implicit def equivWrap: Equiv[Wrap] = new Equiv[Wrap] {
def equiv(a: Wrap, b: Wrap) = a.options sameElements b.options
}
This is not a good solution when defining Arbitrary/Gen instances for ScalaCheck or Format instances for SBinary, however.
So, is this a just an necessary tradeoff to prevent infinite expansion? Or, is it a possible candidate for a spec enhancement? Are there better workarounds?
Thanks,
Mark
Fri, 2010-07-02, 13:57
#2
Re: diverging implicits issue
On Tuesday 29 June 2010, martin odersky wrote:
> On Sun, Jun 27, 2010 at 5:49 AM, Mark Harrah wrote:
> > I have a class:
> > class A(val options: Seq[String])
> >
> > I want to define Equiv for A:
> > implicit def equivA(implicit seqEq: Equiv[Seq[String]]): Equiv[A] = ...
> >
> > However, equivA can't be used implicitly because Equiv[Seq[String]] dominates Equiv[A] :
> > complexity( Equiv[Seq[String]]) == 6
> > and
> > complexity( Equiv[A]) == 4
> >
> > So, the compiler produces a diverging implicit error (and I'm pretty sure this is the right behavior according to the specification).
> >
> > This is also a problem with enumerations or nested classes:
> > class B(val value: SomeEnum.Value)
> >
> > because
> > complexity( Equiv[SomeEnum.Value] ) > complexity( Equiv[B] )
> >
> > I've only gotten around it by explicitly calling equivA, but this is verbose for larger hierarchies. Or, for large hierarchies I've also done:
> > class Wrap(val options: Seq[String])
> > and directly defined
> > implicit def equivWrap: Equiv[Wrap] = new Equiv[Wrap] {
> > def equiv(a: Wrap, b: Wrap) = a.options sameElements b.options
> > }
> >
> > This is not a good solution when defining Arbitrary/Gen instances for ScalaCheck or Format instances for SBinary, however.
> >
> > So, is this a just an necessary tradeoff to prevent infinite expansion? Or, is it a possible candidate for a spec enhancement? Are there better workarounds?
> >
> > Thanks,
> > Mark
> >
> I realize it would be useful to do this, but I cannot see a way to
> make this work. Essentially, you are asking for a descreasing measure
> that is allowed to choose a global ordering of class weights. But
> that's not how implicits resolution works.
>
> Cheers
>
Fri, 2010-07-02, 17:07
#3
Scala, Spring and List injections
Hi,
I'm trying to use Scala and Spring together. I'm having difficulties to inject a list created with Spring into a Scala object that expect a List.
I'm looking for the best way to acheive that.
In the Spring context file, I have this:
<util:list id="myBeanList">
<ref bean="myBean1" />
<ref bean="myBean2" />
<ref bean="myBean3" />
</util:list>
When I get this bean in the code, in either of these two ways:
val o1 = context.getBean("myBeanList")
val o2 = context.getBean("myBeanList", classOf[java.util.List[MyType]])
The returned object is, as expected, of type java.util.ArrayList
I want to inject this bean into a Scala object that expects scala.collection.immutable.List - this is the part that does not work.
<bean id="myListContainerBean" class="my.scal.object.Wrapper">
<property name="theList" value="myBeanList" />
</bean>
Where the class is defined as follow
class my.scal.object.Wrapper {
@scala.reflect.BeanProperty
var theList : List[MyType] = List()
}
Now the question… is it possible to do such thing? How? I have the feeling that the JavaConversions package can help with that but I haven't figured out how to use it to do what I want.
I'm using Scala 2.8 RC7 with Spring 3.0.
Thanks,
-Martin
I'm trying to use Scala and Spring together. I'm having difficulties to inject a list created with Spring into a Scala object that expect a List.
I'm looking for the best way to acheive that.
In the Spring context file, I have this:
<util:list id="myBeanList">
<ref bean="myBean1" />
<ref bean="myBean2" />
<ref bean="myBean3" />
</util:list>
When I get this bean in the code, in either of these two ways:
val o1 = context.getBean("myBeanList")
val o2 = context.getBean("myBeanList", classOf[java.util.List[MyType]])
The returned object is, as expected, of type java.util.ArrayList
I want to inject this bean into a Scala object that expects scala.collection.immutable.List - this is the part that does not work.
<bean id="myListContainerBean" class="my.scal.object.Wrapper">
<property name="theList" value="myBeanList" />
</bean>
Where the class is defined as follow
class my.scal.object.Wrapper {
@scala.reflect.BeanProperty
var theList : List[MyType] = List()
}
Now the question… is it possible to do such thing? How? I have the feeling that the JavaConversions package can help with that but I haven't figured out how to use it to do what I want.
I'm using Scala 2.8 RC7 with Spring 3.0.
Thanks,
-Martin
Fri, 2010-07-02, 17:27
#4
Re: Scala, Spring and List injections
it looks like you're trying to inject an object of type java.util.List into a variable of type scala.collection.immutable.ListUnsurprisingly... it doesn't work.
You need something like:
import java.{util => ju} ... @BeanProperty var theList : ju.List[MyType] = _
If you import scala.collection.JavaConversion._ as well, then your property will be implicitly pimped with all methods available on scala.collection.mutable.Buffer
If you truly want an immutable list in your instance (which is a scala best practice) and you want to avoid all those messy getter/setter methods that spring demands, then consider using constructor-injection, or creating a dedicated factory bean. Whatever you do though, you must expect spring to provide the relevant java collection type, then convert it yourself.
On 2 July 2010 17:00, Martin Renaud <mr_martin_renaud [at] yahoo [dot] ca> wrote:
--
Kevin Wright
mail/google talk: kev [dot] lee [dot] wright [at] gmail [dot] com
wave: kev [dot] lee [dot] wright [at] googlewave [dot] com
skype: kev.lee.wright
twitter: @thecoda
You need something like:
import java.{util => ju} ... @BeanProperty var theList : ju.List[MyType] = _
If you import scala.collection.JavaConversion._ as well, then your property will be implicitly pimped with all methods available on scala.collection.mutable.Buffer
If you truly want an immutable list in your instance (which is a scala best practice) and you want to avoid all those messy getter/setter methods that spring demands, then consider using constructor-injection, or creating a dedicated factory bean. Whatever you do though, you must expect spring to provide the relevant java collection type, then convert it yourself.
On 2 July 2010 17:00, Martin Renaud <mr_martin_renaud [at] yahoo [dot] ca> wrote:
Hi,
I'm trying to use Scala and Spring together. I'm having difficulties to inject a list created with Spring into a Scala object that expect a List.
I'm looking for the best way to acheive that.
In the Spring context file, I have this:
<util:list id="myBeanList">
<ref bean="myBean1" />
<ref bean="myBean2" />
<ref bean="myBean3" />
</util:list>
When I get this bean in the code, in either of these two ways:
val o1 = context.getBean("myBeanList")
val o2 = context.getBean("myBeanList", classOf[java.util.List[MyType]])
The returned object is, as expected, of type java.util.ArrayList
I want to inject this bean into a Scala object that expects scala.collection.immutable.List - this is the part that does not work.
<bean id="myListContainerBean" class="my.scal.object.Wrapper">
<property name="theList" value="myBeanList" />
</bean>
Where the class is defined as follow
class my.scal.object.Wrapper {
@scala.reflect.BeanProperty
var theList : List[MyType] = List()
}
Now the question… is it possible to do such thing? How? I have the feeling that the JavaConversions package can help with that but I haven't figured out how to use it to do what I want.
I'm using Scala 2.8 RC7 with Spring 3.0.
Thanks,
-Martin
--
Kevin Wright
mail/google talk: kev [dot] lee [dot] wright [at] gmail [dot] com
wave: kev [dot] lee [dot] wright [at] googlewave [dot] com
skype: kev.lee.wright
twitter: @thecoda
Fri, 2010-07-02, 18:27
#5
Re: Scala, Spring and List injections
spring can inject lists as arrays too so you could use that to converto to a scala List - so you dont get 2 Lists in your namespace
On Fri, Jul 2, 2010 at 1:24 PM, Kevin Wright <kev [dot] lee [dot] wright [at] gmail [dot] com> wrote:
--
http://mapsdev.blogspot.com/
Marcelo Takeshi Fukushima
On Fri, Jul 2, 2010 at 1:24 PM, Kevin Wright <kev [dot] lee [dot] wright [at] gmail [dot] com> wrote:
it looks like you're trying to inject an object of type java.util.List into a variable of type scala.collection.immutable.List Unsurprisingly... it doesn't work.
You need something like:
import java.{util => ju} ... @BeanProperty var theList : ju.List[MyType] = _
If you import scala.collection.JavaConversion._ as well, then your property will be implicitly pimped with all methods available on scala.collection.mutable.Buffer
If you truly want an immutable list in your instance (which is a scala best practice) and you want to avoid all those messy getter/setter methods that spring demands, then consider using constructor-injection, or creating a dedicated factory bean. Whatever you do though, you must expect spring to provide the relevant java collection type, then convert it yourself.
On 2 July 2010 17:00, Martin Renaud <mr_martin_renaud [at] yahoo [dot] ca> wrote:
Hi,
I'm trying to use Scala and Spring together. I'm having difficulties to inject a list created with Spring into a Scala object that expect a List.
I'm looking for the best way to acheive that.
In the Spring context file, I have this:
<util:list id="myBeanList">
<ref bean="myBean1" />
<ref bean="myBean2" />
<ref bean="myBean3" />
</util:list>
When I get this bean in the code, in either of these two ways:
val o1 = context.getBean("myBeanList")
val o2 = context.getBean("myBeanList", classOf[java.util.List[MyType]])
The returned object is, as expected, of type java.util.ArrayList
I want to inject this bean into a Scala object that expects scala.collection.immutable.List - this is the part that does not work.
<bean id="myListContainerBean" class="my.scal.object.Wrapper">
<property name="theList" value="myBeanList" />
</bean>
Where the class is defined as follow
class my.scal.object.Wrapper {
@scala.reflect.BeanProperty
var theList : List[MyType] = List()
}
Now the question… is it possible to do such thing? How? I have the feeling that the JavaConversions package can help with that but I haven't figured out how to use it to do what I want.
I'm using Scala 2.8 RC7 with Spring 3.0.
Thanks,
-Martin
--
Kevin Wright
mail/google talk: kev [dot] lee [dot] wright [at] gmail [dot] com
wave: kev [dot] lee [dot] wright [at] googlewave [dot] com
skype: kev.lee.wright
twitter: @thecoda
--
http://mapsdev.blogspot.com/
Marcelo Takeshi Fukushima
Fri, 2010-07-02, 18:37
#6
Re: Scala, Spring and List injections
I can't make anything work with import scala.collection.JavaConversion._. It has no effect, wetter I inject the ArrayList into a Buffer with Spring or if I do a simple assignation in code.
I've manage to make things work like this, but I don't really like it. I was looking for a way to do the equivalant without an reference to java.util in my code. Maybe it is just impossible ?
class MyRepository {
var attractions : List[MyType] = List()
def setAttractionList(list : java.util.List[MyType]) = {
attractions = list.toList
}
}
and this :
<bean id="myRepository" class="pkg.MyRepository">
<property name="attractionList" ref="myBeanList" />
</bean>
From: Kevin Wright <kev [dot] lee [dot] wright [at] gmail [dot] com>
To: Martin Renaud <mr_martin_renaud [at] yahoo [dot] ca>
Cc: scala [at] listes [dot] epfl [dot] ch
Sent: Fri, July 2, 2010 12:24:07 PM
Subject: Re: [scala] Scala, Spring and List injections
it looks like you're trying to inject an object of type java.util.List into a variable of type scala.collection.immutable.ListUnsurprisingly... it doesn't work.
You need something like:
import java.{util => ju} ... @BeanProperty var theList : ju.List[MyType] = _
If you import scala.collection.JavaConversion._ as well, then your property will be implicitly pimped with all methods available on scala.collection.mutable.Buffer
If you truly want an immutable list in your instance (which is a scala best practice) and you want to avoid all those messy getter/setter methods that spring demands, then consider using constructor-injection, or creating a dedicated factory bean. Whatever you do though, you must expect spring to provide the relevant java collection type, then convert it yourself.
On 2 July 2010 17:00, Martin Renaud <mr_martin_renaud [at] yahoo [dot] ca" target="_blank" href="mailto:mr_martin_renaud [at] yahoo [dot] ca" rel="nofollow">mr_martin_renaud [at] yahoo [dot] ca> wrote:
--
Kevin Wright
mail/google talk: kev [dot] lee [dot] wright [at] gmail [dot] com" target="_blank" href="mailto:kev [dot] lee [dot] wright [at] gmail [dot] com" rel="nofollow">kev [dot] lee [dot] wright [at] gmail [dot] com
wave: kev [dot] lee [dot] wright [at] googlewave [dot] com" target="_blank" href="mailto:kev [dot] lee [dot] wright [at] googlewave [dot] com" rel="nofollow">kev [dot] lee [dot] wright [at] googlewave [dot] com
skype: kev.lee.wright
twitter: @thecoda
I've manage to make things work like this, but I don't really like it. I was looking for a way to do the equivalant without an reference to java.util in my code. Maybe it is just impossible ?
class MyRepository {
var attractions : List[MyType] = List()
def setAttractionList(list : java.util.List[MyType]) = {
attractions = list.toList
}
}
and this :
<bean id="myRepository" class="pkg.MyRepository">
<property name="attractionList" ref="myBeanList" />
</bean>
From: Kevin Wright <kev [dot] lee [dot] wright [at] gmail [dot] com>
To: Martin Renaud <mr_martin_renaud [at] yahoo [dot] ca>
Cc: scala [at] listes [dot] epfl [dot] ch
Sent: Fri, July 2, 2010 12:24:07 PM
Subject: Re: [scala] Scala, Spring and List injections
it looks like you're trying to inject an object of type java.util.List into a variable of type scala.collection.immutable.ListUnsurprisingly... it doesn't work.
You need something like:
import java.{util => ju} ... @BeanProperty var theList : ju.List[MyType] = _
If you import scala.collection.JavaConversion._ as well, then your property will be implicitly pimped with all methods available on scala.collection.mutable.Buffer
If you truly want an immutable list in your instance (which is a scala best practice) and you want to avoid all those messy getter/setter methods that spring demands, then consider using constructor-injection, or creating a dedicated factory bean. Whatever you do though, you must expect spring to provide the relevant java collection type, then convert it yourself.
On 2 July 2010 17:00, Martin Renaud <mr_martin_renaud [at] yahoo [dot] ca" target="_blank" href="mailto:mr_martin_renaud [at] yahoo [dot] ca" rel="nofollow">mr_martin_renaud [at] yahoo [dot] ca> wrote:
Hi,
I'm trying to use Scala and Spring together. I'm having difficulties to inject a list created with Spring into a Scala object that expect a List.
I'm looking for the best way to acheive that.
In the Spring context file, I have this:
<util:list id="myBeanList">
<ref bean="myBean1" />
<ref bean="myBean2" />
<ref bean="myBean3" />
</util:list>
When I get this bean in the code, in either of these two ways:
val o1 = context.getBean("myBeanList")
val o2 = context.getBean("myBeanList", classOf[java.util.List[MyType]])
The returned object is, as expected, of type java.util.ArrayList
I want to inject this bean into a Scala object that expects scala.collection.immutable.List - this is the part that does not work.
<bean id="myListContainerBean" class="my.scal.object.Wrapper">
<property name="theList" value="myBeanList" />
</bean>
Where the class is defined as follow
class my.scal.object.Wrapper {
@scala.reflect.BeanProperty
var theList : List[MyType] = List()
}
Now the question… is it possible to do such thing? How? I have the feeling that the JavaConversions package can help with that but I haven't figured out how to use it to do what I want.
I'm using Scala 2.8 RC7 with Spring 3.0.
Thanks,
-Martin
--
Kevin Wright
mail/google talk: kev [dot] lee [dot] wright [at] gmail [dot] com" target="_blank" href="mailto:kev [dot] lee [dot] wright [at] gmail [dot] com" rel="nofollow">kev [dot] lee [dot] wright [at] gmail [dot] com
wave: kev [dot] lee [dot] wright [at] googlewave [dot] com" target="_blank" href="mailto:kev [dot] lee [dot] wright [at] googlewave [dot] com" rel="nofollow">kev [dot] lee [dot] wright [at] googlewave [dot] com
skype: kev.lee.wright
twitter: @thecoda
Fri, 2010-07-02, 18:37
#7
Re: Scala, Spring and List injections
Again, this solution requires my code to compensate the fact that Spring util:list cannot return something directly Scala either by smart util:list or some automatic conversion.
But as I mention in a previous reply, I found a workaround similar to the one you suggested. I was looking for something more automatic/transparent.
Thanks,
Martin
From: Marcelo Fukushima <takeshi10 [at] gmail [dot] com>
To: Kevin Wright <kev [dot] lee [dot] wright [at] gmail [dot] com>
Cc: Martin Renaud <mr_martin_renaud [at] yahoo [dot] ca>; scala [at] listes [dot] epfl [dot] ch
Sent: Fri, July 2, 2010 1:17:53 PM
Subject: Re: [scala] Scala, Spring and List injections
spring can inject lists as arrays too so you could use that to converto to a scala List - so you dont get 2 Lists in your namespace
On Fri, Jul 2, 2010 at 1:24 PM, Kevin Wright <kev [dot] lee [dot] wright [at] gmail [dot] com" target="_blank" href="mailto:kev [dot] lee [dot] wright [at] gmail [dot] com" rel="nofollow">kev [dot] lee [dot] wright [at] gmail [dot] com> wrote:
--
http://mapsdev.blogspot.com/
Marcelo Takeshi Fukushima
But as I mention in a previous reply, I found a workaround similar to the one you suggested. I was looking for something more automatic/transparent.
Thanks,
Martin
From: Marcelo Fukushima <takeshi10 [at] gmail [dot] com>
To: Kevin Wright <kev [dot] lee [dot] wright [at] gmail [dot] com>
Cc: Martin Renaud <mr_martin_renaud [at] yahoo [dot] ca>; scala [at] listes [dot] epfl [dot] ch
Sent: Fri, July 2, 2010 1:17:53 PM
Subject: Re: [scala] Scala, Spring and List injections
spring can inject lists as arrays too so you could use that to converto to a scala List - so you dont get 2 Lists in your namespace
On Fri, Jul 2, 2010 at 1:24 PM, Kevin Wright <kev [dot] lee [dot] wright [at] gmail [dot] com" target="_blank" href="mailto:kev [dot] lee [dot] wright [at] gmail [dot] com" rel="nofollow">kev [dot] lee [dot] wright [at] gmail [dot] com> wrote:
it looks like you're trying to inject an object of type java.util.List into a variable of type scala.collection.immutable.List Unsurprisingly... it doesn't work.
You need something like:
import java.{util => ju} ... @BeanProperty var theList : ju.List[MyType] = _
If you import scala.collection.JavaConversion._ as well, then your property will be implicitly pimped with all methods available on scala.collection.mutable.Buffer
If you truly want an immutable list in your instance (which is a scala best practice) and you want to avoid all those messy getter/setter methods that spring demands, then consider using constructor-injection, or creating a dedicated factory bean. Whatever you do though, you must expect spring to provide the relevant java collection type, then convert it yourself.
On 2 July 2010 17:00, Martin Renaud <mr_martin_renaud [at] yahoo [dot] ca" target="_blank" href="mailto:mr_martin_renaud [at] yahoo [dot] ca" rel="nofollow">mr_martin_renaud [at] yahoo [dot] ca> wrote:
Hi,
I'm trying to use Scala and Spring together. I'm having difficulties to inject a list created with Spring into a Scala object that expect a List.
I'm looking for the best way to acheive that.
In the Spring context file, I have this:
<util:list id="myBeanList">
<ref bean="myBean1" />
<ref bean="myBean2" />
<ref bean="myBean3" />
</util:list>
When I get this bean in the code, in either of these two ways:
val o1 = context.getBean("myBeanList")
val o2 = context.getBean("myBeanList", classOf[java.util.List[MyType]])
The returned object is, as expected, of type java.util.ArrayList
I want to inject this bean into a Scala object that expects scala.collection.immutable.List - this is the part that does not work.
<bean id="myListContainerBean" class="my.scal.object.Wrapper">
<property name="theList" value="myBeanList" />
</bean>
Where the class is defined as follow
class my.scal.object.Wrapper {
@scala.reflect.BeanProperty
var theList : List[MyType] = List()
}
Now the question… is it possible to do such thing? How? I have the feeling that the JavaConversions package can help with that but I haven't figured out how to use it to do what I want.
I'm using Scala 2.8 RC7 with Spring 3.0.
Thanks,
-Martin
--
Kevin Wright
mail/google talk: kev [dot] lee [dot] wright [at] gmail [dot] com" target="_blank" href="mailto:kev [dot] lee [dot] wright [at] gmail [dot] com" rel="nofollow">kev [dot] lee [dot] wright [at] gmail [dot] com
wave: kev [dot] lee [dot] wright [at] googlewave [dot] com" target="_blank" href="mailto:kev [dot] lee [dot] wright [at] googlewave [dot] com" rel="nofollow">kev [dot] lee [dot] wright [at] googlewave [dot] com
skype: kev.lee.wright
twitter: @thecoda
--
http://mapsdev.blogspot.com/
Marcelo Takeshi Fukushima
Sat, 2010-07-03, 13:47
#8
Re: Scala, Spring and List injections
Hi Martin,
Your problem is rooted in the fact that Spring is written for the Java libraries and so, while it's quite good at coercing things into standard Java types, Spring knows nothing about types from the Scala libraries.
However, having worked quite a lot with Spring, I would be very surprised if there isn't some way that you can extend Spring to do what you want.I would suggest getting the Spring source code, running in debug mode with some code that uses a java.util.List, setting a breakpoint on your List setter, and then reading the Spring code all the way up the stack until you find the class that is coercing the configuration into a java.util.List.Somewhere around that class or the ones above and below it in the stack you will hopefully find an extension point where you can plug in a piece of custom code that will tell Spring how to coerce your configuration into a Scala List.This means you will still have to add some custom code, but you will only need to add it in one place, rather than in every class that you write that requires a list of beans.
Let us know if you find a solution!
Cheers,
Graham.
--
Graham Lea
graham [at] grahamlea [dot] com
On 03/07/2010, at 3:32 AM, Martin Renaud wrote:
Your problem is rooted in the fact that Spring is written for the Java libraries and so, while it's quite good at coercing things into standard Java types, Spring knows nothing about types from the Scala libraries.
However, having worked quite a lot with Spring, I would be very surprised if there isn't some way that you can extend Spring to do what you want.I would suggest getting the Spring source code, running in debug mode with some code that uses a java.util.List, setting a breakpoint on your List setter, and then reading the Spring code all the way up the stack until you find the class that is coercing the configuration into a java.util.List.Somewhere around that class or the ones above and below it in the stack you will hopefully find an extension point where you can plug in a piece of custom code that will tell Spring how to coerce your configuration into a Scala List.This means you will still have to add some custom code, but you will only need to add it in one place, rather than in every class that you write that requires a list of beans.
Let us know if you find a solution!
Cheers,
Graham.
--
Graham Lea
graham [at] grahamlea [dot] com
On 03/07/2010, at 3:32 AM, Martin Renaud wrote:
Again, this solution requires my code to compensate the fact that Spring util:list cannot return something directly Scala either by smart util:list or some automatic conversion.
But as I mention in a previous reply, I found a workaround similar to the one you suggested. I was looking for something more automatic/transparent.
Thanks,
Martin
From: Marcelo Fukushima <takeshi10 [at] gmail [dot] com>
To: Kevin Wright <kev [dot] lee [dot] wright [at] gmail [dot] com>
Cc: Martin Renaud <mr_martin_renaud [at] yahoo [dot] ca>; scala [at] listes [dot] epfl [dot] ch
Sent: Fri, July 2, 2010 1:17:53 PM
Subject: Re: [scala] Scala, Spring and List injections
spring can inject lists as arrays too so you could use that to converto to a scala List - so you dont get 2 Lists in your namespace
On Fri, Jul 2, 2010 at 1:24 PM, Kevin Wright <kev [dot] lee [dot] wright [at] gmail [dot] com" target="_blank" href="mailto:kev [dot] lee [dot] wright [at] gmail [dot] com" rel="nofollow">kev [dot] lee [dot] wright [at] gmail [dot] com> wrote:it looks like you're trying to inject an object of type java.util.List into a variable of type scala.collection.immutable.ListUnsurprisingly... it doesn't work.
You need something like:
import java.{util => ju} ... @BeanProperty var theList : ju.List[MyType] = _
If you import scala.collection.JavaConversion._ as well, then your property will be implicitly pimped with all methods available on scala.collection.mutable.Buffer
If you truly want an immutable list in your instance (which is a scala best practice) and you want to avoid all those messy getter/setter methods that spring demands, then consider using constructor-injection, or creating a dedicated factory bean. Whatever you do though, you must expect spring to provide the relevant java collection type, then convert it yourself.
On 2 July 2010 17:00, Martin Renaud <mr_martin_renaud [at] yahoo [dot] ca" target="_blank" href="mailto:mr_martin_renaud [at] yahoo [dot] ca" rel="nofollow">mr_martin_renaud [at] yahoo [dot] ca> wrote:Hi,
I'm trying to use Scala and Spring together. I'm having difficulties to inject a list created with Spring into a Scala object that expect a List.
I'm looking for the best way to acheive that.
In the Spring context file, I have this:
<util:list id="myBeanList">
<ref bean="myBean1" />
<ref bean="myBean2" />
<ref bean="myBean3" />
</util:list>
When I get this bean in the code, in either of these two ways:
val o1 = context.getBean("myBeanList")
val o2 = context.getBean("myBeanList", classOf[java.util.List[MyType]])
The returned object is, as expected, of type java.util.ArrayList
I want to inject this bean into a Scala object that expects scala.collection.immutable.List - this is the part that does not work.
<bean id="myListContainerBean" class="my.scal.object.Wrapper">
<property name="theList" value="myBeanList" />
</bean>
Where the class is defined as follow
class my.scal.object.Wrapper {
@scala.reflect.BeanProperty
var theList : List[MyType] = List()
}
Now the question… is it possible to do such thing? How? I have the feeling that the JavaConversions package can help with that but I haven't figured out how to use it to do what I want.
I'm using Scala 2.8 RC7 with Spring 3.0.
Thanks,
-Martin
--
Kevin Wright
mail/google talk: kev [dot] lee [dot] wright [at] gmail [dot] com" target="_blank" href="mailto:kev [dot] lee [dot] wright [at] gmail [dot] com" rel="nofollow">kev [dot] lee [dot] wright [at] gmail [dot] com
wave: kev [dot] lee [dot] wright [at] googlewave [dot] com" target="_blank" href="mailto:kev [dot] lee [dot] wright [at] googlewave [dot] com" rel="nofollow">kev [dot] lee [dot] wright [at] googlewave [dot] com
skype: kev.lee.wright
twitter: @thecoda
--
http://mapsdev.blogspot.com/
Marcelo Takeshi Fukushima
Sat, 2010-07-03, 13:57
#9
Re: Scala, Spring and List injections
Graham Lea wrote:
C082BA79-94AF-4D93-B8F8-1F2B43376CE4 [at] grahamlea [dot] com" type="cite"> Hi Martin,
Your problem is rooted in the fact that Spring is written for the Java libraries and so, while it's quite good at coercing things into standard Java types, Spring knows nothing about types from the Scala libraries.
However, having worked quite a lot with Spring, I would be very surprised if there isn't some way that you can extend Spring to do what you want. I would suggest getting the Spring source code, running in debug mode with some code that uses a java.util.List, setting a breakpoint on your List setter, and then reading the Spring code all the way up the stack until you find the class that is coercing the configuration into a java.util.List.
Having looked at the source, there are two ways:
1. a custom property editor
2. a custom conversion service.
See TypedConversionDelegate (used by BeanWrapperImpl)
I didn't personally use any of these, so I can't give more explanation
Ittay
C082BA79-94AF-4D93-B8F8-1F2B43376CE4 [at] grahamlea [dot] com" type="cite"> Somewhere around that class or the ones above and below it in the stack you will hopefully find an extension point where you can plug in a piece of custom code that will tell Spring how to coerce your configuration into a Scala List. This means you will still have to add some custom code, but you will only need to add it in one place, rather than in every class that you write that requires a list of beans.
Let us know if you find a solution!
Cheers,
Graham.
--
Graham Lea
graham [at] grahamlea [dot] com" rel="nofollow">graham [at] grahamlea [dot] com
On 03/07/2010, at 3:32 AM, Martin Renaud wrote:
Again, this solution requires my code to compensate the fact that Spring util:list cannot return something directly Scala either by smart util:list or some automatic conversion.
But as I mention in a previous reply, I found a workaround similar to the one you suggested. I was looking for something more automatic/transparent.
Thanks,
Martin
From: Marcelo Fukushima <takeshi10 [at] gmail [dot] com" rel="nofollow">takeshi10 [at] gmail [dot] com>
To: Kevin Wright <kev [dot] lee [dot] wright [at] gmail [dot] com" rel="nofollow">kev [dot] lee [dot] wright [at] gmail [dot] com>
Cc: Martin Renaud <mr_martin_renaud [at] yahoo [dot] ca" rel="nofollow">mr_martin_renaud [at] yahoo [dot] ca>; scala [at] listes [dot] epfl [dot] ch" rel="nofollow">scala [at] listes [dot] epfl [dot] ch
Sent: Fri, July 2, 2010 1:17:53 PM
Subject: Re: [scala] Scala, Spring and List injections
spring can inject lists as arrays too so you could use that to converto to a scala List - so you dont get 2 Lists in your namespace
On Fri, Jul 2, 2010 at 1:24 PM, Kevin Wright <kev [dot] lee [dot] wright [at] gmail [dot] com" target="_blank" href="mailto:kev [dot] lee [dot] wright [at] gmail [dot] com" rel="nofollow">kev [dot] lee [dot] wright [at] gmail [dot] com> wrote:
it looks like you're trying to inject an object of type java.util.List into a variable of type scala.collection.immutable.List Unsurprisingly... it doesn't work.
You need something like:
import java.{util => ju} ... @BeanProperty var theList : ju.List[MyType] = _
If you import scala.collection.JavaConversion._ as well, then your property will be implicitly pimped with all methods available on scala.collection.mutable.Buffer
If you truly want an immutable list in your instance (which is a scala best practice) and you want to avoid all those messy getter/setter methods that spring demands, then consider using constructor-injection, or creating a dedicated factory bean. Whatever you do though, you must expect spring to provide the relevant java collection type, then convert it yourself.
On 2 July 2010 17:00, Martin Renaud <mr_martin_renaud [at] yahoo [dot] ca" target="_blank" href="mailto:mr_martin_renaud [at] yahoo [dot] ca" rel="nofollow">mr_martin_renaud [at] yahoo [dot] ca> wrote:
Hi,
I'm trying to use Scala and Spring together. I'm having difficulties to inject a list created with Spring into a Scala object that expect a List.
I'm looking for the best way to acheive that.
In the Spring context file, I have this:
<util:list id="myBeanList">
<ref bean="myBean1" />
<ref bean="myBean2" />
<ref bean="myBean3" />
</util:list>
When I get this bean in the code, in either of these two ways:
val o1 = context.getBean("myBeanList")
val o2 = context.getBean("myBeanList", classOf[java.util.List[MyType]])
The returned object is, as expected, of type java.util.ArrayList
I want to inject this bean into a Scala object that expects scala.collection.immutable.List - this is the part that does not work.
<bean id="myListContainerBean" class="my.scal.object.Wrapper">
<property name="theList" value="myBeanList" />
</bean>
Where the class is defined as follow
class my.scal.object.Wrapper {
@scala.reflect.BeanProperty
var theList : List[MyType] = List()
}
Now the question… is it possible to do such thing? How? I have the feeling that the JavaConversions package can help with that but I haven't figured out how to use it to do what I want.
I'm using Scala 2.8 RC7 with Spring 3.0.
Thanks,
-Martin
--
Kevin Wright
mail/google talk: kev [dot] lee [dot] wright [at] gmail [dot] com" target="_blank" href="mailto:kev [dot] lee [dot] wright [at] gmail [dot] com" rel="nofollow">kev [dot] lee [dot] wright [at] gmail [dot] com
wave: kev [dot] lee [dot] wright [at] googlewave [dot] com" target="_blank" href="mailto:kev [dot] lee [dot] wright [at] googlewave [dot] com" rel="nofollow">kev [dot] lee [dot] wright [at] googlewave [dot] com
skype: kev.lee.wright
twitter: @thecoda
--
http://mapsdev.blogspot.com/
Marcelo Takeshi Fukushima
Sun, 2010-07-04, 13:47
#10
Re: Scala, Spring and List injections
Hi,
I'm also convinced of what you are saying. I was hoppoing someone already did it ;-)
I'll give it a try. I think it's possible to provide another namespace for Scala stuff and it would be possible to go with scala:list, scala:map, ... One advantage of using Spring is to be able to get all what comes with it, the bean post-processor for example. If I acheive something, I will post the result here.
Thanks for the pointers.
-Martin
From: Graham Lea <graham [at] grahamlea [dot] com>
To: Martin Renaud <mr_martin_renaud [at] yahoo [dot] ca>
Cc: Scala list <scala [at] listes [dot] epfl [dot] ch>
Sent: Sat, July 3, 2010 8:37:31 AM
Subject: Re: [scala] Scala, Spring and List injections
Hi Martin,
Your problem is rooted in the fact that Spring is written for the Java libraries and so, while it's quite good at coercing things into standard Java types, Spring knows nothing about types from the Scala libraries.
However, having worked quite a lot with Spring, I would be very surprised if there isn't some way that you can extend Spring to do what you want.I would suggest getting the Spring source code, running in debug mode with some code that uses a java.util.List, setting a breakpoint on your List setter, and then reading the Spring code all the way up the stack until you find the class that is coercing the configuration into a java.util.List.Somewhere around that class or the ones above and below it in the stack you will hopefully find an extension point where you can plug in a piece of custom code that will tell Spring how to coerce your configuration into a Scala List.This means you will still have to add some custom code, but you will only need to add it in one place, rather than in every class that you write that requires a list of beans.
Let us know if you find a solution!
Cheers,
Graham.
--
Graham Lea
graham [at] grahamlea [dot] com" target="_blank" href="mailto:graham [at] grahamlea [dot] com" rel="nofollow">graham [at] grahamlea [dot] com
On 03/07/2010, at 3:32 AM, Martin Renaud wrote:
I'm also convinced of what you are saying. I was hoppoing someone already did it ;-)
I'll give it a try. I think it's possible to provide another namespace for Scala stuff and it would be possible to go with scala:list, scala:map, ... One advantage of using Spring is to be able to get all what comes with it, the bean post-processor for example. If I acheive something, I will post the result here.
Thanks for the pointers.
-Martin
From: Graham Lea <graham [at] grahamlea [dot] com>
To: Martin Renaud <mr_martin_renaud [at] yahoo [dot] ca>
Cc: Scala list <scala [at] listes [dot] epfl [dot] ch>
Sent: Sat, July 3, 2010 8:37:31 AM
Subject: Re: [scala] Scala, Spring and List injections
Hi Martin,
Your problem is rooted in the fact that Spring is written for the Java libraries and so, while it's quite good at coercing things into standard Java types, Spring knows nothing about types from the Scala libraries.
However, having worked quite a lot with Spring, I would be very surprised if there isn't some way that you can extend Spring to do what you want.I would suggest getting the Spring source code, running in debug mode with some code that uses a java.util.List, setting a breakpoint on your List setter, and then reading the Spring code all the way up the stack until you find the class that is coercing the configuration into a java.util.List.Somewhere around that class or the ones above and below it in the stack you will hopefully find an extension point where you can plug in a piece of custom code that will tell Spring how to coerce your configuration into a Scala List.This means you will still have to add some custom code, but you will only need to add it in one place, rather than in every class that you write that requires a list of beans.
Let us know if you find a solution!
Cheers,
Graham.
--
Graham Lea
graham [at] grahamlea [dot] com" target="_blank" href="mailto:graham [at] grahamlea [dot] com" rel="nofollow">graham [at] grahamlea [dot] com
On 03/07/2010, at 3:32 AM, Martin Renaud wrote:
Again, this solution requires my code to compensate the fact that Spring util:list cannot return something directly Scala either by smart util:list or some automatic conversion.
But as I mention in a previous reply, I found a workaround similar to the one you suggested. I was looking for something more automatic/transparent.
Thanks,
Martin
From: Marcelo Fukushima <takeshi10 [at] gmail [dot] com" target="_blank" href="mailto:takeshi10 [at] gmail [dot] com" rel="nofollow">takeshi10 [at] gmail [dot] com>
To: Kevin Wright <kev [dot] lee [dot] wright [at] gmail [dot] com" target="_blank" href="mailto:kev [dot] lee [dot] wright [at] gmail [dot] com" rel="nofollow">kev [dot] lee [dot] wright [at] gmail [dot] com>
Cc: Martin Renaud <mr_martin_renaud [at] yahoo [dot] ca" target="_blank" href="mailto:mr_martin_renaud [at] yahoo [dot] ca" rel="nofollow">mr_martin_renaud [at] yahoo [dot] ca>; scala [at] listes [dot] epfl [dot] ch" target="_blank" href="mailto:scala [at] listes [dot] epfl [dot] ch" rel="nofollow">scala [at] listes [dot] epfl [dot] ch
Sent: Fri, July 2, 2010 1:17:53 PM
Subject: Re: [scala] Scala, Spring and List injections
spring can inject lists as arrays too so you could use that to converto to a scala List - so you dont get 2 Lists in your namespace
On Fri, Jul 2, 2010 at 1:24 PM, Kevin Wright <kev [dot] lee [dot] wright [at] gmail [dot] com" target="_blank" href="mailto:kev [dot] lee [dot] wright [at] gmail [dot] com" rel="nofollow">kev [dot] lee [dot] wright [at] gmail [dot] com> wrote:it looks like you're trying to inject an object of type java.util.List into a variable of type scala.collection.immutable.ListUnsurprisingly... it doesn't work.
You need something like:
import java.{util => ju} ... @BeanProperty var theList : ju.List[MyType] = _
If you import scala.collection.JavaConversion._ as well, then your property will be implicitly pimped with all methods available on scala.collection.mutable.Buffer
If you truly want an immutable list in your instance (which is a scala best practice) and you want to avoid all those messy getter/setter methods that spring demands, then consider using constructor-injection, or creating a dedicated factory bean. Whatever you do though, you must expect spring to provide the relevant java collection type, then convert it yourself.
On 2 July 2010 17:00, Martin Renaud <mr_martin_renaud [at] yahoo [dot] ca" target="_blank" href="mailto:mr_martin_renaud [at] yahoo [dot] ca" rel="nofollow">mr_martin_renaud [at] yahoo [dot] ca> wrote:Hi,
I'm trying to use Scala and Spring together. I'm having difficulties to inject a list created with Spring into a Scala object that expect a List.
I'm looking for the best way to acheive that.
In the Spring context file, I have this:
<util:list id="myBeanList">
<ref bean="myBean1" />
<ref bean="myBean2" />
<ref bean="myBean3" />
</util:list>
When I get this bean in the code, in either of these two ways:
val o1 = context.getBean("myBeanList")
val o2 = context.getBean("myBeanList", classOf[java.util.List[MyType]])
The returned object is, as expected, of type java.util.ArrayList
I want to inject this bean into a Scala object that expects scala.collection.immutable.List - this is the part that does not work.
<bean id="myListContainerBean" class="my.scal.object.Wrapper">
<property name="theList" value="myBeanList" />
</bean>
Where the class is defined as follow
class my.scal.object.Wrapper {
@scala.reflect.BeanProperty
var theList : List[MyType] = List()
}
Now the question… is it possible to do such thing? How? I have the feeling that the JavaConversions package can help with that but I haven't figured out how to use it to do what I want.
I'm using Scala 2.8 RC7 with Spring 3.0.
Thanks,
-Martin
--
Kevin Wright
mail/google talk: kev [dot] lee [dot] wright [at] gmail [dot] com" target="_blank" href="mailto:kev [dot] lee [dot] wright [at] gmail [dot] com" rel="nofollow">kev [dot] lee [dot] wright [at] gmail [dot] com
wave: kev [dot] lee [dot] wright [at] googlewave [dot] com" target="_blank" href="mailto:kev [dot] lee [dot] wright [at] googlewave [dot] com" rel="nofollow">kev [dot] lee [dot] wright [at] googlewave [dot] com
skype: kev.lee.wright
twitter: @thecoda
--
http://mapsdev.blogspot.com/
Marcelo Takeshi Fukushima
Wed, 2010-07-14, 01:27
#11
Re: diverging implicits issue
On Fri, Jul 2, 2010 at 2:52 PM, Mark Harrah wrote:
> On Tuesday 29 June 2010, martin odersky wrote:
>> On Sun, Jun 27, 2010 at 5:49 AM, Mark Harrah wrote:
>> > I have a class:
>> > class A(val options: Seq[String])
>> >
>> > I want to define Equiv for A:
>> > implicit def equivA(implicit seqEq: Equiv[Seq[String]]): Equiv[A] = ...
>> >
>> > However, equivA can't be used implicitly because Equiv[Seq[String]] dominates Equiv[A] :
>> > complexity( Equiv[Seq[String]]) == 6
>> > and
>> > complexity( Equiv[A]) == 4
>> >
>> > So, the compiler produces a diverging implicit error (and I'm pretty sure this is the right behavior according to the specification).
>> >
>> > This is also a problem with enumerations or nested classes:
>> > class B(val value: SomeEnum.Value)
>> >
>> > because
>> > complexity( Equiv[SomeEnum.Value] ) > complexity( Equiv[B] )
>> >
>> > I've only gotten around it by explicitly calling equivA, but this is verbose for larger hierarchies. Or, for large hierarchies I've also done:
>> > class Wrap(val options: Seq[String])
>> > and directly defined
>> > implicit def equivWrap: Equiv[Wrap] = new Equiv[Wrap] {
>> > def equiv(a: Wrap, b: Wrap) = a.options sameElements b.options
>> > }
>> >
>> > This is not a good solution when defining Arbitrary/Gen instances for ScalaCheck or Format instances for SBinary, however.
>> >
>> > So, is this a just an necessary tradeoff to prevent infinite expansion? Or, is it a possible candidate for a spec enhancement? Are there better workarounds?
>> >
>> > Thanks,
>> > Mark
>> >
>> I realize it would be useful to do this, but I cannot see a way to
>> make this work. Essentially, you are asking for a descreasing measure
>> that is allowed to choose a global ordering of class weights. But
>> that's not how implicits resolution works.
>>
>> Cheers
>>
>> -- Martin
>
> My understanding is that for an infinite expansion to occur, the search must cycle through the same method with types are not lower in complexity than the last time through.
>
> If this is true, isn't it sufficient to check only the open implicits for the same implicit method as the new implicit being searched?
>
> I'm not sure that was worded too well, so maybe I can be clearer with some examples.
>
> For my case above,
> implicit def equivSeq: Equiv[Seq[String]] = ...
> implicit def equivA(implicit e: Equiv[Seq[String]]): Equiv[A] = ...
> implicitly[Equiv[A]]
>
> we search for Equiv[A] to be a parameter to the implicitly method. equivA provides it, so we search for Equiv[Seq[String]] for the equivA method. Although Equiv[Seq[String]] dominates Equiv[A], the searches for these types are on different methods, so can't be an infinite expansion. equivSeq provides Equiv[Seq[String]] and it ends there ok.
>
> From one of the diverging tests in Scala's test suite:
>
> implicit def foo2bar(x: Foo)(implicit baz2bar: Baz => Bar): Bar = baz2bar(new Baz)
> implicit def baz2bar(x: Baz)(implicit foo2bar: Foo => Bar): Bar = foo2bar(new Foo)
>
> For baz2bar(new Baz), we search for Foo => Bar for the method baz2bar. foo2bar provides this, so we search for Baz => Bar for the foo2bar method. baz2bar provides Baz => Bar, but needs Foo => Bar. There is an open implicit Foo => Bar, it dominates Foo => Bar, and they are for the same method baz2Bar, so we get a diverging implicit.
>
>
> Attached is a patch that tries out the idea. With this, scalac accepts my code, but still rejects the infinite expansions in the test suite.
>
> Thanks,
> Mark
>
>
That might be a solution, yes. I'll think about it.
Cheers
Thu, 2010-07-29, 14:37
#12
Re: Scala, Spring and List injections
I've now at this in much more depth, so...
Spring 3.0 TypeConverters look very promising, and would allow the existing <list/> notation in spring, they'd also help with binding of page backing models, etc. but they're not without issues:
On 4 July 2010 13:43, Martin Renaud <mr_martin_renaud [at] yahoo [dot] ca> wrote:
--
Kevin Wright
mail/google talk: kev [dot] lee [dot] wright [at] gmail [dot] com
wave: kev [dot] lee [dot] wright [at] googlewave [dot] com
skype: kev.lee.wright
twitter: @thecoda
Spring 3.0 TypeConverters look very promising, and would allow the existing <list/> notation in spring, they'd also help with binding of page backing models, etc. but they're not without issues:
- spring likes to determine type params at runtime via Java reflection, scala prefers compile-time manifests
- The real issue is converting elements in collections, spring will assume everything is a string at first, then convert based on the target type.
- By the time I find I need to know the paramaterised type of a scala collection (when injecting into it), the compiler is long gone and it's too late to use a manifest.
- This stuff *is* in the "Scala Signature", but scala.reflect is still very immature.
- I may be able to use a reflection approach similar to GenericCollectionTypeResolver, but this depends on whether Scala is emitting enough java signature. Nor can I fall back on the old trick of using the type of the first element, as the target collection hasn't even been constructed yet, so has no elements.
- The "TypeDescriptor" mechanism is key to the whole thing and looks to be largely equivalent to manifests, but at run-time. It's also labyrinthine,
- nulls will always be a pain in the proverbial (more so in Spring than in regular Java), so nothing new there then
- Watch this space, there's still hope :)
On 4 July 2010 13:43, Martin Renaud <mr_martin_renaud [at] yahoo [dot] ca> wrote:
Hi,
I'm also convinced of what you are saying. I was hoppoing someone already did it ;-)
I'll give it a try. I think it's possible to provide another namespace for Scala stuff and it would be possible to go with scala:list, scala:map, ... One advantage of using Spring is to be able to get all what comes with it, the bean post-processor for example. If I acheive something, I will post the result here.
Thanks for the pointers.
-Martin
From: Graham Lea <graham [at] grahamlea [dot] com>
To: Martin Renaud <mr_martin_renaud [at] yahoo [dot] ca>
Cc: Scala list <scala [at] listes [dot] epfl [dot] ch>
Sent: Sat, July 3, 2010 8:37:31 AM
Subject: Re: [scala] Scala, Spring and List injections
Hi Martin,
Your problem is rooted in the fact that Spring is written for the Java libraries and so, while it's quite good at coercing things into standard Java types, Spring knows nothing about types from the Scala libraries.
However, having worked quite a lot with Spring, I would be very surprised if there isn't some way that you can extend Spring to do what you want.I would suggest getting the Spring source code, running in debug mode with some code that uses a java.util.List, setting a breakpoint on your List setter, and then reading the Spring code all the way up the stack until you find the class that is coercing the configuration into a java.util.List. Somewhere around that class or the ones above and below it in the stack you will hopefully find an extension point where you can plug in a piece of custom code that will tell Spring how to coerce your configuration into a Scala List. This means you will still have to add some custom code, but you will only need to add it in one place, rather than in every class that you write that requires a list of beans.
Let us know if you find a solution!
Cheers,
Graham.
--
Graham Lea
graham [at] grahamlea [dot] com
On 03/07/2010, at 3:32 AM, Martin Renaud wrote:Again, this solution requires my code to compensate the fact that Spring util:list cannot return something directly Scala either by smart util:list or some automatic conversion.
But as I mention in a previous reply, I found a workaround similar to the one you suggested. I was looking for something more automatic/transparent.
Thanks,
Martin
From: Marcelo Fukushima <takeshi10 [at] gmail [dot] com>
To: Kevin Wright <kev [dot] lee [dot] wright [at] gmail [dot] com>
Cc: Martin Renaud <mr_martin_renaud [at] yahoo [dot] ca>; scala [at] listes [dot] epfl [dot] ch
Sent: Fri, July 2, 2010 1:17:53 PM
Subject: Re: [scala] Scala, Spring and List injections
spring can inject lists as arrays too so you could use that to converto to a scala List - so you dont get 2 Lists in your namespace
On Fri, Jul 2, 2010 at 1:24 PM, Kevin Wright <kev [dot] lee [dot] wright [at] gmail [dot] com> wrote:
it looks like you're trying to inject an object of type java.util.List into a variable of type scala.collection.immutable.List Unsurprisingly... it doesn't work.
You need something like:
import java.{util => ju} ... @BeanProperty var theList : ju.List[MyType] = _
If you import scala.collection.JavaConversion._ as well, then your property will be implicitly pimped with all methods available on scala.collection.mutable.Buffer
If you truly want an immutable list in your instance (which is a scala best practice) and you want to avoid all those messy getter/setter methods that spring demands, then consider using constructor-injection, or creating a dedicated factory bean. Whatever you do though, you must expect spring to provide the relevant java collection type, then convert it yourself.
On 2 July 2010 17:00, Martin Renaud <mr_martin_renaud [at] yahoo [dot] ca> wrote:Hi,
I'm trying to use Scala and Spring together. I'm having difficulties to inject a list created with Spring into a Scala object that expect a List.
I'm looking for the best way to acheive that.
In the Spring context file, I have this:
<util:list id="myBeanList">
<ref bean="myBean1" />
<ref bean="myBean2" />
<ref bean="myBean3" />
</util:list>
When I get this bean in the code, in either of these two ways:
val o1 = context.getBean("myBeanList")
val o2 = context.getBean("myBeanList", classOf[java.util.List[MyType]])
The returned object is, as expected, of type java.util.ArrayList
I want to inject this bean into a Scala object that expects scala.collection.immutable.List - this is the part that does not work.
<bean id="myListContainerBean" class="my.scal.object.Wrapper">
<property name="theList" value="myBeanList" />
</bean>
Where the class is defined as follow
class my.scal.object.Wrapper {
@scala.reflect.BeanProperty
var theList : List[MyType] = List()
}
Now the question… is it possible to do such thing? How? I have the feeling that the JavaConversions package can help with that but I haven't figured out how to use it to do what I want.
I'm using Scala 2.8 RC7 with Spring 3.0.
Thanks,
-Martin
--
Kevin Wright
mail/google talk: kev [dot] lee [dot] wright [at] gmail [dot] com
wave: kev [dot] lee [dot] wright [at] googlewave [dot] com
skype: kev.lee.wright
twitter: @thecoda
--
http://mapsdev.blogspot.com/
Marcelo Takeshi Fukushima
--
Kevin Wright
mail/google talk: kev [dot] lee [dot] wright [at] gmail [dot] com
wave: kev [dot] lee [dot] wright [at] googlewave [dot] com
skype: kev.lee.wright
twitter: @thecoda









On Sun, Jun 27, 2010 at 5:49 AM, Mark Harrah wrote:
> I have a class:
> class A(val options: Seq[String])
>
> I want to define Equiv for A:
> implicit def equivA(implicit seqEq: Equiv[Seq[String]]): Equiv[A] = ...
>
> However, equivA can't be used implicitly because Equiv[Seq[String]] dominates Equiv[A] :
> complexity( Equiv[Seq[String]]) == 6
> and
> complexity( Equiv[A]) == 4
>
> So, the compiler produces a diverging implicit error (and I'm pretty sure this is the right behavior according to the specification).
>
> This is also a problem with enumerations or nested classes:
> class B(val value: SomeEnum.Value)
>
> because
> complexity( Equiv[SomeEnum.Value] ) > complexity( Equiv[B] )
>
> I've only gotten around it by explicitly calling equivA, but this is verbose for larger hierarchies. Or, for large hierarchies I've also done:
> class Wrap(val options: Seq[String])
> and directly defined
> implicit def equivWrap: Equiv[Wrap] = new Equiv[Wrap] {
> def equiv(a: Wrap, b: Wrap) = a.options sameElements b.options
> }
>
> This is not a good solution when defining Arbitrary/Gen instances for ScalaCheck or Format instances for SBinary, however.
>
> So, is this a just an necessary tradeoff to prevent infinite expansion? Or, is it a possible candidate for a spec enhancement? Are there better workarounds?
>
> Thanks,
> Mark
>
I realize it would be useful to do this, but I cannot see a way to
make this work. Essentially, you are asking for a descreasing measure
that is allowed to choose a global ordering of class weights. But
that's not how implicits resolution works.
Cheers