# Undefined constant Digest::CRC32

**URL:** <https://forum.crystal-lang.org/t/undefined-constant-digest-crc32/3007>\
**Category:** Help & Support\
**Created:** [March 5, 2021, 12:17am UTC](https://forum.crystal-lang.org/t/undefined-constant-digest-crc32/3007 "2021-03-05T00:17:18Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jwoertink](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/jwoertink/32/50_2.png) [@jwoertink](https://forum.crystal-lang.org/u/jwoertink)\
**Post date:** [March 5, 2021, 12:17am UTC](https://forum.crystal-lang.org/t/undefined-constant-digest-crc32/3007/1 "2021-03-05T00:17:18Z")

</div>

I have this [stumpy\_png](https://github.com/stumpycr/stumpy_png) shard in my project that I’m trying to update for Crystal 0.36.1, but I keep running in to [this issue](https://github.com/stumpycr/stumpy_png/issues/36) which I originally reported to the shard.

```nohighlight
In src/stumpy_png/crc_io.cr:18:12

 18 | @crc = Digest::CRC32.update(slice, @crc)
             ^------------
Error: undefined constant Digest::CRC32

```

I tried to break out the issue outside of my project to narrow down what was causing it, and I noticed something weird. Take this example:

```crystal
require "digest" # <-- if I remove this line, it'll compile
require "digest/crc32"

class CrcIO < IO # <-- or don't inherit from IO
  getter crc : UInt32
  property size
  
  def initialize
    @crc = 0_u32
    @size = 0
  end
  
  def read(slice : Bytes)
    0
  end

  def write(slice : Bytes) : Nil
    @crc = Digest::CRC32.update(slice, @crc)
    @size += slice.size
  end
  
  def reset
    @crc = 0_u32
  end
end

```

It seems to be some combination of requiring digest, and inheriting from IO that causes the `Digest::CRC32` to randomly disappear…

Now, in the shard, it never calls `require "digest"` which means that the shard on its own compiles fine and specs run and pass. My guess is my Lucky app must have a require somewhere that’s causing this issue, but I haven’t found where or what yet. The other strange thing is, if I replace that `require "digest"` with a `require "digest/digest"`, it’ll also work. Unfortunately, the patch doesn’t seem to work in my app directly. As in, I can’t just throw a random `require "digest/digest"` at the top of my app and have it work.

Anyone have any ideas?

---

<div class="post-metadata">

**Author:** ![asterite](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/asterite/32/60_2.png) [@asterite](https://forum.crystal-lang.org/u/asterite)\
**Post date:** [March 5, 2021, 12:33am UTC](https://forum.crystal-lang.org/t/undefined-constant-digest-crc32/3007/2 "2021-03-05T00:33:56Z")

</div>

This is probably the issue: [crystal/io\_digest.cr at master · crystal-lang/crystal · GitHub](https://github.com/crystal-lang/crystal/blob/master/src/digest/io_digest.cr)

I really think we should rename that to DigestIO. Then also MemoryIO and so on. I think it was a very bad decision to try to namespace everything…

---

<div class="post-metadata">

**Author:** ![bcardiff](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/bcardiff/32/3_2.png) [@bcardiff](https://forum.crystal-lang.org/u/bcardiff)\
**Post date:** [March 5, 2021, 12:42am UTC](https://forum.crystal-lang.org/t/undefined-constant-digest-crc32/3007/3 "2021-03-05T00:42:42Z")

</div>

Yup is probably that.

Workaround:

```auto
  def write(slice : Bytes) : Nil
--- @crc = Digest::CRC32.update(slice, @crc)
+++ @crc = ::Digest::CRC32.update(slice, @crc)
    @size += slice.size
  end

```

---

<div class="post-metadata">

**Author:** ![straight-shoota](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/straight-shoota/32/36_2.png) [@straight-shoota](https://forum.crystal-lang.org/u/straight-shoota)\
**Post date:** [March 5, 2021, 11:13am UTC](https://forum.crystal-lang.org/t/undefined-constant-digest-crc32/3007/4 "2021-03-05T11:13:55Z")

</div>

I find it actually a bit surprising is the namespace lookup from an inherited scope which takes precedence over literal scope. Basically this:

```auto
class Bar
  BAR = "::Bar"
end

class Baz
  class Bar
    BAR = "Baz::Bar"
  end
end

class Foo < Baz
  Bar::BAR # => "Baz::Bar"
end

```

It works like this in Ruby. And I suppose you would expect that mostly for constants for easy access to constant values defined in an ancestor.  
But in the context of namespaces, it feels somewhat unexpected to me. `Foo` isn’t in the `Baz` namespace, it’s in top level namespace and thus I’d expect `Bar` to mean `::Bar` not `Baz::Bar`.

There’s probably no way to change this without losing consistency. This is an effect of types also being namespaces, so I guess we’ll have to accept it.

---

<div class="post-metadata">

**Author:** ![asterite](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/asterite/32/60_2.png) [@asterite](https://forum.crystal-lang.org/u/asterite)\
**Post date:** [March 5, 2021, 11:59am UTC](https://forum.crystal-lang.org/t/undefined-constant-digest-crc32/3007/5 "2021-03-05T11:59:31Z")

</div>

Yeah, the lookup works exactly the same as in Ruby. Maybe it’s a bad thing :-)

The problem with it is that it allows type-hijacking. For example, I define a library:

```crystal
class Foo
  def self.foo
    Bar.bar
  end
end

class Bar
  def self.bar
    puts "Hello!"
  end
end

Foo.foo # "Hello"

```

Then someone comes and does this:

```crystal
class Foo
  class Bar
    def self.bar
      puts "Bye!"
    end
  end
end

```

Then the `Bar` I was referring to in `Foo` is no longer the same type, and I get “Bye!” as an output.

This is not that bad if you don’t use the same names inside namespaces that are also part of an outer namespace.

Again, I was against naming `IO::Memory` like that (`MemoryIO` is better: its name is more readable and clear, there’s less typing, and there’s less chance of a name clash). Same with `StringBuilder`, `DigestIO`, etc. It really makes no sense that it’s called `IO::Digest`… what is it, a digest? No! It’s a “digest IO”, so it should be called `DigestIO`. What’s `IO::Memory`? Is it a memory? No! Well, I’ll stop my rant now…

By the way, the way C# names things is exactly the same as what I’m saying here. They have `MemoryStream`, they don’t have `Stream.Memory`.

---

<div class="post-metadata">

**Author:** ![jwoertink](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/jwoertink/32/50_2.png) [@jwoertink](https://forum.crystal-lang.org/u/jwoertink)\
**Post date:** [March 5, 2021, 3:58pm UTC](https://forum.crystal-lang.org/t/undefined-constant-digest-crc32/3007/6 "2021-03-05T15:58:18Z")

</div>

Thanks for all the info! That at least lets me move past my issue so I can continue the upgrades.

---

<div class="post-metadata">

**Author:** ![straight-shoota](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/straight-shoota/32/36_2.png) [@straight-shoota](https://forum.crystal-lang.org/u/straight-shoota)\
**Post date:** [March 5, 2021, 9:36pm UTC](https://forum.crystal-lang.org/t/undefined-constant-digest-crc32/3007/7 "2021-03-05T21:36:44Z")

</div>

> [@asterite](#):
>
> The problem with it is that it allows type-hijacking.

I suppose in a more positive way this could also be considered a feature for injecting mock implementations for tests. 😆

`MemoryIO` is certainly better as a name than `IO::Memory`. And there are similar cases like `*::Builder`, `*::Reader`, `*::Writer`, `*::Parser`.

But the problem is, if we rename all of them, there’s gonna be a whole lot more types in the top level namespace. We’d have `DelimitedIO`, `StapledIO`, `FileDescriptorIO`, `MemoryIO`, `HeydumpIO`, `SizedIO`, `MultiWriterIO` just from the `IO` namespace. Polluting the namespace may or may not be a huge issue. For the API docs it would definitely mean to lose context.  
Before even considering to change anything about this, we would need to find a really better alternative. Otherwise it’s not worth any migration cost.
