Duncan McGregor | 1 Jun 2011 01:25

Compiling Dynamic code - field access problems

As my first foray into Dynamic Scala land, I thought that I'd try
accessing bean properties via applyDynamic.

My first very rough cut is

trait BeanProperties extends Dynamic {
  def applyDynamic(name: String)(args: Any*) = {
    if (args.length == 0)
      PropertyUtils.getProperty(this, name)
    else
      null
  }
}

so that

val bean = new JTextField("text") with BeanProperties
bean.getText should equal("text")
bean.text should equal("text")

so far so good! But when I try

bean.background should equal(bean.getBackground)

the compiler complains, trying instead to give access to the field
named background rather than synthesizing a method.

variable background in class Component cannot be accessed in
javax.swing.JTextField with BeanPropertiesTest.this.BeanProperties

(Continue reading)

Rodrigo Cano | 1 Jun 2011 02:04
Picon

Re: Compiling Dynamic code - field access problems

On a side note, where can one read about scala.Dynamic?, wasn't it supposed to use a feature of java7? invokedynamic was it?


Thanks

On Tue, May 31, 2011 at 8:25 PM, Duncan McGregor <oneeyedmen-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:
As my first foray into Dynamic Scala land, I thought that I'd try
accessing bean properties via applyDynamic.

My first very rough cut is

trait BeanProperties extends Dynamic {
 def applyDynamic(name: String)(args: Any*) = {
   if (args.length == 0)
     PropertyUtils.getProperty(this, name)
   else
     null
 }
}

so that

val bean = new JTextField("text") with BeanProperties
bean.getText should equal("text")
bean.text should equal("text")

so far so good! But when I try

bean.background should equal(bean.getBackground)

the compiler complains, trying instead to give access to the field
named background rather than synthesizing a method.

variable background in class Component cannot be accessed in
javax.swing.JTextField with BeanPropertiesTest.this.BeanProperties

Is this by design, an oversight, or something that is planned to be
fixed.

Thanks

Duncan McGregor

Daniel Hinojosa | 1 Jun 2011 02:35
Picon

Re: Determining the an abstract type of anonymous object.

Great, thanks David.

Sean Corfield | 1 Jun 2011 04:08
Picon
Gravatar

Scala Days 2011?

With Scala Days being just over a day away, I'm a little surprised at
how little coverage / promotion it seems to have.

http://days2011.scala-lang.org/

The site says it's sold out (which I guess is good) but since I bought
my ticket back in March, I've had zero communication from the
conference and the website has had only a few news items and has
quietly posted a tentative schedule for the two days.

I go to a lot of tech events / conferences and I think Scala Days 2011
qualifies for the most minimal information of any event I've
experienced so far... So I thought I'd post a note here to see if my
experience is typical or whether there is cause for concern? :)

I am looking forward to it - the talks on the schedule look
fascinating - but it is a bit unnerving (for me) to hear so little
about an event, not even much chatter on any of the Scala lists that I
noticed...
--

-- 
Sean A Corfield -- (904) 302-SEAN
An Architect's View -- http://corfield.org/
World Singles, LLC. -- http://worldsingles.com/
Railo Technologies, Inc. -- http://www.getrailo.com/

"Perfection is the enemy of the good."
-- Gustave Flaubert, French realist novelist (1821-1880)

Mark Nelson | 1 Jun 2011 04:29
Picon

Re: Scala Days 2011?

Yeah, I also am surprised at how little information there is.  I expected a final program at the very least :-)   I just flew over 14 hours to attend it, so I hope it is "alright on the night"

On Wed, Jun 1, 2011 at 12:08 PM, Sean Corfield <seancorfield-Re5JQEeQqe8@public.gmane.orgm> wrote:
With Scala Days being just over a day away, I'm a little surprised at
how little coverage / promotion it seems to have.

http://days2011.scala-lang.org/

The site says it's sold out (which I guess is good) but since I bought
my ticket back in March, I've had zero communication from the
conference and the website has had only a few news items and has
quietly posted a tentative schedule for the two days.

I go to a lot of tech events / conferences and I think Scala Days 2011
qualifies for the most minimal information of any event I've
experienced so far... So I thought I'd post a note here to see if my
experience is typical or whether there is cause for concern? :)

I am looking forward to it - the talks on the schedule look
fascinating - but it is a bit unnerving (for me) to hear so little
about an event, not even much chatter on any of the Scala lists that I
noticed...
--
Sean A Corfield -- (904) 302-SEAN
An Architect's View -- http://corfield.org/
World Singles, LLC. -- http://worldsingles.com/
Railo Technologies, Inc. -- http://www.getrailo.com/

