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

Scala style opinions sought: Is this user or abuse?

11 replies
Ken McDonald
Joined: 2011-02-13,
User offline. Last seen 42 years 45 weeks ago.
I'm doing AWT event processing with Scala. One of the things I want to achieve is easy specifications of events (I'm very influenced by Tk in this regard), while at the same time maintaining some degree of compile-time checking. So in this vein, I've defined the following:
class Modifiers(val modifiers: HashSet[KeyName]) { def control = new Modifiers(modifiers + KeyName.control) def alt = new Modifiers(modifiers + KeyName.alt) def shift = new Modifiers(modifiers + KeyName.shift) def meta = new Modifiers(modifiers + KeyName.meta) def apply(keys: String) = KeySeq(modifiers, keys)}
object control extends Modifiers(HashSet(KeyName.control))object alt extends Modifiers(HashSet(KeyName.alt))object shift extends Modifiers(HashSet(KeyName.shift))object meta extends Modifiers(HashSet(KeyName.meta))
which allows users of the lib to write something like
control.alt("x")
to specify holding down control-alt and pressing x, or
control("xs")
to indicate holding down control while pressing x and then s. (Hey, I still remember a few Emacs commands, after all these years.)
I'm curious as to what people think of doing things like this in Scala, and if there are any recommendations for better ways to achieve the same effect. On one hand, this is easy and preserves some degree of compile-time checking. On the other hand, it does seem like I'm putting Scala through backflips a bit.
Thanks,Ken
Tony Morris 2
Joined: 2009-03-20,
User offline. Last seen 42 years 45 weeks ago.
Re: Scala style opinions sought: Is this user or abuse?
Hide the constructor. "HashSet" should not appear in your scaladoc output -- as a user of your API I should not know it is there.

Otherwise, minor improvements, nice work. Carry on.

On 28/03/11 09:58, Kenneth McDonald wrote:
I'm doing AWT event processing with Scala. One of the things I want to achieve is easy specifications of events (I'm very influenced by Tk in this regard), while at the same time maintaining some degree of compile-time checking. So in this vein, I've defined the following:
class Modifiers(val modifiers: HashSet[KeyName]) { def control = new Modifiers(modifiers + KeyName.control) def alt = new Modifiers(modifiers + KeyName.alt) def shift = new Modifiers(modifiers + KeyName.shift) def meta = new Modifiers(modifiers + KeyName.meta) def apply(keys: String) = KeySeq(modifiers, keys) }
object control extends Modifiers(HashSet(KeyName.control)) object alt extends Modifiers(HashSet(KeyName.alt)) object shift extends Modifiers(HashSet(KeyName.shift)) object meta extends Modifiers(HashSet(KeyName.meta))
which allows users of the lib to write something like
control.alt("x")
to specify holding down control-alt and pressing x, or
control("xs")
to indicate holding down control while pressing x and then s. (Hey, I still remember a few Emacs commands, after all these years.)
I'm curious as to what people think of doing things like this in Scala, and if there are any recommendations for better ways to achieve the same effect. On one hand, this is easy and preserves some degree of compile-time checking. On the other hand, it does seem like I'm putting Scala through backflips a bit.
Thanks, Ken


-- 
Tony Morris
http://tmorris.net/

Ken McDonald
Joined: 2011-02-13,
User offline. Last seen 42 years 45 weeks ago.
Re: Scala style opinions sought: Is this user or abuse?

Ah, that should be "use" rather than "user" in the title. Sigh.

Razvan Cojocaru 3
Joined: 2010-07-28,
User offline. Last seen 42 years 45 weeks ago.
RE: Scala style opinions sought: Is this user or abuse?

Why not just

 

import KeyName._

 

def expect (controlSeq:KeyName*) (keySeq:String) = KeySeq (controlSeq, keySeq)

 

expect (control, alt, shift) (“xs”)

 

or other versions like:

 

“xs” with (control, alt, shift) // some RichString stuff

 

 

Lazy as I am, the point is that since the modifiers are many, just keep the respective class (KeyName) as is and mess with the syntax instead

 

From: scala-debate [at] googlegroups [dot] com [mailto:scala-debate [at] googlegroups [dot] com] On Behalf Of Kenneth McDonald
Sent: March-27-11 7:59 PM
To: scala-debate [at] googlegroups [dot] com
Subject: [scala-debate] Scala style opinions sought: Is this user or abuse?

 

I'm doing AWT event processing with Scala. One of the things I want to achieve is easy specifications of events (I'm very influenced by Tk in this regard), while at the same time maintaining some degree of compile-time checking. So in this vein, I've defined the following:

 

