This would create a Buf/Blob of the Int of the given value, for the correct size, big endian.
OK, but which problem is this going to resolve?
Edit: Maybe it should be stressed in the README that the ticket should clearly explain what the problem is, before proposing a solution. We had a similar issue before: https://colabti.org/irclogger/irclogger_log/perl6?date=2019-04-05#l523
What @AlexDaniel said: we should start with the problem. I speculate the problem is:
my Buf $x .= new; $x.write-int32(42); to get a Buf containing the encoding of that integer valueBlob rather than a Buf if wanting to have it as an immutable valueThose are probably fair problems, thought the solution proposed here doesn't seem ideal to me, since:
Str are also Cool, and having that be numified in response to .Buf or .Blob feels a bit odd. Folks would quite possibly expect it to do something like .encode does.I'd be more in favor of factory methods on Blob/Buf, so you could do Blob.from-int32(-1) and it'd produce a 4-byte Blob holding the representation of -1; endianness can optionally be provided as it is in write-int32.
That'd work for me too :-)
While you're at it, how about taking an array (Iterable) as input as well?
@CurtTilmes Perhaps, but we have to be a bit careful, in that arrays in numeric contexts typically numify. It'd not be unreasonable for somebody to write my $chunk-count = Blob.from-int32(@chunks) and expect to have a Blob with the number of elements in the array encoded. It's a natural generalization for anyone happily doing if @stuff == 1 { } and similar.
Maybe a plural version of the methods: Blob.from-int32s(@values)?
Sounds good. I like this because it makes a few things easier.
Native types, shaped arrays, Blob-ifying numbers, etc. are on their way to competing with NumPy and PDL, but I still don't understand how it all fits together.
I like Blobs because they are easy to work with for NativeCall, but it seems that instead of converting from Int to Blob, I should be using an int32 that really already has the right representation under the sheets, right?
I shouldn't need to convert an array of int32 into a Blob, I should just be able to use an array of int32 directly? Like if a library wants a huge array of int32 as a pointer and a size, what's the efficient way to send it there? I use Blobs and CArrays a lot, but it seems like I am doing extra work.
Most helpful comment
What @AlexDaniel said: we should start with the problem. I speculate the problem is:
my Buf $x .= new; $x.write-int32(42);to get aBufcontaining the encoding of that integer valueBlobrather than aBufif wanting to have it as an immutable valueThose are probably fair problems, thought the solution proposed here doesn't seem ideal to me, since:
Strare alsoCool, and having that be numified in response to.Bufor.Blobfeels a bit odd. Folks would quite possibly expect it to do something like.encodedoes.I'd be more in favor of factory methods on
Blob/Buf, so you could doBlob.from-int32(-1)and it'd produce a 4-byteBlobholding the representation of-1; endianness can optionally be provided as it is inwrite-int32.