"Perfection is the enemy of the good."
-- Gustave Flaubert, French realist novelist (1821-1880)



--
Mark Nelson (mark.x.nelson <at> gmail.com)

Vlad Patryshev | 1 Jun 2011 04:39
Picon
Gravatar

Re: Difference between List and Set variance?

Right, Traversable is covariant, pow is contravariant, but what is Set? Is it, hmm, a functor? Seems like it is not.
Thanks,
-Vlad


On Tue, May 31, 2011 at 12:12 AM, Luc Duponcheel <luc.duponcheel-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
Hi,

is the original question (replacing inheritance with implicits)
not somehow circumventing this variance issue in the first place?

scala> implicit def toPow[X](set: Set[X]): Function[X, Boolean] = new Function[X, Boolean] {
     |  def apply(x: X) = set.contains(x)
     | }
toPow: [X](set: Set[X])(X) => Boolean

scala> implicit def toTrav[X](set: Set[X]): Traversable[X] = new Traversable[X] {
     |  def foreach[U](f: X => U): Unit = set.foreach(f)
     | }
toTrav: [X](set: Set[X])Traversable[X]


maybe I'm plain wrong here

I mean: variance issues will eventually pop up anyway
(and, if yes, then, maybe, it makes sense that type inference would be less strict when implicits are involved than when inheritance is involved (?))

... just a wild thought ...

Luc



On Mon, May 30, 2011 at 9:40 AM, Vlad Patryshev <vpatryshev-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
While it is easy to turn an obviously invariant Set[A] to contravariant [A->Boolean], it might make sense to have something covariant... something like Iterable[+A].

What's interesting is how we can casually disambiguate (between) these two functors. And whether it is feasible to disambiguate in current programming practice. What we call "sets" in Scala are not sets in any scientific sense anyway, are they? What Set[A] means is a "powertype", right?

Thanks,
-Vlad


On Sat, May 28, 2011 at 3:29 PM, Meredith Gregory <lgreg.meredith-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
Dear Scalarazzi,

i was surprised by the following.

Best wishes,

--greg

Welcome to Scala version 2.8.1.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_24).
Type in expressions to have them evaluated.
Type :help for more information.

scala> trait GCG[+A] { def left : List[Either[A,GCG[A]]]; def right : List[Either[A,GCG[A]]] }
defined trait GCG

scala> trait GCG2[+A] { def left : Set[Either[A,GCG[A]]]; def right : Set[Either[A,GCG[A]]] }
<console>:6: error: covariant type A occurs in invariant position in type => Set[Either[A,GCG[A]]] of method left
       trait GCG2[+A] { def left : Set[Either[A,GCG[A]]]; def right : Set[Either[A,GCG[A]]] }
                            ^
<console>:6: error: covariant type A occurs in invariant position in type => Set[Either[A,GCG[A]]] of method right
       trait GCG2[+A] { def left : Set[Either[A,GCG[A]]]; def right : Set[Either[A,GCG[A]]] }
                                                              ^

scala> 

--
L.G. Meredith
Managing Partner
Biosimilarity LLC
7329 39th Ave SW




--
   __~O
  -\ <,
(*)/ (*)

reality goes far beyond imagination


Randall R Schulz | 1 Jun 2011 07:05
Favicon

Re: Where are combinator parser combinators defined?

On Tuesday May 31 2011, Ken McDonald wrote:
> > This sort of thing is exactly why an IDE is called for. If you were
> > using IDEA you'd just ALT-click or select and CTRL-B and you'd be
> > there.
> >
> >
> > Randall Schulz
>
> Doing so takes me to the class file, even though in my project
> settings I have the scala 2.9 source jar attached as a source file.
> (This is IDEA, I should mention.) I've never been able to figure out
> how to get this feature to work, please feel free to give me
> pointers.

You did attach it as the source for the Scala library, right?

It has always worked for me. The only challenge is when the project is 
generated by the IDEA project-generating plug-in for SBT.

> Thanks,
> Ken

Randall Schulz

Adriaan Moors | 1 Jun 2011 07:57
Picon
Picon
Favicon

Re: Re: inconsistent implicit/circularity issue?

I don't think this is related to implicits. The initialisation semantics of objects should explain the behaviour, but I'll leave that to someone else to verify.


