On 11/02/2020 10:56, David Santiago wrote: > Hi! > > Can someone explain me why this doesn't work: > > my Blob $read; > $read ~= $socket.read(1024); > > Dies with error: > > X::Buf::AsStr: Cannot use a Buf as a string, but you called the Stringy > method on it > > This also doesn't work: > > my Buf $read; > $read ~= $socket.read(1024); > > Dies with the same error as above. > > > But this works? > > my Blob $read = Buf.new; > $read ~= $socket.read(1024); > > > Best regards, > David Santiago
Hi David, the important difference is that in the first two examples $read contains an undefined object, either Blob or Buf. In the last one it contains an instance. The operator ~= uses the one-argument form of ~ to build an initial value to calculate with. The reason is that with *= you want to start with 1, but with += you want to start with 0, and so on. However, ~ being called with no options doesn't realize you really want a Buf or Blob, it just gives the empty string by default, and then the next thing that happens is you get "" ~ $socket.read(1024) and that complains that you're mixing strings and bufs. I'm not sure if there is a good solution for inside rakudo or the raku language. Setting an initial value for the variable is one correct way to do this, though. Hope that helps! - Timo