Save data structures directly (not as pointer)

Discussion of Common Lisp
Post Reply
porky11
Posts: 25
Joined: Fri May 02, 2014 6:46 am

Save data structures directly (not as pointer)

Post by porky11 » Sun Feb 08, 2015 5:51 pm

In C(++) its also possible to save datastructures directly (not as pointers).
Is this possible in CL (maybe via libaries)?
for example if I have a struct, that can have different number of dimensions, I would do it this way:

Code: Select all

(defstruct mover
  (pos nil :type vector)
  (vel nil :type vector))
and make the vectors of different size, dependent on the dimension I use, but this is slower than special structs like this:

Code: Select all

(defstruct mover2d
  px py
  vx vy)

(defstruct mover3d
  px py pz
  vx vy vz)
in c++ I could use generics (templates) to change the object

Code: Select all

template <int S>
struct mover{
  int pos[S];
  int vel[S];
}
instead of using pointers

is a similar behaviour possible in CL or would I have to write own macros that work this way

Code: Select all

(my-defstruct-template mover p v)
(macroexpand '(my-defstruct moverxd (mover x x)))
=>(defstruct moverxd
    p0 .. px
    v0 .. vx)

David Mullen
Posts: 78
Joined: Mon Dec 01, 2014 12:29 pm
Contact:

Re: Save data structures directly (not as pointer)

Post by David Mullen » Mon Feb 09, 2015 2:05 pm

C++ templates are type-safe and Common Lisp is dynamically typed, so there's no direct comparison to be made here. If you want to work with C-style data structures then your best bet is to use the Foreign Function Interface of your Common Lisp implementation.

nuntius
Posts: 538
Joined: Sat Aug 09, 2008 10:44 am
Location: Newton, MA

Re: Save data structures directly (not as pointer)

Post by nuntius » Mon Feb 09, 2015 11:04 pm

Lisp macros provide features equivalent to C++ templates and preprocessor macros.
You probably need to define a custom macro for what you want.

Post Reply