This is the mail archive of the kawa@sourceware.org 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: constructor with pre-allocated memory


Something like this might work:

(define (keyword->setter-name (kw ::keyword)) ::string
  (let* ((name (kw:getName)) (c (string-ref name 0)))
    (format #f "set~A~A" (char-upcase c) (name:substring 1))))

(define-syntax (apply-setters form)
(syntax-case form ()
((apply-setters pointer class-type)
#`pointer)
((apply-setters pointer class-type keyword value rest ...)
(let ((setter-name (keyword->setter-name (syntax keyword))))
#`(begin (invoke (as class-type pointer) #,(datum->syntax- object form setter-name) value)
(apply-setters pointer class-type rest ...))))))


(define-syntax (auto-fill form)
  (syntax-case form ()
    ((auto-fill pointer class-type context rest ...)
     (if (eq? #!null (syntax pointer))
         #`(class-type context rest ...)
         #`(apply-setters pointer class-type rest ...)))))


But like Per and Marius said, you could probably do it with a function, too:


(define (auto-fill pointer class-type context #!rest setter-pairs)
(let ((o (if (eq? #!null pointer) (class-type context) pointer)))
(let loop ((setter-pairs setter-pairs))
(cond ((null? setter-pairs) o)
(else (invoke (as class-type o) (keyword->setter-name (car setter-pairs)) (cadr setter-pairs))
(loop (cddr setter-pairs)))))))



I just typed all of that into my mail client, so it needs some testing.


-Jamie

On Jul 7, 2012, at 9:48 AM, Marius Kjeldahl wrote:

So let me ask a stupid question; why couldn't you refactor this into its own function? Why do you need macros for this?

Thanks,

Marius K.

On Saturday 07 July 2012 07:57 AM, Peter Feigl wrote:
Hello fellow Schemers!

I have a question regarding Android and the (very nice) kawa extended
syntax for object construction. In Android, if a list is displayed to
the user, the interface gives the programmer either a null-pointer
(meaning the view object has to be constructed from scratch) or an
existing object pointer (meaning this could be filled in with data,
saving the extra garbage collection and new allocation of another
object).
Right now, I just use

(<android.widget.TextView> context text: "some text" textSize: 12
                           textColor: someColor)

to create a new object. It would however be much preferable to have some
sort of macro that automatically checks whether a given value is #! null
or not, and fills in the object or creates a new one:


(auto-fill pointer <android.widget.TextView> context text: "hello" ...)

This should expand to

(if (eq? #!null pointer)
    (<android.widget.TextView> context text: "some text" textSize: 12
                           textColor: someColor)
    (begin
      (pointer:setText "some text")
      (pointer:setTextSize 12)
      (pointer:setTextColor someColor)))

Is this even possible, given the  text: "hello"  syntax automatically
searches for setText and calls that? Does kawa support unhygienic
macros?

I'd be very glad about any comments on how to make this work, it would
provide another major benefit over "normal" java code :)


Thanks for any help,

Peter




-- Jamison Hope The PTR Group www.theptrgroup.com




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