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

Why can a button set a frame attribute?

3 replies
Walter van der Laan
Joined: 2010-02-01,
User offline. Last seen 42 years 45 weeks ago.

Hi,

I just started programming in Scala and am really enjoying the experience.
I posted this message because the below program does not do what I expect.
Perhaps because I do not fully understand the scope implications of brackets.

The second assignment to 'title' changes the Frame title
while I was expecting it to set a title for the button.
I now know that one needs to set the 'text' of a button
but it still doesn't feel quit right.
Why didn't the compiler warn me about setting a non-existing attribute?

Walter

===the=code===

import scala.swing._

object TitleError extends SimpleGUIApplication {
def top = new MainFrame {
title = "Frame"
contents = new Button {
title = "Button" // Button doesn't have a title
}
}
}

Ken Scambler
Joined: 2009-11-07,
User offline. Last seen 42 years 45 weeks ago.
Re: Why can a button set a frame attribute?

Hi Walter,
'title' is an instance method on your MainFrame, and since your 'new Button
{}' is an inner class within the MainFrame, members of the frame are of
course still visible.
Cheers,
Ken

Walter van der Laan wrote:
>
> Hi,
>
> I just started programming in Scala and am really enjoying the experience.
> I posted this message because the below program does not do what I expect.
> Perhaps because I do not fully understand the scope implications of
> brackets.
>
> The second assignment to 'title' changes the Frame title
> while I was expecting it to set a title for the button.
> I now know that one needs to set the 'text' of a button
> but it still doesn't feel quit right.
> Why didn't the compiler warn me about setting a non-existing attribute?
>
> Walter
>
> ===the=code===
>
> import scala.swing._
>
> object TitleError extends SimpleGUIApplication {
> def top = new MainFrame {
> title = "Frame"
> contents = new Button {
> title = "Button" // Button doesn't have a title
> }
> }
> }
>
>
>
>

dcsobral
Joined: 2009-04-23,
User offline. Last seen 38 weeks 5 days ago.
Re: Why can a button set a frame attribute?
What you did is about the same as this:   class Counter {   var count = 0   def incrementer(n: Int) = new Function0[Int] {     count += n     count   } }   I hope this different context makes what happened clearer.
On Mon, Feb 1, 2010 at 10:01 AM, Walter van der Laan <waltervanderlaan@gmail.com> wrote:
Hi,

I just started programming in Scala and am really enjoying the experience.
I posted this message because the below program does not do what I expect.
Perhaps because I do not fully understand the scope implications of brackets.

The second assignment to 'title' changes the Frame title
while I was expecting it to set a title for the button.
I now know that one needs to set the 'text' of a button
but it still doesn't feel quit right.
Why didn't the compiler warn me about setting a non-existing attribute?

Walter

===the=code===

import scala.swing._

object TitleError extends SimpleGUIApplication {
       def top = new MainFrame {
               title = "Frame"
               contents = new Button {
                       title = "Button" // Button doesn't have a title
               }
       }
}





--
Daniel C. Sobral

I travel to the future all the time.
Walter van der Laan
Joined: 2010-02-01,
User offline. Last seen 42 years 45 weeks ago.
Re: Why can a button set a frame attribute?

Ken, Daniel,

Thanks for the clarification. I thought I was initializing abstract members,
but, as you explained title and text are plain old setter methods.

Thanks!

Walter

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