3 ms·
> and Python really has no infrastructure for that. Nor do any other dynamic languages like Ruby, Perl, JavaScript, Lua, or PHP, as far as I can tell. This is
by flavio81 9y ago
> and Python really has no infrastructure for that.
Nor do any other dynamic languages like Ruby, Perl, JavaScript, Lua, or PHP, as far as I can tell.
This is not correct.
Common Lisp easily supports structs, and is a dynamic language. No need to specify types for structs, although you can optionally do it in CL if you want.
Here's a textbook example (straight from the CLTL2 book...)
Here a struct is defined (only one line needed), and then the struct is used.
;; structure definition
(defstruct door knob-color width material)
;; Automatically creates the following function:
;; make-door
;; And accesors (can be used for getting and setting)
;; door-width
;; door-knob-color
;; door-width
;; door-material
;; ----------------------
;; 'instancing' the structure into "my-door"
(setq my-door
(make-door :knob-color 'red :width 5.0))
;; accessing
(door-width my-door) => 5.0
;; setting
(setf (door-width my-door) 43.7)
;; accessing
(door-width my-door) => 43.7
(door-knob-color my-door) => red
- chubot 9y agoI don't doubt that CL could do it -- if there's any dynamic language that can support efficient C-style attribute lookup for structs, it's Lisp. (Although I honestly doubt that idiomatic Lisp does this. Idiomatic Lisp might be even slower than idiomatic Python, because I've seen people use alists as structs rather than dictionaries.) Either way, your example doesn't really address what I'm talking about. Python has a struct module too, and it has namedtuple, etc. My claim is that Python doesn't have C-style structs, where attribute names are resolved to indexes AT COMPILE TIME, because it breaks the model of the bytecode compiler and dynamic module system. Lisp is flexible about compile time vs. runtime, so I won't be surprised if it can do that. But again there's no evidence in your example that it does that.
- lispm 9y agoCL implementations offer the indexed access, but include the data in the struct only for few data types. Any good Common Lisp implementation will use fixed indexes. The Common Lisp compiler can inline the accessor and will be using fixed indexes. What Lisp compiles usually not do is inline the data in the struct. Thus the struct slots will contain a pointer to the slot value. Exceptions: small integers, characters, ... Those will fit into the slot and will be stored directly in the struct. That's the point of structures in Common Lisp. Below is an example in Clozure Common Lisp on a ARM processor. As you can see, the compiler can inline the accessor and uses fixed indexes to access the slots... CL-USER> (defstruct person name age) PERSON CL-USER> (defun test (person) (declare (person person) (inline person-name person-age) (optimize (speed 3) (safety 0) (debug 0))) (person-name person)) TEST CL-USER> (disassemble #'test) (mov imm0 (:$ 19)) (stmdb (:! sp) (imm0 vsp fn lr)) (mov fn temp2) (vpush1 arg_z) ;[12] (ldr arg_z (:@ arg_z (:$ 2))) ; name at pos 2 (ldmia (:! sp) (imm0 vsp fn pc)) NIL CL-USER> (defun test (person) (declare (person person) (inline person-name person-age) (optimize (speed 3) (safety 0) (debug 0))) (person-age person)) TEST CL-USER> (disassemble #'test) (mov imm0 (:$ 19)) (stmdb (:! sp) (imm0 vsp fn lr)) (mov fn temp2) (vpush1 arg_z) ;[12] (ldr arg_z (:@ arg_z (:$ 6))) ; age at pos 6 (ldmia (:! sp) (imm0 vsp fn pc)) NIL