Re: Question about Blob and Buf
Hi Timo, Thanks for the answer: > the liskov substitution principle I didn't knew about this principle. I'm now going down the rabbit hole. Is this always the case for all the derived classes in Raku? Best regards, David Santiago Timo Paulssen escreveu no dia terça, 11/02/2020 à(s) 13:32: > > On 11/02/2020 14:14, David Santiago wrote: > > Awesome explanation! Thank you! > > > > BTW, > >> my Blob $read = Buf.new; > > Is it creating either a Blob or a Buf? > > > > Regards, > > David Santiago > > > Hi David, > > "my Blob $read" will define the variable $read to 1) only accept things > that typecheck against Blob, and 2) has the starting value of Blob (the > Blob type object). Assigning Buf.new to it will assign the newly created > Buf object to the variable, because a Buf Is-A Blob (by the liskov > substitution principle, everywhere you can use a Blob, you can also use > a Buf, but not the other way around). > > BTW, assigning to a variable with a % or @ sigil behaves differently. > That is called "list assignment" and will actually use whatever type the > % or @ variable is defined to use (Hash and Array by default) and store > the values from the right-hand side of the assignment into the existing > object. This is why "my %foo = SetHash.new()" will result in a > Hash. For this example, you would want "my %foo is SetHash = " > instead. > > Hope that clears things up > - Timo >
Re: Question about Blob and Buf
The problem is that you are using ~ with an uninitialized Buf/Blob my Buf $read; $read ~ Buf.new; # Use of uninitialized value element of type Buf in string context. Note that it is not complaining about it being a Buf. It is complaining about it being uninitialized. If you initialize it then it works just fine. my Buf $read .= new; $read ~ Buf.new; It will also work with ~= my Buf $read .= new; $read ~= Buf.new( 'a'.ord, 'b'.ord ); $read ~= Blob.new( 'c'.ord, 'd'.ord ); say $read.decode; # abcd It also works with Blob, but you may not want to do that if you plan on modifying the contents with something like `.subbuf-rw()`. On Tue, Feb 11, 2020 at 3:56 AM David Santiago wrote: > A 11 de fevereiro de 2020 10:47:34 CET, David Santiago > escreveu: > >A 11 de fevereiro de 2020 09:46:06 CET, David Santiago < > deman...@gmail.com> escreveu: > >> > >>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! > > > >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! > > 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 > > -- > Sent from my Android device with K-9 Mail. Please excuse my brevity. >
Re: Question about Blob and Buf
On 11/02/2020 14:14, David Santiago wrote: > Awesome explanation! Thank you! > > BTW, >> my Blob $read = Buf.new; > Is it creating either a Blob or a Buf? > > Regards, > David Santiago Hi David, "my Blob $read" will define the variable $read to 1) only accept things that typecheck against Blob, and 2) has the starting value of Blob (the Blob type object). Assigning Buf.new to it will assign the newly created Buf object to the variable, because a Buf Is-A Blob (by the liskov substitution principle, everywhere you can use a Blob, you can also use a Buf, but not the other way around). BTW, assigning to a variable with a % or @ sigil behaves differently. That is called "list assignment" and will actually use whatever type the % or @ variable is defined to use (Hash and Array by default) and store the values from the right-hand side of the assignment into the existing object. This is why "my %foo = SetHash.new()" will result in a Hash. For this example, you would want "my %foo is SetHash = " instead. Hope that clears things up - Timo
Re: Question about Blob and Buf
Awesome explanation! Thank you! BTW, > my Blob $read = Buf.new; Is it creating either a Blob or a Buf? Regards, David Santiago -- Sent from my Android device with K-9 Mail. Please excuse my brevity.
Re: Question about Blob and Buf
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
Re: Question about Blob and Buf
A 11 de fevereiro de 2020 12:03:19 CET, Simon Proctor escreveu: >Ok I 100% don't know after trying this out : > >my Buf $a = Buf.new(1,2,3); >my Blob $b = Blob.new(4,5,6); >$a ~= $b; >say $a > >And it worked fine so... I dunno. > > >On Tue, 11 Feb 2020 at 11:00, Simon Proctor wrote: > >> I think the problem is IO::Socket.read() returns a Blob not a Buf. >> >> ~ has a Buf, Buf variant : >> https://docs.raku.org/language/operators#infix_~ >> >> But not a Blob one. Buf does Blob but not vice versa. >> >> I think you need to transform the output from .read into a Buf if you want >> to use the ~= how you want to. >> >> Would this work? >> my Blob $read = Buf.new; >> $read ~= Buf.new( $socket.read(1024) ); >> >> >> On Tue, 11 Feb 2020 at 10:46, Kevin Pye wrote: >> >>> >>> ~ works fine for concatenating Bufs; For example: >>> >>> my $a = Buf.new(1,2,3); >>> my $b = $a ~ Buf.new(4,5,6) >>> >>> will assign correctly to $b. >>> >>> I can't work out what the problem is here, despite trying various >>> combinations. Perhaps socket isn't really returning a Blob? >>> >>> Kevin. >>> >>> On Tue, 11 Feb 2020 at 21:01, JJ Merelo wrote: >>> You are using ~, which stringifies. Bufs are not strings: you need to decode them to concatenate it to a string. If what you want is to concatenate the buffer, probably ,= will work (not sure about this, would have to check), or any other operator that works on Positionals. JJ El mar., 11 feb. 2020 a las 10:56, David Santiago () escribió: > A 11 de fevereiro de 2020 10:47:34 CET, David Santiago < > deman...@gmail.com> escreveu: > >A 11 de fevereiro de 2020 09:46:06 CET, David Santiago < > deman...@gmail.com> escreveu: > >> > >>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! > > > >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! > > 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 > > -- > Sent from my Android device with K-9 Mail. Please excuse my brevity. > -- JJ >>> >> >> -- >> Simon Proctor >> Cognoscite aliquid novum cotidie >> >> http://www.khanate.co.uk/ >> > > Hi! I'm still confused. The read returns a blob, and ~ can be used with strings and Buf. I get this. >my Blob $read = Buf.new Does this means that Blob will do Buf role as well? >my Buf $a = Buf.new(1,2,3); >my Blob $b = Blob.new(4,5,6); >$a ~= $b; Since this is allowed, does it means that Blob does Buf role by being coerced(?) as well? But then shouldn't this be allowed as well? >my Blob $read; >$read ~= $socket.read(1024) Best regards, David Santiago -- Sent from my Android device with K-9 Mail. Please excuse my brevity.
Re: Question about Blob and Buf
Ok I 100% don't know after trying this out : my Buf $a = Buf.new(1,2,3); my Blob $b = Blob.new(4,5,6); $a ~= $b; say $a And it worked fine so... I dunno. On Tue, 11 Feb 2020 at 11:00, Simon Proctor wrote: > I think the problem is IO::Socket.read() returns a Blob not a Buf. > > ~ has a Buf, Buf variant : > https://docs.raku.org/language/operators#infix_~ > > But not a Blob one. Buf does Blob but not vice versa. > > I think you need to transform the output from .read into a Buf if you want > to use the ~= how you want to. > > Would this work? > my Blob $read = Buf.new; > $read ~= Buf.new( $socket.read(1024) ); > > > On Tue, 11 Feb 2020 at 10:46, Kevin Pye wrote: > >> >> ~ works fine for concatenating Bufs; For example: >> >> my $a = Buf.new(1,2,3); >> my $b = $a ~ Buf.new(4,5,6) >> >> will assign correctly to $b. >> >> I can't work out what the problem is here, despite trying various >> combinations. Perhaps socket isn't really returning a Blob? >> >> Kevin. >> >> On Tue, 11 Feb 2020 at 21:01, JJ Merelo wrote: >> >>> You are using ~, which stringifies. Bufs are not strings: you need to >>> decode them to concatenate it to a string. If what you want is to >>> concatenate the buffer, probably ,= will work (not sure about this, would >>> have to check), or any other operator that works on Positionals. >>> >>> JJ >>> >>> El mar., 11 feb. 2020 a las 10:56, David Santiago () >>> escribió: >>> A 11 de fevereiro de 2020 10:47:34 CET, David Santiago < deman...@gmail.com> escreveu: >A 11 de fevereiro de 2020 09:46:06 CET, David Santiago < deman...@gmail.com> escreveu: >> >>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! > >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! 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 -- Sent from my Android device with K-9 Mail. Please excuse my brevity. >>> >>> >>> -- >>> JJ >>> >> > > -- > Simon Proctor > Cognoscite aliquid novum cotidie > > http://www.khanate.co.uk/ > -- Simon Proctor Cognoscite aliquid novum cotidie http://www.khanate.co.uk/
Re: Question about Blob and Buf
I think the problem is IO::Socket.read() returns a Blob not a Buf. ~ has a Buf, Buf variant : https://docs.raku.org/language/operators#infix_~ But not a Blob one. Buf does Blob but not vice versa. I think you need to transform the output from .read into a Buf if you want to use the ~= how you want to. Would this work? my Blob $read = Buf.new; $read ~= Buf.new( $socket.read(1024) ); On Tue, 11 Feb 2020 at 10:46, Kevin Pye wrote: > > ~ works fine for concatenating Bufs; For example: > > my $a = Buf.new(1,2,3); > my $b = $a ~ Buf.new(4,5,6) > > will assign correctly to $b. > > I can't work out what the problem is here, despite trying various > combinations. Perhaps socket isn't really returning a Blob? > > Kevin. > > On Tue, 11 Feb 2020 at 21:01, JJ Merelo wrote: > >> You are using ~, which stringifies. Bufs are not strings: you need to >> decode them to concatenate it to a string. If what you want is to >> concatenate the buffer, probably ,= will work (not sure about this, would >> have to check), or any other operator that works on Positionals. >> >> JJ >> >> El mar., 11 feb. 2020 a las 10:56, David Santiago () >> escribió: >> >>> A 11 de fevereiro de 2020 10:47:34 CET, David Santiago < >>> deman...@gmail.com> escreveu: >>> >A 11 de fevereiro de 2020 09:46:06 CET, David Santiago < >>> deman...@gmail.com> escreveu: >>> >> >>> >>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! >>> > >>> >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! >>> >>> 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 >>> >>> -- >>> Sent from my Android device with K-9 Mail. Please excuse my brevity. >>> >> >> >> -- >> JJ >> > -- Simon Proctor Cognoscite aliquid novum cotidie http://www.khanate.co.uk/
Re: Question about Blob and Buf
~ works fine for concatenating Bufs; For example: my $a = Buf.new(1,2,3); my $b = $a ~ Buf.new(4,5,6) will assign correctly to $b. I can't work out what the problem is here, despite trying various combinations. Perhaps socket isn't really returning a Blob? Kevin. On Tue, 11 Feb 2020 at 21:01, JJ Merelo wrote: > You are using ~, which stringifies. Bufs are not strings: you need to > decode them to concatenate it to a string. If what you want is to > concatenate the buffer, probably ,= will work (not sure about this, would > have to check), or any other operator that works on Positionals. > > JJ > > El mar., 11 feb. 2020 a las 10:56, David Santiago () > escribió: > >> A 11 de fevereiro de 2020 10:47:34 CET, David Santiago < >> deman...@gmail.com> escreveu: >> >A 11 de fevereiro de 2020 09:46:06 CET, David Santiago < >> deman...@gmail.com> escreveu: >> >> >> >>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! >> > >> >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! >> >> 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 >> >> -- >> Sent from my Android device with K-9 Mail. Please excuse my brevity. >> > > > -- > JJ >
Re: Question about Blob and Buf
You are using ~, which stringifies. Bufs are not strings: you need to decode them to concatenate it to a string. If what you want is to concatenate the buffer, probably ,= will work (not sure about this, would have to check), or any other operator that works on Positionals. JJ El mar., 11 feb. 2020 a las 10:56, David Santiago () escribió: > A 11 de fevereiro de 2020 10:47:34 CET, David Santiago > escreveu: > >A 11 de fevereiro de 2020 09:46:06 CET, David Santiago < > deman...@gmail.com> escreveu: > >> > >>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! > > > >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! > > 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 > > -- > Sent from my Android device with K-9 Mail. Please excuse my brevity. > -- JJ
Question about Blob and Buf
A 11 de fevereiro de 2020 10:47:34 CET, David Santiago escreveu: >A 11 de fevereiro de 2020 09:46:06 CET, David Santiago >escreveu: >> >>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! > >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! 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 -- Sent from my Android device with K-9 Mail. Please excuse my brevity.