Skip to content

Commit

Permalink
Normalizing IO::Handle.say/put/print/note, part 5
Browse files Browse the repository at this point in the history
Basically rince / repeat for .print, like in 88d9822
  • Loading branch information
lizmat committed May 10, 2020
1 parent 483cac3 commit 2b38666
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 10 deletions.
1 change: 0 additions & 1 deletion src/core.c/IO/CatHandle.pm6
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,6 @@ my class IO::CatHandle is IO::Handle {
multi method flush (|) { X::NYI.new(:feature<flush>).throw }
proto method out-buffer (|) {*}
multi method out-buffer (|) { X::NYI.new(:feature<out-buffer>).throw }
proto method print (|) {*}
multi method print (|) { X::NYI.new(:feature<print>).throw }
proto method printf (|) {*}
multi method printf (|) { X::NYI.new(:feature<printf>).throw }
Expand Down
20 changes: 16 additions & 4 deletions src/core.c/IO/Handle.pm6
Original file line number Diff line number Diff line change
Expand Up @@ -655,16 +655,28 @@ my class IO::Handle {
self.print(sprintf |c);
}

proto method print(|) {*}
multi method print(Junction:D \j) { j.THREAD: { self.print: $_ } }
multi method print(IO::Handle:D: Str:D \x --> True) {
$!decoder
?? self.WRITE($!encoder.encode-chars(x))
!! X::IO::BinaryMode.new(:trying<print>).throw
}
multi method print(IO::Handle:D: **@list is raw --> True) { # is raw gives List, which is cheaper
self.print(@list.join);
multi method print(IO::Handle:D: \x --> True) {
$!decoder
?? self.WRITE($!encoder.encode-chars(x.Str))
!! X::IO::BinaryMode.new(:trying<print>).throw
}
multi method print(IO::Handle:D: | --> True) {
if $!decoder {
my Mu $args := nqp::p6argvmarray;
nqp::shift($args);
self.WRITE($!encoder.encode-chars(
nqp::join("",Rakudo::Internals.StrList2list_s($args))))
}
else {
X::IO::BinaryMode.new(:trying<print>).throw;
}
}
multi method print(Junction:D \j) { j.THREAD: {self.print: $_} }

multi method put(Junction:D \j) { j.THREAD: { self.put: $_ } }
multi method put(IO::Handle:D: --> True) {
Expand Down
6 changes: 5 additions & 1 deletion src/core.c/Mu.pm6
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,7 @@ Perhaps it can be found at https://docs.raku.org/type/$name"
proto method say(|) {*}
proto method put(|) {*}
proto method note(|) {*}
proto method print(|) {*}

# Handle the typical "foo.say"
multi method say() {
Expand Down Expand Up @@ -745,7 +746,10 @@ Perhaps it can be found at https://docs.raku.org/type/$name"
}
}

method print() { print(self) }
# Handle the typical "foo.print"
multi method print() {
$*OUT.print: self.Str
}

method gistseen(Mu:D \SELF: $id, $gist, *%named) {
if nqp::not_i(nqp::isnull(nqp::getlexdyn('$*gistseen'))) {
Expand Down
11 changes: 7 additions & 4 deletions src/core.c/io_operators.pm6
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ my class IO::ArgFiles { ... }

proto sub print(|) {*}
multi sub print(--> True) { } # nothing to do
multi sub print(Junction:D \j) { j.THREAD(&print) }
multi sub print(Str:D \x) { $*OUT.print(x) }
multi sub print(\x) { $*OUT.print(x.Str) }
multi sub print(**@args is raw) { $*OUT.print: @args.join }
multi sub print(Junction:D \j) { j.THREAD(&print) }
multi sub print(Str:D \x) { $*OUT.print(x) }
multi sub print(\x) { $*OUT.print(x.Str) }
multi sub print(|) {
$*OUT.print:
nqp::join("",Rakudo::Internals.StrList2list_s(nqp::p6argvmarray))
}

# To ensure that classes that mimic the $*OUT / $*ERR API (which are only
# required to provide a ".print" method), all logic is done in the subs
Expand Down

0 comments on commit 2b38666

Please sign in to comment.