Here's my explicit version of exhibit 1, which exhibits the same runtime behaviour:

trait C {val a: List[D]}

class D( val c: C)

object Holder {
        object I extends C {
               val a = List(A)  // can fix runtime error by making it lazy
       }
}

import Holder._

object A extends D(I) {
       val b = c.a.head
       val s = "hello"
}

object Circularity extends App {
       println(I.a.head.s)
}

You might want to check out scalac's -Xcheckinit command-line option and related postings.

cheers
adriaan

On Tue, May 31, 2011 at 3:27 PM, AlanP <alanpog-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
And I get same behavior under both 2.9.0 and 2.9.0-1. It's on github
as an sbt project if it helps: https://github.com/alanpog/Circularity
.
Tks,
Alan

On May 31, 6:30 pm, AlanP <alan... <at> gmail.com> wrote:
> I'm getting a behavior with implicits that does not seem consistent to
> me. It is easier if I show it with code.
>
> The following code in Exhibit 1(below) gets no compiler error, but
> does trigger a NullPointerException at runtime. It can be fixed making
> 'val a' lazy, in object I. So it seems like an order of initialization
> issue, except that it is between different objects that refer to each
> other.
>
> Then the code in Exhibit 2 is the same as Exhibit 1, except that
> object A is being held directly in 'val a', instead of being inside a
> List. In this case I get no runtime error, even without making 'a'
> lazy, it runs as intended.
>
> It seems to me that: 1) I should get the same behavior with Lists or
> direct objects or, 2) I should be able to get a compile-time error on
> Exhibit 1.
>
> If this is the expected behavior, can someone explain me what's the
> reasoning involved?
>
> Thanks,
>
> Alan
>
> ------ Exhibit 1 -------
> trait C {val a: List[D]}
>
> class D(implicit val c: C)
>
> object ImplicitHolder {
>         implicit object I extends C {
>                 val a = List(A)  // can fix runtime error by making it lazy
>         }
>
> }
>
> import ImplicitHolder._
>
> object A extends D {
>         val b = c.a.head
>         val s = "hello"
>
> }
>
> object Circularity extends App {
>         println(I.a.head.s)}
>
> --------------------------------
>
> ------ Exhibit 2 -----------
> trait C {val a: D}
>
> class D(implicit val c: C)
>
> object ImplicitHolder {
>         implicit object I extends C {
>                 val a = A
>         }
>
> }
>
> import ImplicitHolder._
>
> object A extends D {
>         val b = c.a
>         val s = "hello"
>
> }
>
> object Circularity extends App {
>         println(I.a.s)}
>
> --------------------------

Meredith Gregory | 1 Jun 2011 08:02
Picon
Gravatar

Re: Difference between List and Set variance?

Dear Vlad,


i guess the point is that it is not one functor. There are many reasonable functors, each with different characteristics. This is why inheritance gets in the way. It's not flexible enough to model the range of useful contexts. That's why the implicits route has now been proposed by no less than three parties: myself, Kris and Luc.

Best wishes,

--greg

On Tue, May 31, 2011 at 7:39 PM, Vlad Patryshev <vpatryshev <at> gmail.com> wrote:
Right, Traversable is covariant, pow is contravariant, but what is Set? Is it, hmm, a functor? Seems like it is not.
Thanks,
-Vlad



On Tue, May 31, 2011 at 12:12 AM, Luc Duponcheel <luc.duponcheel-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
Hi,

is the original question (replacing inheritance with implicits)
not somehow circumventing this variance issue in the first place?

scala> implicit def toPow[X](set: Set[X]): Function[X, Boolean] = new Function[X, Boolean] {
     |  def apply(x: X) = set.contains(x)
     | }
toPow: [X](set: Set[X])(X) => Boolean

scala> implicit def toTrav[X](set: Set[X]): Traversable[X] = new Traversable[X] {
     |  def foreach[U](f: X => U): Unit = set.foreach(f)
     | }
toTrav: [X](set: Set[X])Traversable[X]


maybe I'm plain wrong here

I mean: variance issues will eventually pop up anyway
(and, if yes, then, maybe, it makes sense that type inference would be less strict when implicits are involved than when inheritance is involved (?))

... just a wild thought ...

Luc



On Mon, May 30, 2011 at 9:40 AM, Vlad Patryshev <vpatryshev-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
While it is easy to turn an obviously invariant Set[A] to contravariant [A->Boolean], it might make sense to have something covariant... something like Iterable[+A].

