Skip to content

Add type restrictions to Digest#15696

Merged
straight-shoota merged 3 commits intocrystal-lang:masterfrom
Vici37:add-type-restrictions-to-digest
May 11, 2025
Merged

Add type restrictions to Digest#15696
straight-shoota merged 3 commits intocrystal-lang:masterfrom
Vici37:add-type-restrictions-to-digest

Conversation

@Vici37
Copy link
Contributor

@Vici37 Vici37 commented Apr 21, 2025

This is the output of compiling cr-source-typer and running it with the below incantation:

CRYSTAL_PATH="./src" ./typer spec/std_spec.cr \
  --error-trace --exclude src/crystal/ \
  --stats --progress \
  --union-size-threshold 2 \
  --ignore-private-defs \
  --ignore-protected-defs \
  src/csv

This is related to #15682 .

end

def self.update(data, adler32 : UInt32) : UInt32
def self.update(data : Slice(UInt8) | String, adler32 : UInt32) : UInt32
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
end

The 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.

@straight-shoota straight-shoota added this to the 1.17.0 milestone May 6, 2025
@straight-shoota straight-shoota merged commit f6062f2 into crystal-lang:master May 11, 2025
36 checks passed
@straight-shoota straight-shoota changed the title Add type restrictions to Digest directory Add type restrictions to Digest May 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants