<commit_before>USING: help.markup help.syntax generic kernel.private parser
kernel quotations namespaces sequences arrays effects
generic.standard classes.builtin slots.private classes strings math
assocs byte-arrays alien classes.tuple ;
IN: slots

ARTICLE: "accessors" "Slot accessors"
"For every tuple slot, a " { $emphasis "reader" } " method is defined in the " { $vocab-link "accessors" } " vocabulary. The reader is named " { $snippet { $emphasis "slot" } ">>" } " and given a tuple, pushes the slot value on the stack."
$nl
"Writable slots - that is, those not attributed " { $link read-only } " - also have a " { $emphasis "writer" } ". The writer is named " { $snippet "(>>" { $emphasis "slot" } ")" } " and stores a value into a slot. It has stack effect " { $snippet "( value object -- )" } ". If the slot is specialized to a specific class, the writer checks that the value being written into the slot is an instance of that class first. See " { $link "tuple-declarations" } " for details."
$nl
"In addition, two utility words are defined for each writable slot."
$nl
"The " { $emphasis "setter" } " is named " { $snippet ">>" { $emphasis "slot" } } " and stores a value into a slot. It has stack effect " { $snippet "( object value -- object )" } "."
$nl
"The " { $emphasis "changer" } " is named " { $snippet "change-" { $emphasis "slot" } } ". It applies a quotation to the current slot value and stores the result back in the slot; it has stack effect " { $snippet "( object quot -- object )" } "."
$nl
"Since the reader and writer are generic, words can be written which do not depend on the specific class of tuple passed in, but instead work on any tuple that defines slots with certain names."
$nl
"In most cases, using the setter is preferred over the writer because the stack effect is better suited to the common case where the tuple is needed again, and where the new slot value was just computed and so is at the top of the stack. For example, consider the case where you want to create a tuple and fill in the slots with literals. The following version uses setters:"
{ $code
    "<email>"
    "    \"Happy birthday\" >>subject"
    "    { \"<EMAIL>\" } >>to"
    "    \"<EMAIL>\" >>from"
    "send-email"
}
"The following uses writers, and requires some stack shuffling:"
{ $code
    "<email>"
    "    \"Happy birthday\" over (>>subject)"
    "    { \"<EMAIL>\" } over (>>to)"
    "    \"<EMAIL>\" over (>>from)"
    "send-email"
}
"Even if some of the slot values come from the stack underneath the tuple being constructed, setters win:"
{ $code
    "<email>"
    "    swap >>subject"
    "    swap >>to"
    "    \"<EMAIL>\" >>from"
    "send-email"
}
"The above has less shuffling than the writer version:"
{ $code
    "<email>"
    "    [ (>>subject) ] keep"
    "    [ (>>to) ] keep"
    "    \"<EMAIL>\" over (>>from)"
    "send-email"
}
"The changer word abstracts a common pattern where a slot value is read then stored again; so the following is not idiomatic code:"
{ $code
    "find-manager"
    "    salary>> 0.75 * >>salary"
}
"The following version is preferred:"
{ $code
    "find-manager"
    "    [ 0.75 * ] change-salary"
}
{ $see-also "slots" "mirrors" } ;

ARTICLE: "slot-initial-values" "Initial values of slots"
"An initial value for a slot can be specified with the " { $link initial: } " slot declaration attribute. For certain classes, the initial value is optional; in these cases, it does not need to be specified. For others, it is required. Initial values can be used independently of class declaration, but if specified, the value must satisfy the class predicate."
$nl
"The following classes have default initial values:"
{ $table
    { { { $link f } } { $link f } }
    { { { $link fixnum } } { $snippet "0" } }
    { { { $link float } } { $snippet "0.0" } }
    { { { $link string } } { $snippet "\"\"" } }
    { { { $link byte-array } } { $snippet "B{ }" } }
    { { { $link simple-alien } } { $snippet "BAD-ALIEN" } }
}
"All other classes are handled with one of two cases:"
{ $list
    { "If the class is a union or mixin class which " { $emphasis "contains" } " one of the above known classes, then the initial value of the class is that of the known class, with preference given to classes earlier in the list. For example, if the slot is declared " { $link object } " (this is the default), the initial value is " { $link f } ". Similarly for " { $link sequence } " and " { $link assoc } "." }
    { "If the class is a tuple class, the initial value of the slot is a new, shared instance of the class created with " { $link new } "." }
    { "Otherwise, a " { $link no-initial-value } " error is thrown. In this case, an initial value must be specified explicitly using " { $link initial: } "." }
}
"A word can be used to check if a class has an initial value or not:"
{ $subsections initial-value } ;

ARTICLE: "slots" "Low-level slot operations"<commit_msg>slots: another bootstrap fix<commit_after>USING: help.markup help.syntax generic kernel.private parser
kernel quotations namespaces sequences arrays effects
generic.standard classes.builtin slots.private classes strings math
assocs byte-arrays alien classes.tuple ;
IN: slots

