- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Why can a button set a frame attribute?
Mon, 2010-02-01, 13:05
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
}
}
}
Mon, 2010-02-01, 16:07
#2
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:
--
Daniel C. Sobral
I travel to the future all the time.
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.
Tue, 2010-02-02, 10:17
#3
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
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
> }
> }
> }
>
>
>
>