A newbie problem with generic function

Discussion of Common Lisp

A newbie problem with generic function

Postby TPJ » Tue Dec 16, 2008 1:11 pm

I've got another Lisp-newbie question. I'm trying to define my own button class using the LTK package. In the ltk-mw package the following code can be found:

Code: Select all
(defgeneric redraw (widget))


(defclass redraw-on-resize ()
  ())

(defmethod initialize-instance :after ((r redraw-on-resize) &key)
  (bind r "<Configure>" (lambda (evt) (declare (ignore evt))
           (redraw r))))


My code is as follows:

Code: Select all
(require 'asdf)
(require 'ltk)
(require 'ltk-mw)
(use-package 'ltk)
(use-package 'ltk-mw)

;;; A simple button class.

(defclass btn (redraw-on-resize canvas)
  ( (rect :accessor acc-rect)
    (text :accessor acc-text) ))

(defmethod redraw ((widget btn))
  (let* ( (width (window-width widget))
          (height (window-height widget))
          (whalf (truncate width 2))
          (hhalf (truncate height 2)) )
    (set-coords widget (acc-rect widget) (list 0 0 width height))
    (create-text widget whalf hhalf (acc-text widget))))


The drawing code is probably buggy, but that's not the point. The problem is I get the following message from SBCL when I load my code using (load "my-lisp-file"):

STYLE-WARNING: Implicitly creating new generic function REDRAW.

It seems that Lisp isn't able to find the generic function redraw in the ltk-mw package. But why it is so? The generic function is defined in that package, and the pacakage is used in my program...
TPJ
 
Posts: 11
Joined: Tue Nov 25, 2008 6:37 am
Location: Gliwice, Poland

Re: A newbie problem with generic function

Postby Ramarren » Tue Dec 16, 2008 1:28 pm

REDRAW is not exported from ltk-mw package. In any case using USE-PACKAGE in code is bad practice, you create your own package with DEFPACKAGE and add use declarations there. Also, REQUIRE is not portable, or rather, the way it works (in SBCL it just goes through ASDF) is not. Creating asd file with proper system declarations is usually a better approach. Generally, declarative approach is better then imperative for such things.

REDRAW not being exported suggests that it is not supposed to be used by user's code, but I don't know enough about LTK. If you are extending ltk-mw, it might be simplest to just do (in-package :ltk-mw).
Ramarren
 
Posts: 613
Joined: Sun Jun 29, 2008 4:02 am
Location: Warsaw, Poland

Re: A newbie problem with generic function

Postby TPJ » Thu Dec 18, 2008 3:28 am

Thanks for the answer. I felt it was something silly. :oops:
TPJ
 
Posts: 11
Joined: Tue Nov 25, 2008 6:37 am
Location: Gliwice, Poland


Return to Common Lisp

Who is online

Users browsing this forum: No registered users and 2 guests

cron