class Modifiers(val modifiers: HashSet[KeyName]) {

            def control = new Modifiers(modifiers + KeyName.control)

            def alt = new Modifiers(modifiers + KeyName.alt)

            def shift = new Modifiers(modifiers + KeyName.shift)

            def meta = new Modifiers(modifiers + KeyName.meta)

            def apply(keys: String) = KeySeq(modifiers, keys)

}

 

object control extends Modifiers(HashSet(KeyName.control))

object alt extends Modifiers(HashSet(KeyName.alt))

object shift extends Modifiers(HashSet(KeyName.shift))

object meta extends Modifiers(HashSet(KeyName.meta))

 

which allows users of the lib to write something like

 

control.alt("x")

 

to specify holding down control-alt and pressing x, or

 

control("xs")

 

to indicate holding down control while pressing x and then s. (Hey, I still remember a few Emacs commands, after all these years.)

 

I'm curious as to what people think of doing things like this in Scala, and if there are any recommendations for better ways to achieve the same effect. On one hand, this is easy and preserves some degree of compile-time checking. On the other hand, it does seem like I'm putting Scala through backflips a bit.

 

Thanks,

Ken

Ken McDonald
Joined: 2011-02-13,
User offline. Last seen 42 years 45 weeks ago.
Re: RE: Scala style opinions sought: Is this user or abuse?
Hi Razvan,
Both of your suggestions are reasonable, and I will probably do something like the first to handle the general case. But I'm also trying to follow three principles that I hold dear:
1) Make easy things easy, and hard things possible.
2) For frequently used operations, concise syntax matters--eliminating even one extra word can make a difference.
3) Readability matters.
One of my hopes is that if someone explores sample code for my library (at a time when sample code might actually exist :-) ), they will immediately recognize something like "control.alt("x")" as a key specification and go ahead and "do the obvious" in defining their own keys when they write code.
That said, I'm not sure if that will happen, and I'm not sure my approach is "correct", to the extent that word has meaning in something like this. That's why I posted to this group :-)

Thanks,Ken
Alex Repain
Joined: 2010-07-27,
User offline. Last seen 1 year 31 weeks ago.
Re: RE: Scala style opinions sought: Is this user or abuse?


2011/3/28 Kenneth McDonald <ykkenmcd [at] gmail [dot] com>
Hi Razvan,
Both of your suggestions are reasonable, and I will probably do something like the first to handle the general case. But I'm also trying to follow three principles that I hold dear:
1) Make easy things easy, and hard things possible.
2) For frequently used operations, concise syntax matters--eliminating even one extra word can make a difference.
3) Readability matters.
One of my hopes is that if someone explores sample code for my library (at a time when sample code might actually exist :-) ), they will immediately recognize something like "control.alt("x")" as a key specification and go ahead and "do the obvious" in defining their own keys when they write code.

How would you render a typical Emacs Crl-x Ctrl-c command with this syntax ?

That said, I'm not sure if that will happen, and I'm not sure my approach is "correct", to the extent that word has meaning in something like this. That's why I posted to this group :-)

Thanks,Ken

Ken McDonald
Joined: 2011-02-13,
User offline. Last seen 42 years 45 weeks ago.
Re: RE: Scala style opinions sought: Is this user or abuse?
That falls under the heading of "make easy things easy"; it's just control("xc").
A more general format is also permitted (though the code to actually generate the events isn't yet there): It's possible to specify, "hold down the control key, press x, then also hold down the alt key and press c". I don't expect such signatures to be of much use, and so there's no "convenient" way of specifying them, but the possibility is there.
In fact, it's possible to specify something like, "while holding down the a key, press c". Any key can be used as a modifier. Again, the code to actually generate such events isn't there--it's a bit of a pain to do this under AWT as it requires keeping track of key presses and releases.
I'm also dealing (well, not dealing) with the fact that "Alt-+" actually generates "±" on my keyboard in the "keyTyped" awt invocation, and I need to figure out what to do about that.
This is probably more than you wanted to know, but it gave me an excuse to avoid coding for a few minutes :-)

Cheers,Ken
Razvan Cojocaru 3
Joined: 2010-07-28,
User offline. Last seen 42 years 45 weeks ago.
RE: RE: Scala style opinions sought: Is this user or abuse?

Hey, great idea. What I didn’t like was control.alt(xxx) – doesn’t look intuitive.

 

How about this: control-alt-shift-x

 

If you’re going to the trouble, then you could try to overload the – operator on Modifiers and/or KeyName… it looks more intuitive

 