ARTICLE: "accessors" "Slot accessors"
"For every tuple slot, a " { $emphasis "reader" } " method is defined in the " { $vocab-link "accessors" } " vocabulary. The reader is named " { $snippet { $emphasis "slot" } ">>" } " and given a tuple, pushes the slot value on the stack."
$nl
"Writable slots - that is, those not attributed " { $link read-only } " - also have a " { $emphasis "writer" } ". The writer is named " { $snippet "(>>" { $emphasis "slot" } ")" } " and stores a value into a slot. It has stack effect " { $snippet "( value object -- )" } ". If the slot is specialized to a specific class, the writer checks that the value being written into the slot is an instance of that class first. See " { $link "tuple-declarations" } " for details."
$nl
"In addition, two utility words are defined for each writable slot."
$nl
"The " { $emphasis "setter" } " is named " { $snippet ">>" { $emphasis "slot" } } " and stores a value into a slot. It has stack effect " { $snippet "( object value -- object )" } "."
$nl
"The " { $emphasis "changer" } " is named " { $snippet "change-" { $emphasis "slot" } } ". It applies a quotation to the current slot value and stores the result back in the slot; it has stack effect " { $snippet "( object quot -- object )" } "."
$nl
"Since the reader and writer are generic, words can be written which do not depend on the specific class of tuple passed in, but instead work on any tuple that defines slots with certain names."
$nl
"In most cases, using the setter is preferred over the writer because the stack effect is better suited to the common case where the tuple is needed again, and where the new slot value was just computed and so is at the top of the stack. For example, consider the case where you want to create a tuple and fill in the slots with literals. The following version uses setters:"
{ $code
    "<email>"
    "    \"Happy birthday\" >>subject"
    "    { \"<EMAIL>\" } >>to"
    "    \"<EMAIL>\" >>from"
    "send-email"
}
"The following uses writers, and requires some stack shuffling:"
{ $code
    "<email>"
    "    \"Happy birthday\" over (>>subject)"
    "    { \"<EMAIL>\" } over (>>to)"
    "    \"<EMAIL>.com\" over (>>from)"
    "send-email"
}
"Even if some of the slot values come from the stack underneath the tuple being constructed, setters win:"
{ $code
    "<email>"
    "    swap >>subject"
    "    swap >>to"
    "    \"<EMAIL>\" >>from"
    "send-email"
}
"The above has less shuffling than the writer version:"
{ $code
    "<email>"
    "    [ (>>subject) ] keep"
    "    [ (>>to) ] keep"
    "    \"<EMAIL>\" over (>>from)"
    "send-email"
}
"The changer word abstracts a common pattern where a slot value is read then stored again; so the following is not idiomatic code:"
{ $code
    "find-manager"
    "    salary>> 0.75 * >>salary"
}
"The following version is preferred:"
{ $code
    "find-manager"
    "    [ 0.75 * ] change-salary"
}
{ $see-also "slots" "mirrors" } ;

ARTICLE: "slot-initial-values" "Initial values of slots"
"An initial value for a slot can be specified with the " { $link initial: } " slot declaration attribute. For certain classes, the initial value is optional; in these cases, it does not need to be specified. For others, it is required. Initial values can be used independently of class declaration, but if specified, the value must satisfy the class predicate."
$nl
"The following classes have default initial values:"
{ $table
    { { { $link f } } { $link f } }
    { { { $link fixnum } } { $snippet "0" } }
    { { { $link float } } { $snippet "0.0" } }
    { { { $link string } } { $snippet "\"\"" } }
    { { { $link byte-array } } { $snippet "B{ }" } }
    { { { $link pinned-alien } } { $snippet "BAD-ALIEN" } }
}
"All other classes are handled with one of two cases:"
{ $list
    { "If the class is a union or mixin class which " { $emphasis "contains" } " one of the above known classes, then the initial value of the class is that of the known class, with preference given to classes earlier in the list. For example, if the slot is declared " { $link object } " (this is the default), the initial value is " { $link f } ". Similarly for " { $link sequence } " and " { $link assoc } "." }
    { "If the class is a tuple class, the initial value of the slot is a new, shared instance of the class created with " { $link new } "." }
    { "Otherwise, a " { $link no-initial-value } " error is thrown. In this case, an initial value must be specified explicitly using " { $link initial: } "." }
}
"A word can be used to check if a class has an initial value or not:"
{ $subsections initial-value } ;

ARTICLE: "slots" "Low-level slot operations"
{'fraction_non_alphanumeric': 0.12600020257267294, 'fraction_numerical': 0.0018231540565177757, 'mean_word_length': 3.30239651416122, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 91, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2984945', 'n_tokens_mistral': 2933, 'n_tokens_neox': 2868, 'n_words': 1334}