Add type restrictions to Digest#15696
Conversation
src/digest/adler32.cr
Outdated
| end | ||
|
|
||
| def self.update(data, adler32 : UInt32) : UInt32 | ||
| def self.update(data : Slice(UInt8) | String, adler32 : UInt32) : UInt32 |
There was a problem hiding this comment.
Here is where some sort of trait mechanism would have shined - what really is accepted is slices and anything that can be converted to a slice by implementing to_slice. I don't think it is obvious that the latter should necessary be reduced to just strings.
Aside, not related to PR but to the code in general: Here I'd have preferred to have two method variants, one that is locked down to only slice and don't try to call to_slice on it, and one that is more liberal that try to convert and call the other. (If we ever get endless defs then this is a place where those would be great)
There was a problem hiding this comment.
I agree to both
For this PR, I'd suggest to leave out the type restriction. It's too strict. And we don't have a means to express the actual type requirement accurately (cf. #15682 (comment)).
There was a problem hiding this comment.
We should split the method: one with the implementation that takes a Bytes (Slice(UInt8)) and another that will cast #to_slice?
def self.update(data, adler32 : UInt32)
update(data.to_slice, adler32)
end
def self.update(data : Bytes, adler32 : UInt32)
# actual implementation
endThe advantage is making sure #to_slice does indeed return a Slice(UInt8) not a Slice(UInt16) or Slice(Char).
The same should be applied to other Digest and Crypto and other places in stdlib that expects bytes. Maybe not in this PR, or not, so we have a potentially breaking change in a focused PR.
Digest
This is the output of compiling cr-source-typer and running it with the below incantation:
This is related to #15682 .