1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【CommonLisp】クラス名について思うこと2つ

Last updated at Posted at 2024-01-26

ひとつめ(インスタンス名とクラス名は同じでもよいのか?)

(defclass person ()
  ((name :initarg :name
         :accessor person-name)))

(defun main ()
  (let ((person (make-instance 'person :name "Tako")))
    (format t "~a~%" (person-name person)) ; Tako
    (let ((person (make-instance 'person :name "Ika")))
      (format t "~a~%" (person-name person))))) ; Ika

きちんと区別されている。ふむ、相当賢いな。

ふたつめ(命名規約は?)

とは言え、インスタンス名とクラス名が同じだと区別がつきにくい。
Javaなどでは小文字大文字で区別ができるが…
CommonLispはすべて小文字使えよなあ!みたいな同調圧力がある。
どうしたものか…

どうしようもない。

素直に大文字使えばいい!!

CommonLisp小文字愛好家から後ろ指をさされようが、私はクラス名に大文字を使うのがよいと思う。

(defclass Person ()
  ((name :initarg :name
         :accessor person-name)))

(defun main ()
  (let ((person (make-instance 'Person :name "Tako")))
    (format t "~a~%" (person-name person)) ; Tako
    (let ((person (make-instance 'Person :name "Ika")))
      (format t "~a~%" (person-name person))))) ; Ika

なお、Lispリーダーでシンボル名はデフォルトですべて大文字に変換されているのは知っています。

(readtable-case (copy-readtable)) ; :UPCASE

余談

ちなみに私はCLOSのクラスが嫌いです。(構造体でいい。)
でもメソッドは便利だと思います。

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?