5
\$\begingroup\$

I'm writing some simple emacs tools for visual studio solutions.

I've got a function sln-process-csproj-file. This function takes the path to a project, and returns a tuple that's just a data model of the innards of the project.

Right now the return value on the last line of that function is `(,project-name ,project-path ,project-files).

I have written three getter functions to use with the list:

(defun sln-project-name (project)
 (nth 0 project))
(defun sln-project-path (project)
 (nth 1 project))
(defun sln-project-files (project)
 (nth 2 project))

Does this getter and setter boilerplate code match best practices? Is there a shorter way to write it?

James Khoury
3,1651 gold badge25 silver badges51 bronze badges
asked Feb 19, 2014 at 0:35
\$\endgroup\$
0

1 Answer 1

8
\$\begingroup\$

Yes, it can be shortened by using defstruct. An example from the docs:

 (cl-defstruct person name age sex)
 (setq dave (make-person :name "Dave" :sex 'male))
 ⇒ [cl-struct-person "Dave" nil male]
 (setq other (copy-person dave))
 ⇒ [cl-struct-person "Dave" nil male]
 (eq dave other)
 ⇒ nil
 (eq (person-name dave) (person-name other))
 ⇒ t
 (person-p dave)
 ⇒ t
 (person-p [1 2 3 4])
 ⇒ nil
 (person-p "Bogus")
 ⇒ nil
 (person-p '[cl-struct-person counterfeit person object])
 ⇒ t

Alternatively, if you want something more heavy weight, there is a CLOS-like object system.

answered Feb 19, 2014 at 1:05
\$\endgroup\$
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.