# Undefined method 'types' for Crystal::Nop

**URL:** <https://forum.crystal-lang.org/t/undefined-method-types-for-crystal-nop/5312>\
**Category:** Help & Support\
**Created:** [February 1, 2023, 9:07pm UTC](https://forum.crystal-lang.org/t/undefined-method-types-for-crystal-nop/5312 "2023-02-01T21:07:54Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![meatball133](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/meatball133/32/1869_2.png) [@meatball133](https://forum.crystal-lang.org/u/meatball133)\
**Post date:** [February 1, 2023, 9:07pm UTC](https://forum.crystal-lang.org/t/undefined-method-types-for-crystal-nop/5312/1 "2023-02-01T21:07:54Z")

</div>

I try to use some methods for the ast module in crystal. Although I am running into some problems probably likely because the ast is a macro.

When I run the following code `p node.args[0].not_nil!.types`.

I get this error:  
`Error: undefined method 'types' for Crystal::Nop (compile-time type is Crystal::ASTNode+)`

Does any one know a way around this?

---

<div class="post-metadata">

**Author:** ![Blacksmoke16](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/blacksmoke16/32/1241_2.png) [@Blacksmoke16](https://forum.crystal-lang.org/u/Blacksmoke16)\
**Post date:** [February 1, 2023, 9:12pm UTC](https://forum.crystal-lang.org/t/undefined-method-types-for-crystal-nop/5312/2 "2023-02-01T21:12:42Z")

</div>

I think the error is pretty clear in this case. The type you’re calling `#types` on doesn’t have a method called `#types`. Basically the same error you’d get if you tried to do like `p 1.types`.

Can you share the actual code that reproduces this/what you’re wanting to do?

---

<div class="post-metadata">

**Author:** ![meatball133](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/meatball133/32/1869_2.png) [@meatball133](https://forum.crystal-lang.org/u/meatball133)\
**Post date:** [February 1, 2023, 9:18pm UTC](https://forum.crystal-lang.org/t/undefined-method-types-for-crystal-nop/5312/3 "2023-02-01T21:18:32Z")

</div>

I have this code

```auto
  private def handle_visit_describe_call(node : Crystal::Call)
    p node.args[0].not_nil!.types
    @breadcrumbs << node.args[0].not_nil!.as(Crystal::StringLiteral).value 
    accept(node.block.not_nil!)
  end

```

It is used to convert an ast like: ABC to stringLiteral.

But when the code gets a path like Class1::Class2 it can’t convert it to stringLiteral.  
I want a way to be able to do that conversion anyway.

---

<div class="post-metadata">

**Author:** ![Blacksmoke16](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/blacksmoke16/32/1241_2.png) [@Blacksmoke16](https://forum.crystal-lang.org/u/Blacksmoke16)\
**Post date:** [February 1, 2023, 9:21pm UTC](https://forum.crystal-lang.org/t/undefined-method-types-for-crystal-nop/5312/4 "2023-02-01T21:21:35Z")

</div>

Oh so you’re customizing the compiler? That’s a bit out of my area of expertise.

What are you making that you’re needing to do that? IMO feels like a bit much unless you’re working on some static analysis or LSP tool or something…

---

<div class="post-metadata">

**Author:** ![meatball133](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/meatball133/32/1869_2.png) [@meatball133](https://forum.crystal-lang.org/u/meatball133)\
**Post date:** [February 1, 2023, 9:26pm UTC](https://forum.crystal-lang.org/t/undefined-method-types-for-crystal-nop/5312/5 "2023-02-01T21:26:21Z")

</div>

I am contributor for exercism.

It is a learning platform for a bunch of languages including crystal.

I am at the moment trying to fix a bug with our test-runner, which you can find here: [exercism/crystal-test-runner (github.com)](https://github.com/exercism/crystal-test-runner).

And since I didn’t write the test-runner I am a bit unsure how the person before me thought out the design. And there is an exercise on the platform which has a test case which is `describe class1::class2` and the test-runner flips out when it sees it. My plan was to apply an “easy” fix but it may be more complicated than that.

---

<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:** [February 1, 2023, 9:51pm UTC](https://forum.crystal-lang.org/t/undefined-method-types-for-crystal-nop/5312/6 "2023-02-01T21:51:31Z")

</div>

You should probably use a `case` expression over the different AST node types.

Something like this:

```crystal
case arg = node.args[0]
when StringLiteral
  arg.value
when Path
  arg.to_s
end

```

`Path#to_s` should turn `Class1::Class2` into its string representation.  
I suppose you could call `to_s` on any node type to turn it into a string. Replacing `when Path` by `else` would do that and you’d only have special handling for `StringLiteral`.

---

<div class="post-metadata">

**Author:** ![meatball133](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/meatball133/32/1869_2.png) [@meatball133](https://forum.crystal-lang.org/u/meatball133)\
**Post date:** [February 1, 2023, 9:55pm UTC](https://forum.crystal-lang.org/t/undefined-method-types-for-crystal-nop/5312/7 "2023-02-01T21:55:29Z")

</div>

I get an error saying: Error: undefined constant StringLiteral

---

<div class="post-metadata">

**Author:** ![meatball133](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/meatball133/32/1869_2.png) [@meatball133](https://forum.crystal-lang.org/u/meatball133)\
**Post date:** [February 1, 2023, 9:58pm UTC](https://forum.crystal-lang.org/t/undefined-method-types-for-crystal-nop/5312/8 "2023-02-01T21:58:12Z")

</div>

updating it to Crystal::StringLiteral, fixes it. Does that have the same meaning?

---

<div class="post-metadata">

**Author:** ![meatball133](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/meatball133/32/1869_2.png) [@meatball133](https://forum.crystal-lang.org/u/meatball133)\
**Post date:** [February 1, 2023, 10:07pm UTC](https://forum.crystal-lang.org/t/undefined-method-types-for-crystal-nop/5312/9 "2023-02-01T22:07:38Z")

</div>

Okay I changed it to

Crystal::StringLiteral  
Crystal::Path

And it works.  
Thanks for all of your help!