From: scala-debate [at] googlegroups [dot] com [mailto:scala-debate [at] googlegroups [dot] com] On Behalf Of Alex Repain
Sent: March-28-11 2:41 PM
To: scala-debate [at] googlegroups [dot] com
Subject: Re: RE: [scala-debate] Scala style opinions sought: Is this user or abuse?

 

 

2011/3/28 Kenneth McDonald <ykkenmcd [at] gmail [dot] com>

Hi Razvan,

 

Both of your suggestions are reasonable, and I will probably do something like the first to handle the general case. But I'm also trying to follow three principles that I hold dear:

 

1) Make easy things easy, and hard things possible.

 

2) For frequently used operations, concise syntax matters--eliminating even one extra word can make a difference.

 

3) Readability matters.

 

One of my hopes is that if someone explores sample code for my library (at a time when sample code might actually exist :-) ), they will immediately recognize something like "control.alt("x")" as a key specification and go ahead and "do the obvious" in defining their own keys when they write code.


How would you render a typical Emacs Crl-x Ctrl-c command with this syntax ?

That said, I'm not sure if that will happen, and I'm not sure my approach is "correct", to the extent that word has meaning in something like this. That's why I posted to this group :-)

 

 

Thanks,

Ken

 

Ken McDonald
Joined: 2011-02-13,
User offline. Last seen 42 years 45 weeks ago.
Re: RE: RE: Scala style opinions sought: Is this user or abuse?
Oooh, I like that. Probably easier to implement, too. When I get back to that part of the code, I'll make the changes... 

Hey, great idea. What I didn’t like was control.alt(xxx) – doesn’t look intuitive.

 

How about this: control-alt-shift-x

 

If you’re going to the trouble, then you could try to overload the – operator on Modifiers and/or KeyName… it looks more intuitive


ichoran
Joined: 2009-08-14,
User offline. Last seen 2 years 3 weeks ago.
Re: RE: Scala style opinions sought: Is this user or abuse?


On Mon, Mar 28, 2011 at 4:02 PM, Razvan Cojocaru <pub [at] razie [dot] com> wrote:

Hey, great idea. What I didn’t like was control.alt(xxx) – doesn’t look intuitive.

 

How about this: control-alt-shift-x


Using the - operator to perform addition might count as abuse.

Looks very cool, I have to admit, but
    val mymod = ctrl-alt
    // Lots of space here, so you've forgotten what mymod is
    val whatmod = mymod - alt
makes it look like you're removing "alt" from the list on whatmod.

Unfortunately, using + is painfully error-prone given how strings convert everything to themselves.

  --Rex

Razvan Cojocaru 3
Joined: 2010-07-28,
User offline. Last seen 42 years 45 weeks ago.
RE: RE: Scala style opinions sought: Is this user or abuse?

Indeed. In this specific case though, I would accept it, since the semantics are very clear: everybody has a gut feel for what control-alt-DEL might mean… J

 

Also, there’s a case for using minus for “composing” flags, see rm –r –f  J so if you overload it on the KeyName, then it could also be DEL-control-alt … meh - not as nice…

 

 

From: Rex Kerr [mailto:ichoran [at] gmail [dot] com]
Sent: March-28-11 4:34 PM
To: Razvan Cojocaru
Cc: Alex Repain; scala-debate [at] googlegroups [dot] com
Subject: Re: RE: [scala-debate] Scala style opinions sought: Is this user or abuse?

 

 

On Mon, Mar 28, 2011 at 4:02 PM, Razvan Cojocaru <pub [at] razie [dot] com> wrote:

Hey, great idea. What I didn’t like was control.alt(xxx) – doesn’t look intuitive.

 

How about this: control-alt-shift-x


Using the - operator to perform addition might count as abuse.

Looks very cool, I have to admit, but
    val mymod = ctrl-alt
    // Lots of space here, so you've forgotten what mymod is
    val whatmod = mymod - alt
makes it look like you're removing "alt" from the list on whatmod.

Unfortunately, using + is painfully error-prone given how strings convert everything to themselves.

  --Rex

 

Ken Scambler
Joined: 2009-11-07,
User offline. Last seen 42 years 45 weeks ago.
Re: RE: Scala style opinions sought: Is this user or abuse?

On 29 March 2011 09:55, Razvan Cojocaru <pub [at] razie [dot] com> wrote:

Indeed. In this specific case though, I would accept it, since the semantics are very clear: everybody has a gut feel for what control-alt-DEL might mean… J

 

 


Looking in most Windows menus, the key-shortcuts are listed as Ctrl+D,  Ctrl+Shift+B, and so forth.  Using + wouldn't be out of place, in any case.

Ken



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