You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@pivot.apache.org by Bojan Vučinić <bo...@ma-cad.com> on 2010/11/23 19:05:12 UTC

Scala

Hi All,

If you are using Scala for your code then you are obliged to write 
listeners in the following way:

printButton.getButtonPressListeners.add(new ButtonPressListener {
      override def buttonPressed(b: Button) {
      	print
      }
   })

I've tried to simplify this by defining the following method:

   def pressedDo (aB: PushButton, action: Unit) = {
     aB.getButtonPressListeners.add(new ButtonPressListener {
         override def buttonPressed(b: PushButton) { action }})
   }

and than invoking the method in this way (example)

     pressedDo(printButton, print)

however, this results in a compiler error:

error: object creation impossible, since method buttonPressed in trait ButtonPressListener of type (x$1: org.apache.pivot.wtk.Button)Unit is not defined
     aB.getButtonPressListeners.add(new ButtonPressListener {

I'm not a programming language specialist, and in addition a Scala 
novice, but I'm puzzled why putting boilerplate code in a separate 
method is not working??

Best regards,
Bojan


*Dr. Bojan Vučinić *
President, Ma-CAD <http://www.ma-cad.com/>
bojan.vucinic@ma-cad.com <ma...@ma-cad.com>
IM: bvucinic (Skype)
*http://fr.linkedin.com/in/bvucinic*

/Maintenance Concept Adjustment & Design/ 	31, rue Chanzy
Paris, Ile de France 75011 France
Work: +33 (6) 14 15 36 70
Mobile: +33 (6) 14 15 36 70
Fax: +33 (1) 53 01 38 28
See who we know in common <http://www.linkedin.com/e/wwk/4839994/> 	Want 
a signature like this? <http://www.linkedin.com/e/sig/4839994/>



Re: Scala

Posted by Greg Brown <gk...@mac.com>.
I have heard from other projects that a central location for project-related discussion works best. I tend to agree, so I think that the existing user and dev lists would be the best place for these discussions. Of course, non-Pivot related Scala discussions are best held elsewhere, on the Scala lists.

On Nov 23, 2010, at 6:53 PM, Sandro Martini wrote:

> Hi Greg,
> I was wondering if it would be better to move them to the dedicated
> subproject, but as you I don't see reason to move outside ... so this
> question was just for a quick check between us.
> 
> So happy Pivot-Scala to all, from here :-) .
> 
> Bye


Re: Scala

Posted by Sandro Martini <sa...@gmail.com>.
Hi Greg,
I was wondering if it would be better to move them to the dedicated
subproject, but as you I don't see reason to move outside ... so this
question was just for a quick check between us.

So happy Pivot-Scala to all, from here :-) .

Bye

Re: Scala

Posted by Greg Brown <gk...@mac.com>.
I don't see any reason we shouldn't discuss Pivot-related Scala issues here.

On Nov 23, 2010, at 6:05 PM, Sandro Martini wrote:

> Hi to all, and sorry to have joined this discussion only now ...
> 
> Clint, thank you very much for sharing with us so much Scala tips :-) .
> 
> Greg, I think we have found some idea for our first not-trivial Scala
> sample, for pivot-scala, right ?
> I have some other small ideas for it, only time is so little ... in
> any case, as always, suggestions etc are welcome.
> For Pivot-Scala related things, do you think it will be better to
> discuss here, or opening a forum in Pivot-Scala ?
> 
> Bye,
> Sandro


Re: Scala

Posted by Sandro Martini <sa...@gmail.com>.
Hi to all, and sorry to have joined this discussion only now ...

Clint, thank you very much for sharing with us so much Scala tips :-) .

Greg, I think we have found some idea for our first not-trivial Scala
sample, for pivot-scala, right ?
I have some other small ideas for it, only time is so little ... in
any case, as always, suggestions etc are welcome.
For Pivot-Scala related things, do you think it will be better to
discuss here, or opening a forum in Pivot-Scala ?

Bye,
Sandro

Re: Scala

Posted by Clint Gilbert <cl...@hms.harvard.edu>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Yes, params like a: => Unit mean, in effect 'evaluate a only when it's
referenced, not when it's first passed to a method'.

The implementation is a function, so the notation is really a special
case of the function literal syntax.

if you have

def f1(a: => Unit) = a

it's almost the same as

//a is a funtion that takes an empty param list and returns Unit
def f2(a: () => Unit) = a

in the sense that both times, 'a' in the bodies of the defs means
'invoke the function a'.

But with the first form, you can do cool stuff like

f1 {
  val x = ...
  val y = stuff(x)
  moreStuff(y)
}

I'm gald it worked.  Scala is a fun language!

