ref: d2cfed4135e61f306428a2c39eeea468780bc491
parent: 7b4b0a6f2b186ab516a1668ac2cf86fb639d1e85
author: Sigrid Solveig Haflínudóttir <sigrid@ftrv.se>
date: Thu Apr 17 12:40:29 EDT 2025
defstruct: test and fix :conc-name variants
--- a/boot/sl.boot
+++ b/boot/sl.boot
@@ -19,7 +19,7 @@
(data :size decompressed-bytes)) defstruct ((name doc options… (slot-1 DEFAULT) slot-2 (slot-3
:read-only))
(name (:type 'vec) (:named T) (:constructor T) (:conc-name
- NIL)
+ T)
(:predicate T) . slots)) compare ((x y)) buffer (NIL) num? ((v)) add-exit-hook ((fun)) rand-float (NIL) builtin? ((v)) set-car! ((cell
new-first)) cons? ((v)) doc-group ((group-name doc)) 1+ ((n)) aref ((sequence subscript0 . rest)) zero? ((x)) vec (rest) >= ((a . rest)) sym? ((v)) void? ((x)) length= ((seq
n)) positive? ((x)) doc-for ((term . doc)) aset! ((sequence subscripts… new-value)) car ((lst)) <= ((a . rest)) str (term) cons ((first
@@ -32,7 +32,7 @@
(level 0))) *prompt* (NIL) eq? ((a b)) getprop ((symbol key (def NIL))) vm-stats (NIL) * (rest) putprop ((symbol
key val)) io->str ((io))) *doc* #table(identity "Return `x`." (doc group vm) "VM-related functions." bound? "Return `T` if `symbol` has a value associated with it, `NIL` otherwise." sym-set-doc "Set the documentation for the symbol." io-eof? "Return `T` if `io` is currently in the \"end of file\" state, `NIL`\notherwise." < "Return `T` if the arguments are in strictly increasing order (next\none is greater than the previous one). With a single argument\nthe result is always `T`." cadr "Shorthand for `(car (cdr cell))`, that is, _first element of the\nsecond element_.\n\nExamples:\n\n (cadr '(1 2 3)) → 2\n (cadr '(1)) → NIL\n (cadr NIL) → NIL" nan? "Return `T` if `v` is a floating point representation of NaN, either\nnegative or positive, `NIL` otherwise." for "Call the function `fn` with a single integer argument, starting from\n`min` and ending with `max`.\n\nExamples:\n\n (for 0 2 (λ (i) (print (- 2 i)))) → 210" fixnum? "Return `T` if `v` is of a fixnum type, `NIL` otherwise." exit "Terminate the process with the specified status. Does not return.\nThe status is expected to be a string in case of an error.\n\nExamples:\n\n (exit) ; exit with status 0 (nil on Plan 9)\n (exit \"error\") ; exit with status 1 (\"error\" on Plan 9)" (doc
group compare) "Comparison operators." (doc group compress) "Compression." + "Return sum of the arguments or `0` when none specified." (doc
- group builtin) "Built-in operators." div0 "Return the quotient of two numbers. For non-integers this is\nequivalent to `(div0 (floor a) (floor b))`. The result is always an\ninteger.\n\nExamples:\n\n (div0 7 2) → 3\n (div0 10 -2) → -5\n (div0 6.9 1.9) → 6" lz-unpack "Return decompressed data previously compressed using lz-pack.\n\nEither destination for the decompressed data or the expected size of\nthe decompressed data must be specified. In the latter case a new\narray is allocated." > "Return `T` if the arguments are in strictly decreasing order (previous\none is greater than the next one)." __finish "A function called right before exit by the VM." defstruct "Defines a structure type with a specific name and slots.\n\nThe default underlying type is a \"named\" vector (`:type vec`),\nwhere the first element is the name of the structure's type, the\nrest are the keyworded slot values. A list with slot values alone\ncan be used instead by adding `:type list` option. The list will\nnot contain the name of the struct by default, which can be\nenabled with `:named T` option.\n\nAs an example, the following declaration\n\n (defstruct blah \"Return stuff.\" :doc-group stuff a b (c 1 cset))\n\nGenerates the default constructor definition and accessors:\n\n (make-blah (:a NIL) (:b NIL) (:c 1 cset))\n (blah-a s)\n (blah-b s)\n (blah-c s)\n\nThe constructor can be changed in several ways:\n\n ; disable the constructor altogether\n (defstruct blah :constructor NIL a b c)\n ; only change its name\n (defstruct blah :constructor blargh a b c)\n ; rename AND avoid using keywords\n (defstruct blah :constructor (blah a b c) a b c)\n\nThe option `:conc-name` specifies the slot accessor prefix, which\ndefaults to `structname-`.\n\nDefault predicate can be disabled or its name, which defaults to\n`structname?`, changed:\n\n ; use \"blargh?\" instead of \"blah?\"\n (defstruct blah :predicate blargh? a b c)\n ; without predicate\n (defstruct blah :predicate NIL a b c)" (doc
+ group builtin) "Built-in operators." div0 "Return the quotient of two numbers. For non-integers this is\nequivalent to `(div0 (floor a) (floor b))`. The result is always an\ninteger.\n\nExamples:\n\n (div0 7 2) → 3\n (div0 10 -2) → -5\n (div0 6.9 1.9) → 6" lz-unpack "Return decompressed data previously compressed using lz-pack.\n\nEither destination for the decompressed data or the expected size of\nthe decompressed data must be specified. In the latter case a new\narray is allocated." > "Return `T` if the arguments are in strictly decreasing order (previous\none is greater than the next one)." __finish "A function called right before exit by the VM." defstruct "Defines a structure type with a specific name and slots.\n\nThe default underlying type is a \"named\" vector (`:type vec`), where\nthe first element is the name of the structure's type, the rest are\nthe keyworded slot values. A list with slot values alone can be used\ninstead by adding `:type list` option. The list will not contain the\nname of the struct by default, which can be enabled with `:named T`\noption.\n\nAs an example, the following declaration\n\n (defstruct blah \"Return stuff.\" :doc-group stuff a b (c 1 cset))\n\nGenerates the default constructor definition and accessors:\n\n (make-blah (:a NIL) (:b NIL) (:c 1 cset))\n (blah-a s)\n (blah-b s)\n (blah-c s)\n\nThe constructor can be changed in several ways:\n\n ; disable the constructor altogether\n (defstruct blah :constructor NIL a b c)\n ; only change its name\n (defstruct blah :constructor blargh a b c)\n ; rename AND avoid using keywords\n (defstruct blah :constructor (blah a b c) a b c)\n\nThe option `:conc-name` specifies the slot accessor prefix, which\ndefaults to `structname-`. Prefix can be disabled entirely with\n`:conc-name NIL`.\n\nDefault predicate can be disabled or its name, which defaults to\n`structname?`, changed:\n\n ; use \"blargh?\" instead of \"blah?\"\n (defstruct blah :predicate blargh? a b c)\n ; without predicate\n (defstruct blah :predicate NIL a b c)" (doc
group io) "I/O functionality." compare "Return -1 if `x` is less than `y`, 0 if equal, and `1` if `y` is\ngreater than `x`.\n\nExamples:\n\n (compare 'a 'b) → -1\n (compare 1 1) → 0\n (compare \"b\" \"a\") → 1" buffer "Return an in-memory buffer for I/O, of `io` type.\n\nA buffer can be used for both reading and writing at the same\ntime." num? "Return `T` if `v` is of a numerical type, `NIL` otherwise.\n\nNumerical types include floating point, fixnum, bignum, etc.\nNote: ironically, a NaN value is considered a number by this function\nsince it's only testing the _type_ of the value." add-exit-hook "Puts an one-argument function on top of the list of exit hooks.\n\nOn shutdown each exit hook is called with the exit status as a single\nargument, which is (usually) `NIL` on success and a string describing\nan error otherwise." rand-float "Return a random float on [0.0, 1.0] interval." builtin? "Return `T` if `v` is a built-in function implemented in C, `NIL`\notherwise.\n\nExamples:\n\n (builtin? map) → T\n (builtin? macroexpand) → NIL" set-car! "Modify a cons cell (a list) in-place by putting `new-first` as its\nfirst element (head of the list). Return the modified cons\ncell (list).\n\nExamples:\n\n (def q '(1 2 3 4 5))\n (set-car! q 0) → (0 6 7)\n q → (0 6 7)" doc-group "Define documentation for a group." cons? "Return `T` if `v` is a cons cell, `NIL` otherwise.\n\nExamples:\n\n (cons? 0) → NIL\n (cons? NIL) → NIL\n (cons? '(1)) → T" 1+ "Equivalent to `(+ n 1)`." aref "Return the sequence element specified by the subscripts. The sequence\ncan be an array, vector, a list. Multi-dimensional sequences\nof variating types are also supported.\n\nExamples:\n\n (def a '((1 #(2 (3)) 4)))\n (aref a 0) → (1 (2 (3)) 4)\n (aref a 1) → index 1 out of bounds\n (aref a 0 0) → 1\n (aref a 0 1 0) → 2\n (aref a 0 2) → 4" *properties* "All properties of symbols recorded with `putprop` are recorded in this\ntable." vec "Return a vector constructed of the arguments.\n\nExamples:\n\n (vec) → #() ; empty vector\n (vec 1 2.5 \"a\" 'b) → #(1 2.5 \"a\" b)" >= "Return `T` if the arguments are in non-increasing order (previous\none is greater than or equal to the next one)." sym? "Return `T` if `v` is a symbol, `NIL` otherwise." (doc
group string) "String-related functionality." void? "Return `T` if `x` is `#<void>`, `NIL` otherwise." zero? "Return `T` if `x` is zero." (doc
group list) "Working with lists." positive? "Return `T` if `x` is greater than zero." length= "Perform a bounded length test.\n\nUse this instead of `(= (length seq) n)`, since it avoids unnecessary\nwork and always terminates." doc-for "Define documentation for a top level term.\n\nIf `term` is a function signature and `doc` is not specified, just\nthe signature will be included in the documentation, without\nreplacing any previously defined.\n\nFirst `doc` argument is supposed to be a string with the description\nof the term. The following arguments are expected to be optional tag\npairings that provide grouping for multiple symbols and \"see also\"\nreferences.\n\nUseful in cases where setting the documentation for a term can't\n(or not preferred to) be made during the definition of said term.\nOne of those reasons is that the term is a built-in function\nimplemented in C.\n\nExamples:\n\n (doc-for (func arg (arg2 0))\n \"Return something about the `arg` and `arg2`. This is a short\n description.\n\n This is the longer description, following the short one.\n\n Examples:\n\n (func 0) → T\n (func 1 3) → NIL\"\n :doc-group stuff\n :doc-see func2)\n (doc-for (func arg (:another-variant NIL)))" aset! "Modify the sequence element specified by the subscripts and return the\nnew value. The sequence can be an array, vector, a list.\nMulti-dimensional sequences of variating types are also supported.\n\nExamples:\n\n (def a '((1 #(2 (3)) 4)))\n (aset! a 1 'x) → index 1 out of bounds\n (aset! a 0 0 'x) → x\n a → ((x #(2 (3)) 4))\n (aset! a 0 1 9) → 9\n a → ((x #(9 (3)) 4))" car "Return the first element of a cons cell (head of a list) or `NIL` if\nnot available.\n\nExamples:\n\n (car NIL) → NIL\n (car '(1 2 3)) → 1\n (car '(1 . 2)) → 1" *builtins* "VM instructions as closures." str "Return concatenation of terms formatted as strings.\n\nThis is equivalent to `(princ terms…)`, except the string is\nreturned, rather than printed.\n\nExamples:\n\n (str \"a\" 'b 1 #(0)) → \"ab1#(0)\"" cons "Return a cons cell containing two arguments.\n\nExamples:\n\n (cons 1 2) → (1 . 2)\n (cons 1 '(2)) → (1 2)\n (cons 1 (cons 2 (cons 3 NIL))) → (1 2 3)" - "Return the result of subtraction. With only one argument a\nnegation is performed.\n\nExamples:\n\n (- 1.5) → -1.5\n (- 3 2) → 1" remprop "Remove a property value associated with the symbol." <= "Return `T` if the arguments are in non-decreasing order (previous\none is less than or equal to the next one)." rand "Return a random non-negative fixnum on its maximum range." void "Return the constant `#<void>` while ignoring any arguments.\n\n`#<void>` is mainly used when a function has side effects but does not\nproduce any meaningful value to return, so even though `T` or `NIL` could\nbe returned instead, in case of `#<void>` alone, REPL will not print\nit." negative? "Return `T` if `x` is negative." Instructions "VM instructions mapped to their encoded byte representation." file "Open a file for I/O.\n\nAn `io` object is returned. Without any modes specified the file\nis opened in read-only mode." rand-double "Return a random double on interval [0.0, 1.0]." (doc
@@ -78,7 +78,7 @@
#fn(get) *properties* :kind *doc-extra* filter #fn("n10<20Q:" #(:doc-fmt))
#fn("n10<20Q:" #(:doc-see)) princ foldl #fn("n20=161:") newline "See also:" #fn("n1A<0=700=21522263:" #(getprop
*formals-list* " ")) "Members:" #fn("n1A<070021522263:" #(getprop *formals-list* " ")) void
- "no help for " #fn(str) " " "" " (undefined)")) defstruct #fn("O10005000*///z6W1000J7021?14W2000;J60D?24W3000J60D?34W4000J60q?44W5000;J60D?54IIb=228=230>1_5142224?>5147586518?<8?=268A5127288A528>8A51121C60D@C0129C60q@907:2;1528E3B082;J9047:2<51@;082;35048;;36040e185DQ;3:042=02>52;J504858G;3L048F3708G@A08<3;07:2?51@30q8E3:02@0e2@7002Ae283DQ83;3\\0483H;3M0483DQ;3:042=2B052;J504838DP;J5048384;J:042C02D52II222E8E18F8K848<8G08A>9?M514222F8E108C8F>5?N5148@3G07G02H8@2I8MPe15252@30q42J2Ke18H3{02L8H2Me28E3E02N2O2Me22P8Ie2e3@V02Q2R2S2MEe32P0e2e32T2U2Me27V8B51e3e3e3@30qe18K3C02L8K<8N8K=51e3@30qe18E3U02W2P0e22P2Xe28J3808K<@808N8D51e4@30qe12Y7Z2[8=8D8C8L8E8H8I8F0>98B525165:" #(#(:constructor
+ "no help for " #fn(str) " " "" " (undefined)")) defstruct #fn("O10005000*///z6W1000J7021?14W2000;J60D?24W3000J60D?34W4000J60D?44W5000;J60D?54IIb=228=230>1_5142224?>5147586518?<8?=268A5127288A528>8A51121C60D@C0129C60q@907:2;1528E3B082;J9047:2<51@;082;35048;;36040e185DQ;3:042=02>52;J504858G;3L048F3708G@A08<3;07:2?51@30q8E3:02@0e2@7002Ae283DQ83;3\\0483H;3M0483DQ;3:042=2B052;J504838DP;J5048384DQ;3:042C02D52;J50484II222E8E18F8K848<8G08A>9?M514222F8E108C8F>5?N5148@3G07G02H8@2I8MPe15252@30q42J2Ke18H3{02L8H2Me28E3E02N2O2Me22P8Ie2e3@V02Q2R2S2MEe32P0e2e32T2U2Me27V8B51e3e3e3@30qe18K3C02L8K<8N8K=51e3@30qe18E3U02W2P0e22P2Xe28J3808K<@808N8D51e4@30qe12Y7Z2[8=8D8C8L8E8H8I8F0>98B525165:" #(#(:constructor
2 :predicate 4 NIL NIL :type 0 :named 1 :conc-name 3 NIL NIL) vec #0#
#fn("n17005121220A>28552485:" #(cddr #fn(for-each)
#fn("n17002152340q:722324A<25F2605661:" #(member (:read-only)
@@ -93,7 +93,7 @@
:conc-name :predicate) fmt) #fn("n1200A3Y021Fe12223e2e12292e2e1247526q93535154@f0943S02127e12122e124945152e124935153@@02127e124935152e3:" #(λ
#fn(nconc) quote %struct% #fn(copy-list) foldr #fn("n2202105201PP:" #(#fn(sym) ":")) list) make-constructor)
sym-set-doc #fn(append) :doc-fmt #fn(nconc) begin def s equal? type-of quote and eq? aref =
- length 1+ putprop constructor #fn(copy-list) map-int #fn("n1A<70F05251709205221938652943<0r20i2KM@30022872324q25e3e3953K0269523e2272896e223e3e3@30q292:25e22;2397360K@30E88Me37<2=85523O02>2?2@2886e22A2898e22Be6e2@G02C2397360K@30E88M24e4e4e4:" #(list-ref
+ length 1+ putprop constructor #fn(copy-list) map-int #fn("n1A<70F052517092052933=021938652@4086943<0r20i2KM@30022872324q25e3e3953K0269523e2272896e223e3e3@30q292:25e22;2397360K@30E88Me37<2=85523O02>2?2@2886e22A2898e22Be6e2@G02C2397360K@30E88M24e4e4e4:" #(list-ref
#fn(sym) def s v v-supplied? unless type-error quote if not aref member :read-only error str "slot "
" in struct " " is :read-only" aset!)))) bcode:ctable #fn("n1200Ke3:" #(aref)) with-output-to #fn("z12021e1220e2e1e12315163:" #(#fn(nconc)
with-bindings *io-out* #fn(copy-list))) catch #fn("n22012122e123242522e2262722e22829e2e3262:22e20e3e42;22e22<22e2e4e3e3:" #(trycatch
--- a/src/system.sl
+++ b/src/system.sl
@@ -1073,17 +1073,17 @@
(defmacro (defstruct name (:type 'vec)
(:named T named-supplied)
(:constructor T)
- (:conc-name NIL)
+ (:conc-name T)
(:predicate T predicate-supplied)
. slots)
«Defines a structure type with a specific name and slots.
- The default underlying type is a "named" vector (`:type vec`),
- where the first element is the name of the structure's type, the
- rest are the keyworded slot values. A list with slot values alone
- can be used instead by adding `:type list` option. The list will
- not contain the name of the struct by default, which can be
- enabled with `:named T` option.
+ The default underlying type is a "named" vector (`:type vec`), where
+ the first element is the name of the structure's type, the rest are
+ the keyworded slot values. A list with slot values alone can be used
+ instead by adding `:type list` option. The list will not contain the
+ name of the struct by default, which can be enabled with `:named T`
+ option.
As an example, the following declaration
@@ -1106,7 +1106,8 @@
(defstruct blah :constructor (blah a b c) a b c)
The option `:conc-name` specifies the slot accessor prefix, which
- defaults to `structname-`.
+ defaults to `structname-`. Prefix can be disabled entirely with
+ `:conc-name NIL`.
Default predicate can be disabled or its name, which defaults to
`structname?`, changed:
@@ -1186,8 +1187,9 @@
slots-kw))
constructor))] ; anything else means custom name and args
; accessor prefix
- [access (or conc-name
- (str name "-"))]}
+ [access (or (and (eq? conc-name T)
+ (str name "-"))
+ conc-name)]}
(def (fmt doc)
(let* {[cut (str-find doc "\n\n")]
[hd (if cut (str-sub doc 0 cut) doc)]
@@ -1242,7 +1244,7 @@
; accessor per slot
,@(map-int (λ (i) [let* {[opts (slot-opts (list-ref slots-kw i))]
[fld (list-ref slots-car i)]
- [fun (sym access fld)]
+ [fun (if access (sym access fld) fld)]
[iv (if isvec (+ (* 2 i) 1) i)]}
`(def (,fun s (v NIL v-supplied?))
,(when is?
--- a/test/defstruct.sl
+++ b/test/defstruct.sl
@@ -132,3 +132,23 @@
(assert (= (lcp-a lcpv) 1))
(assert (eh? lcpv))
(assert (not (eh? lnpv)))
+
+; renamed conc-name
+(defstruct scn :conc-name scn/ a b)
+(assert (bound? 'scn/a))
+(assert (not (bound? 'scn-a)))
+(assert (bound? 'scn/b))
+(assert (not (bound? 'scn-b)))
+(def scnv (make-scn :a 1 :b 2))
+(assert (= (scn/a scnv) 1))
+(assert (= (scn/b scnv) 2))
+
+; nil conc-name
+(defstruct sncn :conc-name NIL aaa bbb)
+(assert (bound? 'aaa))
+(assert (not (bound? 'sncn-a)))
+(assert (bound? 'bbb))
+(assert (not (bound? 'sncn-b)))
+(def sncnv (make-sncn :aaa 2 :bbb 3))
+(assert (= (aaa sncnv) 2))
+(assert (= (bbb sncnv) 3))