This is the mail archive of the kawa@sources.redhat.com mailing list for the Kawa project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Alternate syntax for field access/method calls


(Apologies for not replying directly to the original thread -- I just
subscribed to the mailing list, and it's not clear how to get recent
messages sent to me so I can reply directly.  The list manager
provides a mechanism if you know the message numbers, but
they don't seem to be displayed anywhere.)

I've been using the following pseudo-aliases:

(define &  invoke)
(define && invoke-static)
(define $  field)
(define $$ static-field)
(define new make)

I find them fairly intuitive and readable.  Here's a contrived mini-example:

(define (make-test-frame) :: <javax.swing.JFrame>
  (let ((frame :: <javax.swing.JFrame>
	       (new <javax.swing.JFrame> "Kawa App")))

    (& frame 'setSize 300 200)
    (& frame 'setDefaultCloseOperation
       ($$ <javax.swing.WindowConstants> 'EXIT_ON_CLOSE))

    (& frame 'setLocationRelativeTo #!null)

    (let ((root :: <java.awt.Container>
		(& frame 'getContentPane)))

      (& root 'setLayout (new <java.awt.BorderLayout>))

      (let ((panel (new <javax.swing.JPanel>)))

	(& panel 'addMouseListener
	   (object
	    (<java.awt.event.MouseAdapter>)
	    ((mouseClicked e :: <java.awt.event.MouseEvent>) :: <void>
	     (display
	      (format "jvm: ~A~%"
		      (&& <java.lang.System> 'getProperty "java.vm.version"))))))

	(& root 'add panel
	   ($$ <java.awt.BorderLayout> 'CENTER))))
	      
    (& frame 'setVisible #t)
    frame))

(make-test-frame)

Chaining them actually shows them lexically in the same order as
they appear in Java code, e.g.:

new java.lang.String("hello").toUpperCase().endsWith("LO")

(& (& (java.lang.String:new "hello") 'toUpperCase) 'endsWith "LO")
#t

I like Dominique's [obj (m1 a1) (m2 a2)] syntax, except for the
choice of square-brackets.  I just don't free-associate [] with method
invocation, I guess.  I could see using curlies, maybe:

{(java.lang.String:new "hello") (toUpperCase) (endsWith "LO")}

But I'd be just as happy with a non-reader macro:

(& (java.lang.String:new "hello") (toUpperCase) (endsWith "LO"))

Speaking of reader macros -- since I can't reply directly to Per's
request-for-srfi thread until another message comes along, I'll
just put in my vote now for srfi-10. :-)  I'd particularly like to make
a reader-macro that allows me to specify regexps without having
to double-escape everything.  Hash literals would be nice, too.
Even a very simple reader-macro system should be sufficient for
making things like that.

Cheers,

SteveYegge


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]