What's interesting is how we can casually disambiguate (between) these two functors. And whether it is feasible to disambiguate in current programming practice. What we call "sets" in Scala are not sets in any scientific sense anyway, are they? What Set[A] means is a "powertype", right?

Thanks,
-Vlad


On Sat, May 28, 2011 at 3:29 PM, Meredith Gregory <lgreg.meredith-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
Dear Scalarazzi,

i was surprised by the following.

Best wishes,

--greg

Welcome to Scala version 2.8.1.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_24).
Type in expressions to have them evaluated.
Type :help for more information.

scala> trait GCG[+A] { def left : List[Either[A,GCG[A]]]; def right : List[Either[A,GCG[A]]] }
defined trait GCG

scala> trait GCG2[+A] { def left : Set[Either[A,GCG[A]]]; def right : Set[Either[A,GCG[A]]] }
<console>:6: error: covariant type A occurs in invariant position in type => Set[Either[A,GCG[A]]] of method left
       trait GCG2[+A] { def left : Set[Either[A,GCG[A]]]; def right : Set[Either[A,GCG[A]]] }
                            ^
<console>:6: error: covariant type A occurs in invariant position in type => Set[Either[A,GCG[A]]] of method right
       trait GCG2[+A] { def left : Set[Either[A,GCG[A]]]; def right : Set[Either[A,GCG[A]]] }
                                                              ^

scala> 

--
L.G. Meredith
Managing Partner
Biosimilarity LLC
7329 39th Ave SW




--
   __~O
  -\ <,
(*)/ (*)

reality goes far beyond imagination





--
L.G. Meredith
Managing Partner
Biosimilarity LLC
7329 39th Ave SW
Seattle, WA 98136

+1 206.650.3740

http://biosimilarity.blogspot.com

Constantine Plotnikov | 1 Jun 2011 09:11
Picon

Re: Type inference works differently for method types (a : => A)(b : A => B) and (a : => A, b : A => B)

Ok. I undestood how scala compiler works in that case right now. But I still have some questions about it.


1. Why compiler does one list at time? 

2. What are technical problems that prevent inferring dependent argument types within one argument list? 

3. And would it make sense to make a compiler behavior a bit more uniform?

As side note, Scala plugin for IDEA Community works ok with such code and successfully infer types (it fails on other pieces of valid code, but it is a separate issue). And if we look at the fucntion call sites, m1 m m2 call sites have the same information avialable, so from user point of view, the difference is purely cosmetic. 

Thanks,
Constantine

On Mon, May 30, 2011 at 1:00 AM, Kevin Wright <kev.lee.wright <at> gmail.com> wrote:
Type inference proceeds left-to-right, one parameter _list_ at a time.

This means that, for m2, the type A is not yet fixed at the time it encounters the `b` argument.  So it can only typecheck if you explicitly state what type the wildcard in `println(_)` should be.

For m1, the type A is fully determined by the time it encounters `b`, this being in a distinct parameter list.



On 29 May 2011 21:40, Constantine <constantine.plotnikov <at> gmail.com> wrote:
Hello All,

I have a strange complier puzzle on hands (2.9.0) that do not
understand. The case where I have encountered it is a bit more
complex, but I was able to simplify the problem to the case below.

class ParamTest {

 def m1[A,B](a : => A)(b : A => B) : B = b(a)

 def m2[A,B](a : => A, b : A => B) : B = m1(a)(b)

 def test1() {
  m1(1+2)(println(_)) // compiles successfully
  m2(1+2, println(_ : Int)) // compiles successfully
  m2(1+2, println(_)) // does not compile
 }
}

The methods m1 and m2 in the sample are logically equivalent and the
type inference should be able to handle them with the same level of
success. However for m2, the type inference fails and requires type
annotation for the argument.

What is a reason for it?

I think that it should not fail. If compiler is able to handle m1,
there should be no problem with handling m2. The problem looks like
language inconsistency.

Thanks,
Constantine



--
Kevin Wright

gtalk / msn : kev.lee.wright <at> gmail.com
mail: kevin.wright-4U8lxy6Hl1K35wUkaLeaMEEOCMrvLtNR@public.gmane.org
vibe / skype: kev.lee.wright
quora: http://www.quora.com/Kevin-Wright
twitter: <at> thecoda

"My point today is that, if we wish to count lines of code, we should not regard them as "lines produced" but as "lines spent": the current conventional wisdom is so foolish as to book that count on the wrong side of the ledger" ~ Dijkstra



Gmane