Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3234838
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:26:16+00:00 2026-05-17T17:26:16+00:00

I have a class in Common Lisp: (defclass my-cool-class() ((variable1 :initarg :variable1 :accessor variable1

  • 0

I have a class in Common Lisp:

(defclass my-cool-class()
  ((variable1
    :initarg :variable1
    :accessor variable1
    :initform (error "Must supply value to variable1"))
   (variable2
    :initarg :variable2
    :accessor variable2
    :initform (error "Must supply value to variable2"))

I wanted to create a macro that would simplify this redundancy of typing

(defmacro make-slot (slot-name)
  `(slot-name 
     :initarg :,slot-name
     :accessor :,slot-name
     :initform (error "Must supply value")))

Eventually I’d like to have (defclass my-cool-class () (make-slots ‘(foo bar baz)) and get foo, bar, and baz out as slots automagically.

But, when I went to do a macroexpand-1 of make-slot, boy howdy did I get reader errors.

The first one was “illegal terminating character after a colon…” and then it kept going.

SBCL 1.0.37.

edit: the examples are syntactically correct on the system, I did some redaction before I copied.


Six months later –

(defun build-var (classname var)
  (list var 
        :initform nil
        :accessor (intern (concatenate 'string (string classname) "-" 
                                       (string var)))
        :initarg (intern (string var) :keyword)))

(defun build-varlist (classname varlist)
   (loop for var in varlist 
         collect (build-var classname var)))


(defmacro defobject (name &rest varlist)
  "Defines a class with a set of behavior. 
   Variables are accessed by name-varname.

   (defobject classname v1 v2 v3)
  "
  `(defclass ,name ()
     ,(build-varlist name varlist))):

Two and a half years later.

I discovered the six-month-old code in the wild elsewhere. While I’m flattered, it also reminds me to update this.

If you like this idea, I keep this code gardened at: https://github.com/pnathan/defobject . As before, its goal is to produce CLOS classes with the minimum of repetitive typing. A similar system exists called DEFCLASS-STAR. Interested parties are advised to review both.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T17:26:16+00:00Added an answer on May 17, 2026 at 5:26 pm

    You can’t put macros in code where you want. Read the syntax for a construct in CLHS.

    For example you can’t do:

    (defun foo (make-arg-list 'a 'b) a b)
    

    DEFUN expects an arglist and not a function that creates an arglist.

    Lisp expands macros, where Lisp forms are expected. Where other lists (for example a list of slots) are expected, Lisp does not macroexpand.

    Similar DEFCLASS expects a list of slots and not a function that creates a list of slots.
    Similar for the list of slots, DEFCLASS expects each slot to be either a name or a list describing the slot.

    See the syntax of DEFCLASS:
    http://www.lispworks.com/documentation/HyperSpec/Body/m_defcla.htm

    You can’t also put commas where you want.

    Probabaly a basic Lisp book might help. Read about the Lisp syntax.

    :,foo
    

    above is not meaningful.

    The comma operator puts items into backquoted lists. It does not put items into symbols.

    If you want to create a symbol, you need to call INTERN or MAKE-SYMBOL.

    Solution

    Write a MY-DEFCLASS macro that allows a shorter syntax and expands into DEFCLASS. There are already DEFCLASS* macros that are doing something like that in libraries.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a best-practice or common way in JavaScript to have class members as
Do you have a common base class for Hibernate entities, i.e. a MappedSuperclass with
I have some ASP.NET web services which all share a common helper class they
I have a common code of serializing a class object in my 3-4 methods
Can classes have multiple constructors and/or copy constructors in common-lisp? That is - in
I have the following class in a common jar: public class Common { public
I have class method that returns a list of employees that I can iterate
I have: class MyClass extends MyClass2 implements Serializable { //... } In MyClass2 is
I have class A: public class ClassA<T> Class B derives from A: public class
I have class with internal property: internal virtual StateEnum EnrolmentState { get { ..getter

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.