# Error when using @cfunction

**URL:** https://discourse.julialang.org/t/error-when-using-cfunction/103067
**Category:** Internals & Design
**Created:** [August 22, 2023, 1:56pm UTC](https://discourse.julialang.org/t/error-when-using-cfunction/103067 "2023-08-22T13:56:49Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![sletz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sletz/32/18675_2.png) [@sletz](https://discourse.julialang.org/u/sletz)
#### Post date: [August 22, 2023, 1:56pm UTC](https://discourse.julialang.org/t/error-when-using-cfunction/103067/1 "2023-08-22T13:56:49Z")

</div>

The following code `addVerticalSlider = @cfunction( $_addVerticalSlider, Cvoid, (Ptr{Cvoid}, Cstring, Ptr{T}, T, T, T, T))` gives `ERROR: TypeError: in cfunction method definition, expected Type, got a value of type TypeVar` on Julia 1.9. It was working with older version (like 1.6).

I can use real types instead of `T` , but `Ptr{T}` works… Why that? How can it be solved ?

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [August 22, 2023, 2:26pm UTC](https://discourse.julialang.org/t/error-when-using-cfunction/103067/2 "2023-08-22T14:26:13Z")

</div>

What is `T` in your example? Can you share a MWE that people on this forum can reproduce locally, to diagnose the issue?

---

<div class="post-metadata">

### Author: ![sletz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sletz/32/18675_2.png) [@sletz](https://discourse.julialang.org/u/sletz)
#### Post date: [August 22, 2023, 2:40pm UTC](https://discourse.julialang.org/t/error-when-using-cfunction/103067/3 "2023-08-22T14:40:05Z")

</div>

In the `initGlue!` function here: [https://github.com/sletz/Faust.jl/blob/main/src/uiglue.jl](https://github.com/sletz/Faust.jl/blob/main/src/uiglue.jl), so this [Faust.jl](https://github.com/sletz/Faust.jl) package. Is this enough ?

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [August 22, 2023, 3:04pm UTC](https://discourse.julialang.org/t/error-when-using-cfunction/103067/4 "2023-08-22T15:04:53Z")

</div>

Ah, I see - I don’t think you can use `@cfunction` like that in local scope. The function in question must already exist, and the argument types need to be known. As the docstring mentions, all of this happens during compilation, not runtime:

> Note that the argument type tuple must be a literal tuple, and not a tuple-valued variable or expression (although it can include a splat expression). And that these arguments will be evaluated in global scope during compile-time (not deferred until runtime).

---

<div class="post-metadata">

### Author: ![sletz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sletz/32/18675_2.png) [@sletz](https://discourse.julialang.org/u/sletz)
#### Post date: [August 22, 2023, 3:12pm UTC](https://discourse.julialang.org/t/error-when-using-cfunction/103067/5 "2023-08-22T15:12:57Z")

</div>

But then:

- why does the `Ptr{T}` work in the first place (since it is also using a TypeVar `T`)
- Is was working with 1.6, why has this been changed ?
