Matt Hellige | 1 Aug 21:40 2008
Picon

compiler weirdness with crazy types

Sorry for the vague subject, I'm not sure how else to describe this.

This is based on some thinking about the following blog post:
  http://michid.wordpress.com/2008/07/30/meta-programming-with-scala-part-ii-multiplication/#comment-69

Consider:
  object Church {
    abstract class Zero
    abstract class Succ[T]

    type _0[s[_], z] = z
    type _1[s[_], z] = s[z]
    type _2[s[_], z] = s[s[z]]
    type _3[s[_], z] = s[s[s[z]]]
    type _4[s[_], z] = s[s[s[s[z]]]]

    trait apply1[t[_[_],_],u[_]] {
      type It[a] = t[u,a]
    }

    type mul[m[s[_], z], n[s[_], z], s[_], z] = m[apply1[n,s]#It, z]
    type x[m[s[_], z], n[s[_], z]] = mul[m, n, Succ, Zero]

    type three = _3 x _1
    //type four = _2 x _2
    type six = _2 x _3
    type eight = _4 x _2
  }

This code compiles fine. We can even use the defined types! But if we
(Continue reading)

Matt Hellige | 1 Aug 21:55 2008
Picon

Re: compiler weirdness with crazy types

On Fri, Aug 1, 2008 at 2:40 PM, Matt Hellige <matt@...> wrote:
> This code compiles fine. We can even use the defined types! But if we
> uncomment the definition of "four":
>  type four = _2 x _2
> then we get the following:

More generally, it doesn't work for squares:
 type one = _1 x _1
 type nine = _3 x _3
These have the same problem. Does anyone see why?

Matt

--

-- 
Matt Hellige / matt@...
http://matt.immute.net

Matt Hellige | 1 Aug 22:09 2008
Picon

Re: compiler weirdness with crazy types

On Fri, Aug 1, 2008 at 2:55 PM, Matt Hellige <matt@...> wrote:
> On Fri, Aug 1, 2008 at 2:40 PM, Matt Hellige <matt@...> wrote:
>> This code compiles fine. We can even use the defined types! But if we
>> uncomment the definition of "four":
>>  type four = _2 x _2
>> then we get the following:
>
> More generally, it doesn't work for squares:
>  type one = _1 x _1
>  type nine = _3 x _3
> These have the same problem. Does anyone see why?
>

Ok. Sorry to keep emailing myself, but I think I see what's happening.
The expansion of a type like _1 x _1 goes as follows:
  _1 x _1
    -> mul[_1, _1, Succ, Zero]
    -> _1[apply1[_1,Succ]#It, Zero]
    -> apply1[_1,Succ]#It[Zero]
    -> _1[Succ,Zero]
    -> Succ[Zero]

The third line seems to be the problem, as we can see by writing
  type test = _1[apply1[_1,Succ]#It, Zero]
which produces the same error.

This would lead us to think maybe we can avoid the problem just by
making more aliases. Indeed we can. The following works fine:
  type _1x[s[_], z] = s[z]
  type _2x[s[_], z] = s[s[z]]
(Continue reading)

Josh Joy | 2 Aug 01:05 2008
Picon

scala + spring integration

Hi,

I have a spring app and would like to use scala for some portions.

I would like to inject some spring Java beans into a scala class, and then inject that scala class into some of my Java spring beans.

Any examples of how to do this?

Thanks,
Josh
David MacIver | 2 Aug 01:18 2008
Picon

Re: scala + spring integration

It should Just Work. As far as reflection is concerned, Scala objects
are just the same as Java objects.

One thing you might find useful is the  <at> BeanProperty annotation, which
you can add to vars to make them into javabean properties with getters
and setters.

On Sat, Aug 2, 2008 at 12:05 AM, Josh Joy <joshjdevl@...> wrote:
> Hi,
>
> I have a spring app and would like to use scala for some portions.
>
> I would like to inject some spring Java beans into a scala class, and then
> inject that scala class into some of my Java spring beans.
>
> Any examples of how to do this?
>
> Thanks,
> Josh
>

David MacIver | 2 Aug 01:40 2008
Picon

Re: scala + spring integration

On Sat, Aug 2, 2008 at 12:34 AM, Josh Joy <joshjdevl@...> wrote:
> Sorry, I'm still kind of new to this...are there any code examples I can
> look at?

Code samples of what? Using BeanProperty? There's an example in the
scaladoc for it.
http://www.scala-lang.org/docu/files/api/scala/reflect/BeanProperty.html

For specifically using Scala with Spring, no idea. There's not a lot
of point in writing a specific example of that - if you know how to
use Scala and you know how to use Spring, it just works as normal.

> Where do I import BeanProperty from?

scala.reflect.BeanProperty

> How do I use an interface for my Scala class?

Java interfaces are visible as traits in Scala.You use them like you
would any other trait.

David MacIver | 2 Aug 02:00 2008
Picon

Re: scala + spring integration

You're on the newbie mailing list, but you're asking incredibly vague
questions. They're not likely to get you useful answers anywhere you
ask them.

If you want to learn Scala, I'd recommend checking out the various
links on the website, reading some blogs and writing some code. Maybe
buy the staircase book. Asking on a mailing list "Could someone give
me a link to some code samples for how to use Scala with Spring" isn't
likely to be a productive activity if you don't know how to write
Scala.

On Sat, Aug 2, 2008 at 12:47 AM, Josh Joy <joshjdevl@...> wrote:
> Sorry, lemme say this...
>
> I have no idea how to use scala, and I dont know what normal or wrong or
> right is...thanks. maybe the impression I get is that I need to find a
> newbie mailing list.
>
>
> For specifically using Scala with Spring, no idea. There's not a lot
> of point in writing a specific example of that - if you know how to
> use Scala and you know how to use Spring, it just works as normal.
>
>
>
> On Fri, Aug 1, 2008 at 6:40 PM, David MacIver <david.maciver@...>
> wrote:
>>
>> On Sat, Aug 2, 2008 at 12:34 AM, Josh Joy <joshjdevl@...> wrote:
>> > Sorry, I'm still kind of new to this...are there any code examples I can
>> > look at?
>>
>> Code samples of what? Using BeanProperty? There's an example in the
>> scaladoc for it.
>> http://www.scala-lang.org/docu/files/api/scala/reflect/BeanProperty.html
>>
>> For specifically using Scala with Spring, no idea. There's not a lot
>> of point in writing a specific example of that - if you know how to
>> use Scala and you know how to use Spring, it just works as normal.
>>
>> > Where do I import BeanProperty from?
>>
>> scala.reflect.BeanProperty
>>
>> > How do I use an interface for my Scala class?
>>
>> Java interfaces are visible as traits in Scala.You use them like you
>> would any other trait.
>
>

Jorge Ortiz | 2 Aug 02:26 2008
Picon

Re: scala + spring integration

Hey Josh,

You might find this blog post useful:

http://javajmc.blogspot.com/2008/02/simple-scala-spring-and-webworkstruts2.html

If you're just learning Scala, there are several series introducing
Scala to Java programmers:

1) Daniel Spiewak's Scala for Java Refugees
http://www.codecommit.com/blog/scala/roundup-scala-for-java-refugees
2) Ted Neward's The Busy Java Developer's Guide to Scala
http://www.ibm.com/developerworks/views/java/libraryview.jsp?search_by=scala+neward
3) David Pollak's 5 Things a Java Developer Needs to Know About Scala
http://blog.lostlake.org/index.php?/archives/26-5-Things-a-Java-developer-needs-to-know-about-Scala.html
4) Alex Blewitt's Introduction to Scala
http://blog.lostlake.org/index.php?/archives/26-5-Things-a-Java-developer-needs-to-know-about-Scala.html

