https://github.com/akkartik/mu/blob/master/059to_text.mu

     1 # A couple of variants of 'to-text' that we'll use implicitly in stashes (see
     2 # later layers).
     3 #
     4 # Mu code might specialize them to be smarter, but I don't anticipate any need
     5 # beyond specializing 'to-text' itself.
     6
     7 # 'shorter' variant of to-text, when you want to enable some sort of trimming
     8 # define it to be identical to 'to-text' by default
     9 def to-text-line x:_elem -> y:text [
    10   local-scope
    11   load-inputs
    12   y <- to-text x
    13 ]
    14
    15 # variant for arrays (since we can't pass them around otherwise)
    16 def array-to-text-line x:&:@:_elem -> y:text [
    17   local-scope
    18   load-inputs
    19   y <- to-text *x
    20 ]
    21
    22 scenario to-text-line-early-warning-for-static-dispatch [
    23   x:text <- to-text-line 34
    24   # just ensure there were no errors
    25 ]
    26
    27 scenario array-to-text-line-early-warning-for-static-dispatch [
    28   n:&:@:num <- new number:type, 3
    29   x:text <- array-to-text-line n
    30   # just ensure there were no errors
    31 ]
    32
    33 # finally, a specialization for single characters
    34 def to-text c:char -> y:text [
    35   local-scope
    36   load-inputs
    37   y <- new character:type, 1/capacity
    38   *y <- put-index *y, 0, c
    39 ]
    40
    41 scenario character-to-text [
    42   1:char <- copy 111/o
    43   2:text <- to-text 1:char
    44   3:@:char <- copy *2:text
    45   memory-should-contain [
    46     3:array:character <- [o]
    47   ]
    48 ]