# Subtyping issue

**URL:** https://forum.crystal-lang.org/t/subtyping-issue/3283
**Category:** Help & Support
**Created:** [May 8, 2021, 8:48am UTC](https://forum.crystal-lang.org/t/subtyping-issue/3283 "2021-05-08T08:48:40Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![monomonedula](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/monomonedula/32/1271_2.png) [@monomonedula](https://forum.crystal-lang.org/u/monomonedula)
#### Post date: [May 8, 2021, 8:48am UTC](https://forum.crystal-lang.org/t/subtyping-issue/3283/1 "2021-05-08T08:48:41Z")

</div>

Isn’t `String` a subtype of `Object` this making this assignment valid?

```auto
In src/djot/signed_token.cr:19:7

 19 | @payload = payload
      ^-------
Error: instance variable '@payload' of Djot::SignedToken must be Hash(String, Object), not Hash(String, String)

```

---

<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: [May 8, 2021, 9:45am UTC](https://forum.crystal-lang.org/t/subtyping-issue/3283/2 "2021-05-08T09:45:59Z")

</div>

Hi! You can’t have a Hash of Object.

Can you show us your full code?

---

<div class="post-metadata">

### Author: ![erdnaxeli](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/erdnaxeli/32/1242_2.png) [@erdnaxeli](https://forum.crystal-lang.org/u/erdnaxeli)
#### Post date: [May 8, 2021, 10:03am UTC](https://forum.crystal-lang.org/t/subtyping-issue/3283/3 "2021-05-08T10:03:56Z")

</div>

You are probably doing something like this:

```auto
struct Test
  @payload : Hash(String, Object)

  def initialize
    @payload = {"test" => "test"}
  end
end

```

This don’t work because you are assigning a `Hash(String, String)` to a `Hash(String, Object)`. You need to specify the type like this: `@payload = {"test" => "test} of String => Object`. See [Inheritance - Crystal](https://crystal-lang.org/reference/syntax_and_semantics/inheritance.html#covariance-and-contravariance).

But then as @asterite said you got this error:

> Error: can’t use Object as generic type argument yet, use a more specific type

---

<div class="post-metadata">

### Author: ![RespiteSage](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/respitesage/32/793_2.png) [@RespiteSage](https://forum.crystal-lang.org/u/RespiteSage)
#### Post date: [May 9, 2021, 7:21pm UTC](https://forum.crystal-lang.org/t/subtyping-issue/3283/4 "2021-05-09T19:21:09Z")

</div>

An important point as well is that in Crystal currently if you have

```crystal
class Type
end

class Subtype < Type
end

```

You have an “is a” relationship such that `(Subtype.new).is_a? Type` (note: the `#is_a?` method works on objects of a type, not the type itself).

However, the generic system does not extend that within generic type variables. So you end up with this:

```crystal
class Generic(T)
end

(Generic(Subtype).new).is_a? Generic(Type) # => false

```

I think the most relevant reason for this behavior is that it would be harder to implement what you’re expecting (and would make the compiler slower), but this behavior does work once you understand and expect it.

* * *

Side note: we as a community need a document somewhere that we can point to explaining how this behavior works and why it works that way. There are discussions about generics and inheritance regularly on the Gitter, here, and in GitHub issues, and I’ve picked up the little I just wrote in those places, but someone who really understands the relevant issues (and can properly use terms with “variance” in them) needs to write (or start) a definitive, clear document somewhere so that people can contribute to and refine it. That way we can have a direct, clear response to questions like this one.

---

<div class="post-metadata">

### Author: ![jhass](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/jhass/32/1779_2.png) [@jhass](https://forum.crystal-lang.org/u/jhass)
#### Post date: [May 10, 2021, 10:41am UTC](https://forum.crystal-lang.org/t/subtyping-issue/3283/5 "2021-05-10T10:41:47Z")

</div>

You found [Covariance and contravariance · Issue #3803 · crystal-lang/crystal · GitHub](https://github.com/crystal-lang/crystal/issues/3803) More at [Covariance and contravariance (computer science) - Wikipedia](https://en.wikipedia.org/wiki/Covariance_and_contravariance_(computer_science)) :)