Bojan Vučinić wrote:
> Well done!
> 
> It's now working as expected.
> So if I understand correctly the => in the parameter declaration defers 
> function's invocation to a later stage?
> 
> Anyway, thanks for the shared insight with Scala.
> 
> 
> Clint Gilbert said the following on 23/11/2010 21:46:
> Bojan Vu ini wrote:
>>>> Clint, Greg,
>>>> Thanks for taking the time to look into this.
>>>> Here is the code that works:
>>>>    def initButtons = {
>>>> ...
>>>>       pressedDo(printButton, print)
>>>> ...
>>>>    }
>>>>
>>>>     def pressedDo (aB: PushButton, action: Unit) = {
>>>>       aB.getButtonPressListeners.add(new ButtonPressListener {
>>>>           override def buttonPressed(b: Button): Unit = action })
>>>>
> 'print' is a function or method, right?
> 
> Try changing the signature of predded do from
> 
> def pressedDo (aB: PushButton, action: Unit)
> 
> to
> 
> def pressedDo (aB: PushButton, action: =>  Unit)
> 
> When you call the first version,
> 
> pressedDo(printButton, print)
> 
> print is actually invoked right then, instead of when the button is
> pressed.  The semantics for parameters of this type are the same as
> Java's.  The second version
> 
> def pressedDo (aB: PushButton, action: =>  Unit)
> 
> means that the second param is an anonymous function that takes no
> parameters and returns Unit.  Calling this version of pressedDo with
> 
> pressedDo(printButton, print)
> 
> correctly makes that function - in this case a function that takes no
> params, calls 'print', and returns Unit - get called when the button is
> pressed.
> 
> Now, when initButtons, and in turn pressedDo, is called, the window var
> should be non-null, so the error you're seeing is weird.  But the first
> version of pressedDo, where print isn't called when the button is
> pressed, is almost certainly not what you want.  Fix that, and see what
> happens.
> 
>>>> I get the error that display is null??
>>>>
>>>> It is really strange.
>>>>
>>>>
>>>>
>>>> Clint Gilbert said the following on 23/11/2010 20:37:
>>>> Actually, the type of b is the problem:
>>>>
>>>> override def buttonPressed(b: Button): Unit = action
>>>>
>>>> or even
>>>>
>>>> override def buttonPressed(b: Button) = action
>>>>
>>>> instead of
>>>>
>>>> override def buttonPressed(b: PushButton): Unit = action
>>>>
>>>> is what you want.
>>>>
>>>> Clint Gilbert wrote:
>>>>>>> This is getting off-topic, but it's something I actually know a tiny bit
>>>>>>> about.
>>>>>>>
>>>>>>> The compiler is complaining (I think) because you're not actually
>>>>>>> overriding buttonPressed.  This can happen when type inference goes a
>>>>>>> little astray, especially with methods that return Unit.  Naming
>>>>>>> buttonPressed's param 'b' shouldn't be a problem.  I'd try explicitly
>>>>>>> specifying the return type, like
>>>>>>>
>>>>>>> def pressedDo (aB: PushButton, action: Unit) = {
>>>>>>>     aB.getButtonPressListeners.add(new ButtonPressListener {
>>>>>>>       override def buttonPressed(b: PushButton): Unit = action
>>>>>>>     })
>>>>>>> }
>>>>>>>
>>>>>>> There are also inference rule that take effect regarding the = in
>>>>>>>
>>>>>>> def foo(...) = { }
>>>>>>>
>>>>>>> vs def foo(...) { }
>>>>>>>
>>>>>>> The latter (I believe) is assumed to return Unit, but I always add the =
>>>>>>> just to be sure.  (/Programming Scala/ by Odersky, Venners, and Spoon
>>>>>>> explains things like this very, very well.)
>>>>>>>
>>>>>>> Additionally, you probably want something like this:
>>>>>>>
>>>>>>> def pressedDo (aB: PushButton, action: =>   Unit) = {
>>>>>>>     aB.getButtonPressListeners.add(new ButtonPressListener {
>>>>>>>       override def buttonPressed(b: PushButton) = action
>>>>>>>     })
>>>>>>> }
>>>>>>>
>>>>>>> Note the new type of the 'action' param.  It's now "a function that
>>>>>>> takes no params and returns Unit", instead of an actual value of type
>>>>>>> Unit.  This is an easy way to have lazily-evaluated parameters.
>>>>>>>
>>>>>>> Due to some neat syntactic sugar for writing anonymous functions of this
>>>>>>> type, if you split the parameter list for pressedDo, like
>>>>>>>
>>>>>>> def pressedDo (aB: PushButton)(action: =>   Unit) = {
>>>>>>>     aB.getButtonPressListeners.add(new ButtonPressListener {
>>>>>>>       override def buttonPressed(b: PushButton) = action
>>>>>>>     })
>>>>>>> }
>>>>>>>
>>>>>>> You could call it like:
>>>>>>>
>>>>>>> pressedDo(someButton) {
>>>>>>>     val foo = new Foo
>>>>>>>
>>>>>>>     someOtherThing.doStuff(foo)
>>>>>>>
>>>>>>>     //etc
>>>>>>> }
>>>>>>>
>>>>>>> Here the stuff in the braces becomes an anonymous function that gets
>>>>>>> passed to pressedDo.
>>>>>>>
>>>>>>> It's also possible to use implicit conversions (the perhaps
>>>>>>> tastelessly-named "Pimp-My-Library" pattern[1]) to "add" the pressedDo
>>>>>>> method to the Button class.  Well, kind of, not really to the Button
>>>>>>> class, but combined with the above technique, you could make a call like
>>>>>>>
>>>>>>> val someButton: Button = ...
>>>>>>>
>>>>>>> someButton pressed {
>>>>>>>     val foo = new Foo
>>>>>>>
>>>>>>>     someOtherThing.doStuff(foo)
>>>>>>> }
>>>>>>>
>>>>>>> which is starting to look smooth.  Implicits can get pretty black-magic
>>>>>>> though, so use them with care and restraint if you do.
>>>>>>>
>>>>>>> [1]: http://scala.sygneca.com/patterns/pimp-my-library
>>>>>>>
>>>>>>> Bojan Vu ini wrote:
>>>>>>>> Hi All,
>>>>>>>> If you are using Scala for your code then you are obliged to write listeners in the following way:
>>>>>>>> printButton.getButtonPressListeners.add(new ButtonPressListener {
>>>>>>>>        override def buttonPressed(b: Button) {
>>>>>>>>           print
>>>>>>>>        }
>>>>>>>>     })
>>>>>>>> I've tried to simplify this by defining the following method:
>>>>>>>>     def pressedDo (aB: PushButton, action: Unit) = {
>>>>>>>>       aB.getButtonPressListeners.add(new ButtonPressListener {
>>>>>>>>           override def buttonPressed(b: PushButton) { action }})
>>>>>>>>     }
>>>>>>>> and than invoking the method in this way (example)
>>>>>>>>       pressedDo(printButton, print)
>>>>>>>> however, this results in a compiler error:
>>>>>>>> error: object creation impossible, since method buttonPressed in trait ButtonPressListener of type (x$1: org.apache.pivot.wtk.Button)Unit is not defined
>>>>>>>>       aB.getButtonPressListeners.add(new ButtonPressListener {
>>>>>>>> I'm not a programming language specialist, and in addition a Scala novice, but I'm puzzled why putting boilerplate code in a separate method is not working??
>>>>>>>> Best regards,
>>>>>>>> Bojan
>>>>>>>> Dr. Bojan Vu ini
>>>>>>>> President, Ma-CAD<http://www.ma-cad.com/>
>>>>>>>> bojan.vucinic@ma-cad.com<ma...@ma-cad.com>
>>>>>>>> IM: bvucinic (Skype)
>>>>>>>> http://fr.linkedin.com/in/bvucinic
>>>>>>>> Maintenance Concept Adjustment&   Design 31, rue Chanzy
>>>>>>>> Paris, Ile de France 75011 France
>>>>>>>> Work: +33 (6) 14 15 36 70
>>>>>>>> Mobile: +33 (6) 14 15 36 70
>>>>>>>> Fax: +33 (1) 53 01 38 28
>>>>>>>> See who we know in common<http://www.linkedin.com/e/wwk/4839994/>         Want a signature like this?<http://www.linkedin.com/e/sig/4839994/>
>>
>>

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkzsLx8ACgkQ5IyIbnMUeTvCLwCdEe/4Lp+HODLNBicXjFez6bp2
rRIAnRRi9DprdAk5/IqlRLlU0/Wrl/i0
=V9kO
-----END PGP SIGNATURE-----

Re: Scala

Posted by Bojan Vučinić <bo...@ma-cad.com>.
Well done!

It's now working as expected.
So if I understand correctly the => in the parameter declaration defers 
function's invocation to a later stage?

Anyway, thanks for the shared insight with Scala.


Clint Gilbert said the following on 23/11/2010 21:46:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Bojan Vučinić wrote:
>> Clint, Greg,
>> Thanks for taking the time to look into this.
>> Here is the code that works:
>>    def initButtons = {
>> ...
>>       pressedDo(printButton, print)
>> ...
>>    }
>>
>>     def pressedDo (aB: PushButton, action: Unit) = {
>>       aB.getButtonPressListeners.add(new ButtonPressListener {
>>           override def buttonPressed(b: Button): Unit = action })
>>
> 'print' is a function or method, right?
>
> Try changing the signature of predded do from
>
> def pressedDo (aB: PushButton, action: Unit)
>
> to
>
> def pressedDo (aB: PushButton, action: =>  Unit)
>
> When you call the first version,
>
> pressedDo(printButton, print)
>
> print is actually invoked right then, instead of when the button is
> pressed.  The semantics for parameters of this type are the same as
> Java's.  The second version
>
> def pressedDo (aB: PushButton, action: =>  Unit)
>
> means that the second param is an anonymous function that takes no
> parameters and returns Unit.  Calling this version of pressedDo with
>
> pressedDo(printButton, print)
>
> correctly makes that function - in this case a function that takes no
> params, calls 'print', and returns Unit - get called when the button is
> pressed.
>
> Now, when initButtons, and in turn pressedDo, is called, the window var
> should be non-null, so the error you're seeing is weird.  But the first
> version of pressedDo, where print isn't called when the button is
> pressed, is almost certainly not what you want.  Fix that, and see what
> happens.
>
>> I get the error that display is null??
>>
>> It is really strange.
>>
>>
>>
>> Clint Gilbert said the following on 23/11/2010 20:37:
>> Actually, the type of b is the problem:
>>
>> override def buttonPressed(b: Button): Unit = action
>>
>> or even
>>
>> override def buttonPressed(b: Button) = action
>>
>> instead of
>>
>> override def buttonPressed(b: PushButton): Unit = action
>>
>> is what you want.
>>
>> Clint Gilbert wrote:
>>>>> This is getting off-topic, but it's something I actually know a tiny bit
>>>>> about.
>>>>>
>>>>> The compiler is complaining (I think) because you're not actually
>>>>> overriding buttonPressed.  This can happen when type inference goes a
>>>>> little astray, especially with methods that return Unit.  Naming
>>>>> buttonPressed's param 'b' shouldn't be a problem.  I'd try explicitly
>>>>> specifying the return type, like
>>>>>
>>>>> def pressedDo (aB: PushButton, action: Unit) = {
>>>>>     aB.getButtonPressListeners.add(new ButtonPressListener {
>>>>>       override def buttonPressed(b: PushButton): Unit = action
>>>>>     })
>>>>> }
>>>>>
>>>>> There are also inference rule that take effect regarding the = in
>>>>>
>>>>> def foo(...) = { }
>>>>>
>>>>> vs def foo(...) { }
>>>>>
>>>>> The latter (I believe) is assumed to return Unit, but I always add the =
>>>>> just to be sure.  (/Programming Scala/ by Odersky, Venners, and Spoon
>>>>> explains things like this very, very well.)
>>>>>
>>>>> Additionally, you probably want something like this:
>>>>>
>>>>> def pressedDo (aB: PushButton, action: =>   Unit) = {
>>>>>     aB.getButtonPressListeners.add(new ButtonPressListener {
>>>>>       override def buttonPressed(b: PushButton) = action
>>>>>     })
>>>>> }
>>>>>
>>>>> Note the new type of the 'action' param.  It's now "a function that
>>>>> takes no params and returns Unit", instead of an actual value of type
>>>>> Unit.  This is an easy way to have lazily-evaluated parameters.
>>>>>
>>>>> Due to some neat syntactic sugar for writing anonymous functions of this
>>>>> type, if you split the parameter list for pressedDo, like
>>>>>
>>>>> def pressedDo (aB: PushButton)(action: =>   Unit) = {
>>>>>     aB.getButtonPressListeners.add(new ButtonPressListener {
>>>>>       override def buttonPressed(b: PushButton) = action
>>>>>     })
>>>>> }
>>>>>
>>>>> You could call it like:
>>>>>
>>>>> pressedDo(someButton) {
>>>>>     val foo = new Foo
>>>>>
>>>>>     someOtherThing.doStuff(foo)
>>>>>
>>>>>     //etc
>>>>> }
>>>>>
>>>>> Here the stuff in the braces becomes an anonymous function that gets
>>>>> passed to pressedDo.
>>>>>
>>>>> It's also possible to use implicit conversions (the perhaps
>>>>> tastelessly-named "Pimp-My-Library" pattern[1]) to "add" the pressedDo
>>>>> method to the Button class.  Well, kind of, not really to the Button
>>>>> class, but combined with the above technique, you could make a call like
>>>>>
>>>>> val someButton: Button = ...
>>>>>
>>>>> someButton pressed {
>>>>>     val foo = new Foo
>>>>>
>>>>>     someOtherThing.doStuff(foo)
>>>>> }
>>>>>
>>>>> which is starting to look smooth.  Implicits can get pretty black-magic
>>>>> though, so use them with care and restraint if you do.
>>>>>
>>>>> [1]: http://scala.sygneca.com/patterns/pimp-my-library
>>>>>
>>>>> Bojan Vu ini wrote:
>>>>>> Hi All,
>>>>>> If you are using Scala for your code then you are obliged to write listeners in the following way:
>>>>>> printButton.getButtonPressListeners.add(new ButtonPressListener {
>>>>>>        override def buttonPressed(b: Button) {
>>>>>>           print
>>>>>>        }
>>>>>>     })
>>>>>> I've tried to simplify this by defining the following method:
>>>>>>     def pressedDo (aB: PushButton, action: Unit) = {
>>>>>>       aB.getButtonPressListeners.add(new ButtonPressListener {
>>>>>>           override def buttonPressed(b: PushButton) { action }})
>>>>>>     }
>>>>>> and than invoking the method in this way (example)
>>>>>>       pressedDo(printButton, print)
>>>>>> however, this results in a compiler error:
>>>>>> error: object creation impossible, since method buttonPressed in trait ButtonPressListener of type (x$1: org.apache.pivot.wtk.Button)Unit is not defined
>>>>>>       aB.getButtonPressListeners.add(new ButtonPressListener {
>>>>>> I'm not a programming language specialist, and in addition a Scala novice, but I'm puzzled why putting boilerplate code in a separate method is not working??
>>>>>> Best regards,
>>>>>> Bojan
>>>>>> Dr. Bojan Vu ini
>>>>>> President, Ma-CAD<http://www.ma-cad.com/>
>>>>>> bojan.vucinic@ma-cad.com<ma...@ma-cad.com>
>>>>>> IM: bvucinic (Skype)
>>>>>> http://fr.linkedin.com/in/bvucinic
>>>>>> Maintenance Concept Adjustment&   Design 31, rue Chanzy
>>>>>> Paris, Ile de France 75011 France
>>>>>> Work: +33 (6) 14 15 36 70
>>>>>> Mobile: +33 (6) 14 15 36 70
>>>>>> Fax: +33 (1) 53 01 38 28
>>>>>> See who we know in common<http://www.linkedin.com/e/wwk/4839994/>         Want a signature like this?<http://www.linkedin.com/e/sig/4839994/>
>>>
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.9 (GNU/Linux)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
>
> iEYEARECAAYFAkzsKEEACgkQ5IyIbnMUeTvZuACeNSx8S8WlJuKapvMUifzEyPFi
> fuYAn2rDRNUzKa35nM9rbLXEnrj/ESKU
> =uLdg
> -----END PGP SIGNATURE-----
>
>

Re: Scala

Posted by Clint Gilbert <cl...@hms.harvard.edu>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Bojan Vučinić wrote:
> Clint, Greg,
> Thanks for taking the time to look into this.
> Here is the code that works:
>   def initButtons = {
> ...
>      pressedDo(printButton, print)
> ...
>   }
> 
>    def pressedDo (aB: PushButton, action: Unit) = {
>      aB.getButtonPressListeners.add(new ButtonPressListener {
>          override def buttonPressed(b: Button): Unit = action })
> 

'print' is a function or method, right?

Try changing the signature of predded do from

def pressedDo (aB: PushButton, action: Unit)

to

def pressedDo (aB: PushButton, action: => Unit)

When you call the first version,

pressedDo(printButton, print)

print is actually invoked right then, instead of when the button is
pressed.  The semantics for parameters of this type are the same as
Java's.  The second version

def pressedDo (aB: PushButton, action: => Unit)

means that the second param is an anonymous function that takes no
parameters and returns Unit.  Calling this version of pressedDo with

pressedDo(printButton, print)

correctly makes that function - in this case a function that takes no
params, calls 'print', and returns Unit - get called when the button is
pressed.

Now, when initButtons, and in turn pressedDo, is called, the window var
should be non-null, so the error you're seeing is weird.  But the first
version of pressedDo, where print isn't called when the button is
pressed, is almost certainly not what you want.  Fix that, and see what
happens.

> I get the error that display is null??
> 
> It is really strange.
> 
> 
> 
> Clint Gilbert said the following on 23/11/2010 20:37:
> Actually, the type of b is the problem:
> 
> override def buttonPressed(b: Button): Unit = action
> 
> or even
> 
> override def buttonPressed(b: Button) = action
> 
> instead of
> 
> override def buttonPressed(b: PushButton): Unit = action
> 
> is what you want.
> 
> Clint Gilbert wrote:
>>>> This is getting off-topic, but it's something I actually know a tiny bit
>>>> about.
>>>>
>>>> The compiler is complaining (I think) because you're not actually
>>>> overriding buttonPressed.  This can happen when type inference goes a
>>>> little astray, especially with methods that return Unit.  Naming
>>>> buttonPressed's param 'b' shouldn't be a problem.  I'd try explicitly
>>>> specifying the return type, like
>>>>
>>>> def pressedDo (aB: PushButton, action: Unit) = {
>>>>    aB.getButtonPressListeners.add(new ButtonPressListener {
>>>>      override def buttonPressed(b: PushButton): Unit = action
>>>>    })
>>>> }
>>>>
>>>> There are also inference rule that take effect regarding the = in
>>>>
>>>> def foo(...) = { }
>>>>
>>>> vs def foo(...) { }
>>>>
>>>> The latter (I believe) is assumed to return Unit, but I always add the =
>>>> just to be sure.  (/Programming Scala/ by Odersky, Venners, and Spoon
>>>> explains things like this very, very well.)
>>>>
>>>> Additionally, you probably want something like this:
>>>>
>>>> def pressedDo (aB: PushButton, action: =>  Unit) = {
>>>>    aB.getButtonPressListeners.add(new ButtonPressListener {
>>>>      override def buttonPressed(b: PushButton) = action
>>>>    })
>>>> }
>>>>
>>>> Note the new type of the 'action' param.  It's now "a function that
>>>> takes no params and returns Unit", instead of an actual value of type
>>>> Unit.  This is an easy way to have lazily-evaluated parameters.
>>>>
>>>> Due to some neat syntactic sugar for writing anonymous functions of this
>>>> type, if you split the parameter list for pressedDo, like
>>>>
>>>> def pressedDo (aB: PushButton)(action: =>  Unit) = {
>>>>    aB.getButtonPressListeners.add(new ButtonPressListener {
>>>>      override def buttonPressed(b: PushButton) = action
>>>>    })
>>>> }
>>>>
>>>> You could call it like:
>>>>
>>>> pressedDo(someButton) {
>>>>    val foo = new Foo
>>>>
>>>>    someOtherThing.doStuff(foo)
>>>>
>>>>    //etc
>>>> }
>>>>
>>>> Here the stuff in the braces becomes an anonymous function that gets
>>>> passed to pressedDo.
>>>>
>>>> It's also possible to use implicit conversions (the perhaps
>>>> tastelessly-named "Pimp-My-Library" pattern[1]) to "add" the pressedDo
>>>> method to the Button class.  Well, kind of, not really to the Button
>>>> class, but combined with the above technique, you could make a call like
>>>>
>>>> val someButton: Button = ...
>>>>
>>>> someButton pressed {
>>>>    val foo = new Foo
>>>>
>>>>    someOtherThing.doStuff(foo)
>>>> }
>>>>
>>>> which is starting to look smooth.  Implicits can get pretty black-magic
>>>> though, so use them with care and restraint if you do.
>>>>
>>>> [1]: http://scala.sygneca.com/patterns/pimp-my-library
>>>>
>>>> Bojan Vu ini wrote:
>>>>> Hi All,
>>>>> If you are using Scala for your code then you are obliged to write listeners in the following way:
>>>>> printButton.getButtonPressListeners.add(new ButtonPressListener {
>>>>>       override def buttonPressed(b: Button) {
>>>>>          print
>>>>>       }
>>>>>    })
>>>>> I've tried to simplify this by defining the following method:
>>>>>    def pressedDo (aB: PushButton, action: Unit) = {
>>>>>      aB.getButtonPressListeners.add(new ButtonPressListener {
>>>>>          override def buttonPressed(b: PushButton) { action }})
>>>>>    }
>>>>> and than invoking the method in this way (example)
>>>>>      pressedDo(printButton, print)
>>>>> however, this results in a compiler error:
>>>>> error: object creation impossible, since method buttonPressed in trait ButtonPressListener of type (x$1: org.apache.pivot.wtk.Button)Unit is not defined
>>>>>      aB.getButtonPressListeners.add(new ButtonPressListener {
>>>>> I'm not a programming language specialist, and in addition a Scala novice, but I'm puzzled why putting boilerplate code in a separate method is not working??
>>>>> Best regards,
>>>>> Bojan
>>>>> Dr. Bojan Vu ini
>>>>> President, Ma-CAD<http://www.ma-cad.com/>
>>>>> bojan.vucinic@ma-cad.com<ma...@ma-cad.com>
>>>>> IM: bvucinic (Skype)
>>>>> http://fr.linkedin.com/in/bvucinic
>>>>> Maintenance Concept Adjustment&  Design 31, rue Chanzy
>>>>> Paris, Ile de France 75011 France
>>>>> Work: +33 (6) 14 15 36 70
>>>>> Mobile: +33 (6) 14 15 36 70
>>>>> Fax: +33 (1) 53 01 38 28
>>>>> See who we know in common<http://www.linkedin.com/e/wwk/4839994/>        Want a signature like this?<http://www.linkedin.com/e/sig/4839994/>
>>
>>

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkzsKEEACgkQ5IyIbnMUeTvZuACeNSx8S8WlJuKapvMUifzEyPFi
fuYAn2rDRNUzKa35nM9rbLXEnrj/ESKU
=uLdg
-----END PGP SIGNATURE-----

Re: Scala

Posted by Bojan Vučinić <bo...@ma-cad.com>.
Clint, Greg,
Thanks for taking the time to look into this.
Here is the code that works:

...
import org.apache.pivot.wtk.PushButton
import org.apache.pivot.wtk.Button
...

class EnBalSP extends Application {
   var window: Window = null
...
  @BXML private var printButton: PushButton = null
...

...
   override def startup(display:Display, ns:Map[String, String]) = {
     val enBalSer: BXMLSerializer = new BXMLSerializer
...

     window = enBalSer.readObject(classOf[EnBalSP], "enbal.bxml").asInstanceOf[Window]
...
  enBalSer.bind(this, classOf[EnBalSP])

...
     initButtons
...
  window.open(display)
   }

...
  def initButtons = {
...
    printButton.getButtonPressListeners.add(new ButtonPressListener {
      override def buttonPressed(b: Button) {
      print
      }
      })
...
  }

...
  override def suspend() {}

   override def resume() {}

   override def shutdown(optional:Boolean):Boolean = {
     if (window != null) window.close()
     return false
   }
}

So, in the above the Application is launched, the buttons are binded 
with @BXML, the bxml file is deserialized in startup,
which calls initButtons where the listeners are registered. The above is 
working. Now if I change the initButtons as shown below:

...
import org.apache.pivot.wtk.PushButton
import org.apache.pivot.wtk.Button
...

class EnBalSP extends Application {
   var window: Window = null
...
  @BXML private var printButton: PushButton = null
...

...
   override def startup(display:Display, ns:Map[String, String]) = {
     val enBalSer: BXMLSerializer = new BXMLSerializer
...

     window = enBalSer.readObject(classOf[EnBalSP], "enbal.bxml").asInstanceOf[Window]
...
  enBalSer.bind(this, classOf[EnBalSP])

...
     initButtons
...
  window.open(display)
   }

...
  def initButtons = {
...
     pressedDo(printButton, print)
...
  }

   def pressedDo (aB: PushButton, action: Unit) = {
     aB.getButtonPressListeners.add(new ButtonPressListener {
         override def buttonPressed(b: Button): Unit = action })

...
  override def suspend() {}

   override def resume() {}

   override def shutdown(optional:Boolean):Boolean = {
     if (window != null) window.close()
     return false
   }
}

I get the error that display is null??

It is really strange.



Clint Gilbert said the following on 23/11/2010 20:37:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Actually, the type of b is the problem:
>
> override def buttonPressed(b: Button): Unit = action
>
> or even
>
> override def buttonPressed(b: Button) = action
>
> instead of
>
> override def buttonPressed(b: PushButton): Unit = action
>
> is what you want.
>
> Clint Gilbert wrote:
>> This is getting off-topic, but it's something I actually know a tiny bit
>> about.
>>
>> The compiler is complaining (I think) because you're not actually
>> overriding buttonPressed.  This can happen when type inference goes a
>> little astray, especially with methods that return Unit.  Naming
>> buttonPressed's param 'b' shouldn't be a problem.  I'd try explicitly
>> specifying the return type, like
>>
>> def pressedDo (aB: PushButton, action: Unit) = {
>>    aB.getButtonPressListeners.add(new ButtonPressListener {
>>      override def buttonPressed(b: PushButton): Unit = action
>>    })
>> }
>>
>> There are also inference rule that take effect regarding the = in
>>
>> def foo(...) = { }
>>
>> vs def foo(...) { }
>>
>> The latter (I believe) is assumed to return Unit, but I always add the =
>> just to be sure.  (/Programming Scala/ by Odersky, Venners, and Spoon
>> explains things like this very, very well.)
>>
>> Additionally, you probably want something like this:
>>
>> def pressedDo (aB: PushButton, action: =>  Unit) = {
>>    aB.getButtonPressListeners.add(new ButtonPressListener {
>>      override def buttonPressed(b: PushButton) = action
>>    })
>> }
>>
>> Note the new type of the 'action' param.  It's now "a function that
>> takes no params and returns Unit", instead of an actual value of type
>> Unit.  This is an easy way to have lazily-evaluated parameters.
>>
>> Due to some neat syntactic sugar for writing anonymous functions of this
>> type, if you split the parameter list for pressedDo, like
>>
>> def pressedDo (aB: PushButton)(action: =>  Unit) = {
>>    aB.getButtonPressListeners.add(new ButtonPressListener {
>>      override def buttonPressed(b: PushButton) = action
>>    })
>> }
>>
>> You could call it like:
>>
>> pressedDo(someButton) {
>>    val foo = new Foo
>>
>>    someOtherThing.doStuff(foo)
>>
>>    //etc
>> }
>>
>> Here the stuff in the braces becomes an anonymous function that gets
>> passed to pressedDo.
>>
>> It's also possible to use implicit conversions (the perhaps
>> tastelessly-named "Pimp-My-Library" pattern[1]) to "add" the pressedDo
>> method to the Button class.  Well, kind of, not really to the Button
>> class, but combined with the above technique, you could make a call like
>>
>> val someButton: Button = ...
>>
>> someButton pressed {
>>    val foo = new Foo
>>
>>    someOtherThing.doStuff(foo)
>> }
>>
>> which is starting to look smooth.  Implicits can get pretty black-magic
>> though, so use them with care and restraint if you do.
>>
>> [1]: http://scala.sygneca.com/patterns/pimp-my-library
>>
>> Bojan Vu ini wrote:
>>> Hi All,
>>> If you are using Scala for your code then you are obliged to write listeners in the following way:
>>
>>> printButton.getButtonPressListeners.add(new ButtonPressListener {
>>>       override def buttonPressed(b: Button) {
>>>          print
>>>       }
>>>    })
>>
>>> I've tried to simplify this by defining the following method:
>>>    def pressedDo (aB: PushButton, action: Unit) = {
>>>      aB.getButtonPressListeners.add(new ButtonPressListener {
>>>          override def buttonPressed(b: PushButton) { action }})
>>>    }
>>
>>> and than invoking the method in this way (example)
>>>      pressedDo(printButton, print)
>>
>>> however, this results in a compiler error:
>>
>>> error: object creation impossible, since method buttonPressed in trait ButtonPressListener of type (x$1: org.apache.pivot.wtk.Button)Unit is not defined
>>>      aB.getButtonPressListeners.add(new ButtonPressListener {
>>
>>> I'm not a programming language specialist, and in addition a Scala novice, but I'm puzzled why putting boilerplate code in a separate method is not working??
>>> Best regards,
>>> Bojan
>>
>>> Dr. Bojan Vu ini
>>> President, Ma-CAD<http://www.ma-cad.com/>
>>> bojan.vucinic@ma-cad.com<ma...@ma-cad.com>
>>> IM: bvucinic (Skype)
>>> http://fr.linkedin.com/in/bvucinic
>>> Maintenance Concept Adjustment&  Design 31, rue Chanzy
>>> Paris, Ile de France 75011 France
>>> Work: +33 (6) 14 15 36 70
>>> Mobile: +33 (6) 14 15 36 70
>>> Fax: +33 (1) 53 01 38 28
>>> See who we know in common<http://www.linkedin.com/e/wwk/4839994/>        Want a signature like this?<http://www.linkedin.com/e/sig/4839994/>
>>
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.9 (GNU/Linux)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
>
> iEYEARECAAYFAkzsGBIACgkQ5IyIbnMUeTtlkwCZAUsW/ssyCxJ95PaxTbW0ap0a
> RBwAn1ttMyvrnr/iLAZ8gAIPFy/g5obf
> =axhA
> -----END PGP SIGNATURE-----
>
>

Re: Scala

Posted by Clint Gilbert <cl...@hms.harvard.edu>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Actually, the type of b is the problem:

override def buttonPressed(b: Button): Unit = action

or even

override def buttonPressed(b: Button) = action

instead of

override def buttonPressed(b: PushButton): Unit = action

is what you want.

Clint Gilbert wrote:
> This is getting off-topic, but it's something I actually know a tiny bit
> about.
> 
> The compiler is complaining (I think) because you're not actually
> overriding buttonPressed.  This can happen when type inference goes a
> little astray, especially with methods that return Unit.  Naming
> buttonPressed's param 'b' shouldn't be a problem.  I'd try explicitly
> specifying the return type, like
> 
> def pressedDo (aB: PushButton, action: Unit) = {
>   aB.getButtonPressListeners.add(new ButtonPressListener {
>     override def buttonPressed(b: PushButton): Unit = action
>   })
> }
> 
> There are also inference rule that take effect regarding the = in
> 
> def foo(...) = { }
> 
> vs def foo(...) { }
> 
> The latter (I believe) is assumed to return Unit, but I always add the =
> just to be sure.  (/Programming Scala/ by Odersky, Venners, and Spoon
> explains things like this very, very well.)
> 
> Additionally, you probably want something like this:
> 
> def pressedDo (aB: PushButton, action: => Unit) = {
>   aB.getButtonPressListeners.add(new ButtonPressListener {
>     override def buttonPressed(b: PushButton) = action
>   })
> }
> 
> Note the new type of the 'action' param.  It's now "a function that
> takes no params and returns Unit", instead of an actual value of type
> Unit.  This is an easy way to have lazily-evaluated parameters.
> 
> Due to some neat syntactic sugar for writing anonymous functions of this
> type, if you split the parameter list for pressedDo, like
> 
> def pressedDo (aB: PushButton)(action: => Unit) = {
>   aB.getButtonPressListeners.add(new ButtonPressListener {
>     override def buttonPressed(b: PushButton) = action
>   })
> }
> 
> You could call it like:
> 
> pressedDo(someButton) {
>   val foo = new Foo
> 
>   someOtherThing.doStuff(foo)
> 
>   //etc
> }
> 
> Here the stuff in the braces becomes an anonymous function that gets
> passed to pressedDo.
> 
> It's also possible to use implicit conversions (the perhaps
> tastelessly-named "Pimp-My-Library" pattern[1]) to "add" the pressedDo
> method to the Button class.  Well, kind of, not really to the Button
> class, but combined with the above technique, you could make a call like
> 
> val someButton: Button = ...
> 
> someButton pressed {
>   val foo = new Foo
> 
>   someOtherThing.doStuff(foo)
> }
> 
> which is starting to look smooth.  Implicits can get pretty black-magic
> though, so use them with care and restraint if you do.
> 
> [1]: http://scala.sygneca.com/patterns/pimp-my-library
> 
> Bojan Vu ini wrote:
>> Hi All,
> 
>> If you are using Scala for your code then you are obliged to write listeners in the following way:
> 
> 
>> printButton.getButtonPressListeners.add(new ButtonPressListener {
>>      override def buttonPressed(b: Button) {
>>         print
>>      }
>>   })
> 
> 
>> I've tried to simplify this by defining the following method:
> 
>>   def pressedDo (aB: PushButton, action: Unit) = {
>>     aB.getButtonPressListeners.add(new ButtonPressListener {
>>         override def buttonPressed(b: PushButton) { action }})
>>   }
> 
> 
>> and than invoking the method in this way (example)
> 
>>     pressedDo(printButton, print)
> 
> 
>> however, this results in a compiler error:
> 
> 
>> error: object creation impossible, since method buttonPressed in trait ButtonPressListener of type (x$1: org.apache.pivot.wtk.Button)Unit is not defined
>>     aB.getButtonPressListeners.add(new ButtonPressListener {
> 
> 
>> I'm not a programming language specialist, and in addition a Scala novice, but I'm puzzled why putting boilerplate code in a separate method is not working??
> 
>> Best regards,
>> Bojan
> 
> 
>> Dr. Bojan Vu ini
>> President, Ma-CAD<http://www.ma-cad.com/>
>> bojan.vucinic@ma-cad.com<ma...@ma-cad.com>
>> IM: bvucinic (Skype)
>> http://fr.linkedin.com/in/bvucinic
> 
>> Maintenance Concept Adjustment & Design 31, rue Chanzy
>> Paris, Ile de France 75011 France
>> Work: +33 (6) 14 15 36 70
>> Mobile: +33 (6) 14 15 36 70
>> Fax: +33 (1) 53 01 38 28
>> See who we know in common<http://www.linkedin.com/e/wwk/4839994/>       Want a signature like this?<http://www.linkedin.com/e/sig/4839994/>
> 
> 
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkzsGBIACgkQ5IyIbnMUeTtlkwCZAUsW/ssyCxJ95PaxTbW0ap0a
RBwAn1ttMyvrnr/iLAZ8gAIPFy/g5obf
=axhA
-----END PGP SIGNATURE-----

Re: Scala

Posted by Clint Gilbert <cl...@hms.harvard.edu>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

This is getting off-topic, but it's something I actually know a tiny bit
about.

The compiler is complaining (I think) because you're not actually
overriding buttonPressed.  This can happen when type inference goes a
little astray, especially with methods that return Unit.  Naming
buttonPressed's param 'b' shouldn't be a problem.  I'd try explicitly
specifying the return type, like

def pressedDo (aB: PushButton, action: Unit) = {
  aB.getButtonPressListeners.add(new ButtonPressListener {
    override def buttonPressed(b: PushButton): Unit = action
  })
}

There are also inference rule that take effect regarding the = in

def foo(...) = { }

vs def foo(...) { }

The latter (I believe) is assumed to return Unit, but I always add the =
just to be sure.  (/Programming Scala/ by Odersky, Venners, and Spoon
explains things like this very, very well.)

Additionally, you probably want something like this:

def pressedDo (aB: PushButton, action: => Unit) = {
  aB.getButtonPressListeners.add(new ButtonPressListener {
    override def buttonPressed(b: PushButton) = action
  })
}

Note the new type of the 'action' param.  It's now "a function that
takes no params and returns Unit", instead of an actual value of type
Unit.  This is an easy way to have lazily-evaluated parameters.

Due to some neat syntactic sugar for writing anonymous functions of this
type, if you split the parameter list for pressedDo, like

def pressedDo (aB: PushButton)(action: => Unit) = {
  aB.getButtonPressListeners.add(new ButtonPressListener {
    override def buttonPressed(b: PushButton) = action
  })
}

You could call it like:

pressedDo(someButton) {
  val foo = new Foo

  someOtherThing.doStuff(foo)

  //etc
}

Here the stuff in the braces becomes an anonymous function that gets
passed to pressedDo.

It's also possible to use implicit conversions (the perhaps
tastelessly-named "Pimp-My-Library" pattern[1]) to "add" the pressedDo
method to the Button class.  Well, kind of, not really to the Button
class, but combined with the above technique, you could make a call like

val someButton: Button = ...

someButton pressed {
  val foo = new Foo

  someOtherThing.doStuff(foo)
}

which is starting to look smooth.  Implicits can get pretty black-magic
though, so use them with care and restraint if you do.

[1]: http://scala.sygneca.com/patterns/pimp-my-library

Bojan Vučinić wrote:
> Hi All,
> 
> If you are using Scala for your code then you are obliged to write listeners in the following way:
> 
> 
> printButton.getButtonPressListeners.add(new ButtonPressListener {
>      override def buttonPressed(b: Button) {
>         print
>      }
>   })
> 
> 
> I've tried to simplify this by defining the following method:
> 
>   def pressedDo (aB: PushButton, action: Unit) = {
>     aB.getButtonPressListeners.add(new ButtonPressListener {
>         override def buttonPressed(b: PushButton) { action }})
>   }
> 
> 
> and than invoking the method in this way (example)
> 
>     pressedDo(printButton, print)
> 
> 
> however, this results in a compiler error:
> 
> 
> error: object creation impossible, since method buttonPressed in trait ButtonPressListener of type (x$1: org.apache.pivot.wtk.Button)Unit is not defined
>     aB.getButtonPressListeners.add(new ButtonPressListener {
> 
> 
> I'm not a programming language specialist, and in addition a Scala novice, but I'm puzzled why putting boilerplate code in a separate method is not working??
> 
> Best regards,
> Bojan
> 
> 
> Dr. Bojan Vučinić
> President, Ma-CAD<http://www.ma-cad.com/>
> bojan.vucinic@ma-cad.com<ma...@ma-cad.com>
> IM: bvucinic (Skype)
> http://fr.linkedin.com/in/bvucinic
> 
> Maintenance Concept Adjustment & Design 31, rue Chanzy
> Paris, Ile de France 75011 France
> Work: +33 (6) 14 15 36 70
> Mobile: +33 (6) 14 15 36 70
> Fax: +33 (1) 53 01 38 28
> See who we know in common<http://www.linkedin.com/e/wwk/4839994/>       Want a signature like this?<http://www.linkedin.com/e/sig/4839994/>
> 

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkzsFf0ACgkQ5IyIbnMUeTt8QwCfcoFOKMbw1G+kNJCV7O4Ym0sc
RMEAn06q/RVmQIYHER4ifzrxhYHE6NaL
=qH9e
-----END PGP SIGNATURE-----

Re: Scala

Posted by Greg Brown <gk...@mac.com>.
The display is definitely initialized by the time startup() is called, though your buttons may not be attached to the display if they are not descendants of an open Window.

Either way, it shouldn't matter - you can still register event listeners even if a component is not currently part of the display hierarchy. So I suspect that something else is going on. Can you share any additional source code?

On Nov 23, 2010, at 1:26 PM, Bojan Vučinić wrote:

> I've changed b to Button, but the error changed to
> 
> java.lang.IllegalArgumentException: display is null.
> 
> as the display is not initialized yet. Namely, I'm doing this during startup() to define listeners.
> The method is called once, but as it is called for each button I wanted to reduce the number of lines ;-)
> by writing a one-liner for each button, and maybe further on, put all the buttons in a collection and iterate over its members.
> It is just reducing the number of lines in code.
> 
> 
> Greg Brown said the following on 23/11/2010 19:10:
>> I noticed that, in the second example, you are passing an instance of PushButton as b rather than Button. That may be causing the problem, since the buttonPressed() method takes an instance of Button, not PushButton.
>> 
>> Also, I'm not sure how you expect to call pressedDo(), but as coded it will add a new event listener every time it is called. Is that the intended behavior?
>> 
>> 
>> On Nov 23, 2010, at 1:05 PM, Bojan Vučinić wrote:
>> 
>>> Hi All,
>>> 
>>> If you are using Scala for your code then you are obliged to write listeners in the following way:
>>> 
>>> printButton.getButtonPressListeners.add(new ButtonPressListener {
>>>      override def buttonPressed(b: Button) {
>>>      	print
>>>      }
>>>   })
>>> I've tried to simplify this by defining the following method:
>>>   def pressedDo (aB: PushButton, action: Unit) = {
>>>     aB.getButtonPressListeners.add(new ButtonPressListener {
>>>         override def buttonPressed(b: PushButton) { action }})
>>>   }
>>> and than invoking the method in this way (example)
>>>     pressedDo(printButton, print)
>>> however, this results in a compiler error:
>>> error: object creation impossible, since method buttonPressed in trait ButtonPressListener of type (x$1: org.apache.pivot.wtk.Button)Unit is not defined
>>>     aB.getButtonPressListeners.add(new ButtonPressListener {
>>> I'm not a programming language specialist, and in addition a Scala novice, but I'm puzzled why putting boilerplate code in a separate method is not working??
>>> 
>>> Best regards,
>>> Bojan
>>> 
>>> 


Re: Scala

Posted by Bojan Vučinić <bo...@ma-cad.com>.
I've changed b to Button, but the error changed to

java.lang.IllegalArgumentException: display is null.

as the display is not initialized yet. Namely, I'm doing this during 
startup() to define listeners.
The method is called once, but as it is called for each button I wanted 
to reduce the number of lines ;-)
by writing a one-liner for each button, and maybe further on, put all 
the buttons in a collection and iterate over its members.
It is just reducing the number of lines in code.


Greg Brown said the following on 23/11/2010 19:10:
> I noticed that, in the second example, you are passing an instance of 
> PushButton as b rather than Button. That may be causing the problem, 
> since the buttonPressed() method takes an instance of Button, not 
> PushButton.
>
> Also, I'm not sure how you expect to call pressedDo(), but as coded it 
> will add a new event listener every time it is called. Is that the 
> intended behavior?
>
>
> On Nov 23, 2010, at 1:05 PM, Bojan Vučinić wrote:
>
>> Hi All,
>>
>> If you are using Scala for your code then you are obliged to write 
>> listeners in the following way:
>>
>> printButton.getButtonPressListeners.add(new ButtonPressListener {
>>       override def buttonPressed(b: Button) {
>>       	print
>>       }
>>    })
>> I've tried to simplify this by defining the following method:
>>    def pressedDo (aB: PushButton, action: Unit) = {
>>      aB.getButtonPressListeners.add(new ButtonPressListener {
>>          override def buttonPressed(b: PushButton) { action }})
>>    }
>> and than invoking the method in this way (example)
>>      pressedDo(printButton, print)
>> however, this results in a compiler error:
>> error: object creation impossible, since method buttonPressed in trait ButtonPressListener of type (x$1: org.apache.pivot.wtk.Button)Unit is not defined
>>      aB.getButtonPressListeners.add(new ButtonPressListener {
>> I'm not a programming language specialist, and in addition a Scala 
>> novice, but I'm puzzled why putting boilerplate code in a separate 
>> method is not working??
>>
>> Best regards,
>> Bojan
>>
>>

Re: Scala

Posted by Greg Brown <gk...@mac.com>.
I noticed that, in the second example, you are passing an instance of PushButton as b rather than Button. That may be causing the problem, since the buttonPressed() method takes an instance of Button, not PushButton.

Also, I'm not sure how you expect to call pressedDo(), but as coded it will add a new event listener every time it is called. Is that the intended behavior?


On Nov 23, 2010, at 1:05 PM, Bojan Vučinić wrote:

> Hi All,
> 
> If you are using Scala for your code then you are obliged to write listeners in the following way:
> 
> printButton.getButtonPressListeners.add(new ButtonPressListener {
>      override def buttonPressed(b: Button) {
>      	print
>      }
>   })
> I've tried to simplify this by defining the following method:
>   def pressedDo (aB: PushButton, action: Unit) = {
>     aB.getButtonPressListeners.add(new ButtonPressListener {
>         override def buttonPressed(b: PushButton) { action }})
>   }
> and than invoking the method in this way (example)
>     pressedDo(printButton, print)
> however, this results in a compiler error:
> error: object creation impossible, since method buttonPressed in trait ButtonPressListener of type (x$1: org.apache.pivot.wtk.Button)Unit is not defined
>     aB.getButtonPressListeners.add(new ButtonPressListener {
> I'm not a programming language specialist, and in addition a Scala novice, but I'm puzzled why putting boilerplate code in a separate method is not working??
> 
> Best regards,
> Bojan
> 
> 
> Dr. Bojan Vučinić 
> President, Ma-CAD
> bojan.vucinic@ma-cad.com
> IM: bvucinic (Skype)
> http://fr.linkedin.com/in/bvucinic
> 
> Maintenance Concept Adjustment & Design	 31, rue Chanzy
> Paris, Ile de France 75011 France
> Work: +33 (6) 14 15 36 70
> Mobile: +33 (6) 14 15 36 70
> Fax: +33 (1) 53 01 38 28
> See who we know in common	Want a signature like this?