# Obj.as(obj.class) ^ Error: expecting token 'CONST', not 'block'

**URL:** <https://forum.crystal-lang.org/t/obj-as-obj-class-error-expecting-token-const-not-block/2077>\
**Category:** Help & Support\
**Created:** [May 8, 2020, 5:40am UTC](https://forum.crystal-lang.org/t/obj-as-obj-class-error-expecting-token-const-not-block/2077 "2020-05-08T05:40:02Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![krthr](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/krthr/32/755_2.png) [@krthr](https://forum.crystal-lang.org/u/krthr)\
**Post date:** [May 8, 2020, 5:40am UTC](https://forum.crystal-lang.org/t/obj-as-obj-class-error-expecting-token-const-not-block/2077/1 "2020-05-08T05:40:02Z")

</div>

Hi! I have a problem with the type handling… ☹

```auto
@blocks : Array(T) # Array(Gusano::Block(Int32, Nil) | Gusano::Block(String, Nil))

def start(params)
  block = @blocks.find { |el| el.name == @actual_block }

  if block.nil?
    # TODO
  else
    block.as(block.class).run params # <- Error
  end
end

```

I need to cast `block` to its real type. `@blocks` is an array and the objects inside are instances of a generic class so I get this error:

```auto
Error: no overload matches 'Proc(Tuple(String), Nil)#call' with type Tuple(Int32)

Overloads are:
 - Proc(T, R)#call(*args : *T)

```

Could anybody help me? Thank you! 😕

---

<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 8, 2020, 5:57am UTC](https://forum.crystal-lang.org/t/obj-as-obj-class-error-expecting-token-const-not-block/2077/2 "2020-05-08T05:57:29Z")

</div>

The code is a bit too much of fragment to really tell what’s going on and what you really want.

The error is a conceptual one. `block.class` is a runtime value, however `.as` operates (at least partly) in compile time. It needs to generate the right native code for the call with the right signature after all.

---

<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:** [May 8, 2020, 11:23am UTC](https://forum.crystal-lang.org/t/obj-as-obj-class-error-expecting-token-const-not-block/2077/3 "2020-05-08T11:23:26Z")

</div>

I’m sure this code produces a syntax error. `block.as(block.class)` is invalid. The argument to `as` must be a constant.  
There should be no way this could possible parse. Just parsing the snipped you provided without any context shows that syntax error.

---

<div class="post-metadata">

**Author:** ![krthr](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/krthr/32/755_2.png) [@krthr](https://forum.crystal-lang.org/u/krthr)\
**Post date:** [May 8, 2020, 2:46pm UTC](https://forum.crystal-lang.org/t/obj-as-obj-class-error-expecting-token-const-not-block/2077/4 "2020-05-08T14:46:40Z")

</div>

Hi! Thank you for your help… I have uploaded the simplified version of the code to the official online compiler:

[https://play.crystal-lang.org/#/r/9143](https://play.crystal-lang.org/#/r/9143)

```auto
class Block(*T, U)
  @exec_fn : Proc(T, Void)
  getter name
 
  def initialize(@name : Symbol, &fn : *T -> U)
    @exec_fn = ->(params : T) { fn.call *params }
  end
 
  def run(*params)
    @exec_fn.call params
  end
end
 
class Engine(T)
  @actual_block : Symbol
  @blocks : Array(T)
 
  def initialize(@blocks : Array(T), @actual_block : Symbol); end
 
  def start(params)
    block = @blocks.find { |el| el.name == @actual_block }
 
    if !block.nil?
      block.run params
    end
  end
end
 
block1 = Block(Int32, String).new :init { |a| a.to_s }
block2 = Block(String, Int32).new :end { |str| str.to_i }
 
engine = Engine.new(
  [
    block1,
    block2,
  ], 
  :init
)
 
engine.start 123

```

Output:

```auto
Showing last frame. Use --error-trace for full trace.

error in line 10
Error: no overload matches 'Proc(Tuple(String), Int32)#call' with type Tuple(Int32)

Overloads are:
 - Proc(T, R)#call(*args : *T)

```

---

<div class="post-metadata">

**Author:** ![krthr](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/krthr/32/755_2.png) [@krthr](https://forum.crystal-lang.org/u/krthr)\
**Post date:** [May 8, 2020, 2:49pm UTC](https://forum.crystal-lang.org/t/obj-as-obj-class-error-expecting-token-const-not-block/2077/5 "2020-05-08T14:49:27Z")

</div>

The `@blocks` array is: `Array(Gusano::Block(Int32, Nil) | Gusano::Block(String, Nil))`. So, the `block` type on the `#start` function is `Gusano::Block(Int32, Nil) | Gusano::Block(String, Nil)`.

I think that might be the problem.

---

<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, 2020, 3:30pm UTC](https://forum.crystal-lang.org/t/obj-as-obj-class-error-expecting-token-const-not-block/2077/6 "2020-05-08T15:30:14Z")

</div>

There’s no way to it that way.

---

<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 8, 2020, 3:42pm UTC](https://forum.crystal-lang.org/t/obj-as-obj-class-error-expecting-token-const-not-block/2077/7 "2020-05-08T15:42:48Z")

</div>

You sure can get interesting error messages after playing for a while

```auto
Error: no overload matches 'Block(Tuple(Int32))#run' with type Tuple(Int32)

Overloads are:
 - Block(T)#run(params : T)

```

[https://play.crystal-lang.org/#/r/914w](https://play.crystal-lang.org/#/r/914w)

---

<div class="post-metadata">

**Author:** ![krthr](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/krthr/32/755_2.png) [@krthr](https://forum.crystal-lang.org/u/krthr)\
**Post date:** [May 8, 2020, 3:44pm UTC](https://forum.crystal-lang.org/t/obj-as-obj-class-error-expecting-token-const-not-block/2077/8 "2020-05-08T15:44:13Z")

</div>

☹

---

<div class="post-metadata">

**Author:** ![krthr](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/krthr/32/755_2.png) [@krthr](https://forum.crystal-lang.org/u/krthr)\
**Post date:** [May 8, 2020, 4:00pm UTC](https://forum.crystal-lang.org/t/obj-as-obj-class-error-expecting-token-const-not-block/2077/9 "2020-05-08T16:00:49Z")

</div>

I find that error a bit confusing. I have seen the modifications you made and it.

And after playing for a while:

```auto
block.as(Block(typeof(params))).run params 
# Error: can't cast (Block(Int32) | Block(String)) to Block(Tuple(Int32))

```

---

<div class="post-metadata">

**Author:** ![krthr](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/krthr/32/755_2.png) [@krthr](https://forum.crystal-lang.org/u/krthr)\
**Post date:** [May 8, 2020, 4:02pm UTC](https://forum.crystal-lang.org/t/obj-as-obj-class-error-expecting-token-const-not-block/2077/10 "2020-05-08T16:02:16Z")

</div>

I think I’ll pause this. I’ve been at this for several hours and I still don’t fully understand the type system :( Thank you!

---

<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, 2020, 4:15pm UTC](https://forum.crystal-lang.org/t/obj-as-obj-class-error-expecting-token-const-not-block/2077/11 "2020-05-08T16:15:09Z")

</div>

What I mean is, the way you are coding it will never work, but the program you want to build is definitely possible.

Could you explain what are you trying to build?

---

<div class="post-metadata">

**Author:** ![krthr](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/krthr/32/755_2.png) [@krthr](https://forum.crystal-lang.org/u/krthr)\
**Post date:** [May 9, 2020, 12:40am UTC](https://forum.crystal-lang.org/t/obj-as-obj-class-error-expecting-token-const-not-block/2077/12 "2020-05-09T00:40:46Z")

</div>

My idea is to have blocks that execute a function (proc) and that can be connected to each other…

```auto
Block A, B

A(Input).run
B(A.output).run

```

I think it could be useful for visual programming, some rpc tool or even to create a Workflow Automation tool.

---

<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 11, 2020, 12:17pm UTC](https://forum.crystal-lang.org/t/obj-as-obj-class-error-expecting-token-const-not-block/2077/13 "2020-05-11T12:17:47Z")

</div>

This might be possible in the future if there’s a way to do `if x.is_a?(Proc(Int32, _))`, but right now you can’t use underscore in `is_a?`. Then you could do something like:

```crystal
  def start(params : *T) forall T
    block = @blocks.find { |el| el.name == @actual_block }
 
    if block.is_a?(Proc(*T, _))
      block.run params
    end
  end

```

I made underscore kind of work during the weekend by hacking something, but then something else breaks and then I ran out of time. I doubt this will be fixed before 1.0.

What you want to do is definitely possible if you model it in a different way. But exactly like you are doing it here will never work due to language limitations.