The "Programming in Scala" book is probably the best resource, if
you're willing to pay for it.

--j

On Fri, Aug 1, 2008 at 4:05 PM, Josh Joy <joshjdevl@...> wrote:
> Hi,
>
> I have a spring app and would like to use scala for some portions.
>
> I would like to inject some spring Java beans into a scala class, and then
> inject that scala class into some of my Java spring beans.
>
> Any examples of how to do this?
>
> Thanks,
> Josh
>

vkelman | 2 Aug 09:03 2008
Picon

What's wrong with this implicit conversion method?


Sorry if this newbie question is too dumb...
In " http://www.artima.com/shop/programming_in_scala Programming in Scala "
Martin Odersky shows how to define operators in Scala (chapter 6). He
creates a simple class Rational and, among other things adds the following
operator:

  // This implicit conversion method allows 2 * r
  implicit def intToRational(x: Int) = new Rational(x)

However, it doesn't work: I'm getting error:

Description	Resource	Path	Location	Type
overloaded method value * with alternatives (Double)Double <and>
(Float)Float <and> (Long)Long <and> (Int)Int <and> (Char)Int <and>
(Short)Int <and> (Byte)Int cannot be applied to (rational.Rational)
MyApp.scala	chp6Rational/src/rational	Unknown	Scala Problem

Why doesn't it apply implicit def intToRational method here?

-----
Vladimir Kelman
http://pro-thoughts.blogspot.com/
--

-- 
View this message in context: http://www.nabble.com/What%27s-wrong-with-this-implicit-conversion-method--tp18786757p18786757.html
Sent from the Scala - User mailing list archive at Nabble.com.

Garrett Rowe | 2 Aug 10:45 2008
Picon

Function currying; ExceptionInInitializerError

I don't understand why this code works as expected:

object O {
   val g: Int => Int => String = {
      x: Int =>
      y: Int =>
         "FOO"
   }

   val f: Int => String = {
      g(0)
   }

   def main(args: Array[String]): Unit = {
       println(f(0))
   }
}

Output:
FOO

But this compiles, but fails with an ExceptionInInitializerError

object O {
   val f: Int => String = {
      g(0)
   }

   val g: Int => Int => String = {
      x: Int =>
      y: Int =>
         "FOO"
   }

   def main(args: Array[String]): Unit = {
       println(f(0))
   }
}

java.lang.ExceptionInInitializerError
        at O.main(O.scala)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at scala.tools.nsc.ObjectRunner$$anonfun$run$1.apply(ObjectRunner.scala:75)
        at scala.tools.nsc.ObjectRunner$.withContextClassLoader(ObjectRunner.scala:49)
        at scala.tools.nsc.ObjectRunner$.run(ObjectRunner.scala:74)
        at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:161)
        at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)
Caused by: java.lang.NullPointerException
        at O$.<init>(O.scala:5)
        at O$.<clinit>(O.scala)
        ... 10 more

I thought I might have missed some rule about vals being initialized
in the order they are declared, but that wouldn't explain why this
also works:

object O {
   val f: Int => String = {
      x: Int =>
        g(0)(x)
   }

   val g: Int => Int => String = {
      x: Int =>
      y: Int =>
         "FOO"
   }

   def main(args: Array[String]): Unit = {
       println(f(0))
   }
}

Output:
 FOO

Can someone please explain this behavior?


Gmane