# Provide an example using "eval-syntax" & "syntax" in typed/racket

**URL:** <https://racket.discourse.group/t/provide-an-example-using-eval-syntax-syntax-in-typed-racket/1662>\
**Category:** General\
**Created:** [January 25, 2023, 5:09pm UTC](https://racket.discourse.group/t/provide-an-example-using-eval-syntax-syntax-in-typed-racket/1662 "2023-01-25T17:09:07Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![devosalain](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/devosalain/32/546_2.png) [@devosalain](https://racket.discourse.group/u/devosalain)\
**Post date:** [January 25, 2023, 5:09pm UTC](https://racket.discourse.group/t/provide-an-example-using-eval-syntax-syntax-in-typed-racket/1662/1 "2023-01-25T17:09:07Z")

</div>

I have difficulty using typed/racket & the function "eval-syntax".  
Can someone provide a compiling example

---

<div class="post-metadata">

**Author:** ![samth](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/samth/32/3_2.png) [@samth](https://racket.discourse.group/u/samth)\
**Post date:** [January 25, 2023, 7:36pm UTC](https://racket.discourse.group/t/provide-an-example-using-eval-syntax-syntax-in-typed-racket/1662/2 "2023-01-25T19:36:33Z")

</div>

This seems like the simplest thing, and it works:

```scheme
#lang typed/racket

(eval-syntax #'(+ 1 2))

```

My guess is that you're trying something more complex but I don't know what.

---

<div class="post-metadata">

**Author:** ![devosalain](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/devosalain/32/546_2.png) [@devosalain](https://racket.discourse.group/u/devosalain)\
**Post date:** [January 25, 2023, 11:34pm UTC](https://racket.discourse.group/t/provide-an-example-using-eval-syntax-syntax-in-typed-racket/1662/3 "2023-01-25T23:34:02Z")

</div>

the following does not compile

```scheme
#lang typed/racket
(define a
  (syntax '(+ 1 2)
          )
  )
(: b : Number )
(define b
  (eval-syntax a))
(write b)

```

---

<div class="post-metadata">

**Author:** ![samth](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/samth/32/3_2.png) [@samth](https://racket.discourse.group/u/samth)\
**Post date:** [January 26, 2023, 12:11am UTC](https://racket.discourse.group/t/provide-an-example-using-eval-syntax-syntax-in-typed-racket/1662/4 "2023-01-26T00:11:24Z")

</div>

Right, the problem is that eval-syntax doesn't know the type of the argument. If you annotate the result with AnyValues, and if you handle the possibility of multiple values, then it will work. But type checking code that's a value in your program is not something Typed Racket can do.

---

<div class="post-metadata">

**Author:** ![devosalain](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/devosalain/32/546_2.png) [@devosalain](https://racket.discourse.group/u/devosalain)\
**Post date:** [January 26, 2023, 12:13am UTC](https://racket.discourse.group/t/provide-an-example-using-eval-syntax-syntax-in-typed-racket/1662/5 "2023-01-26T00:13:22Z")

</div>

So as i understand there is no solution. Can i say typed/racket in this specific case accept any.  
Or does this breaks the whole system.

---

<div class="post-metadata">

**Author:** ![devosalain](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/devosalain/32/546_2.png) [@devosalain](https://racket.discourse.group/u/devosalain)\
**Post date:** [January 26, 2023, 12:14am UTC](https://racket.discourse.group/t/provide-an-example-using-eval-syntax-syntax-in-typed-racket/1662/6 "2023-01-26T00:14:25Z")

</div>

Or can i force it because i know beforehand what it will be

---

<div class="post-metadata">

**Author:** ![samth](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/samth/32/3_2.png) [@samth](https://racket.discourse.group/u/samth)\
**Post date:** [January 26, 2023, 12:29am UTC](https://racket.discourse.group/t/provide-an-example-using-eval-syntax-syntax-in-typed-racket/1662/7 "2023-01-26T00:29:46Z")

</div>

Maybe you can be more specific about what you want to do. A good way to think about this is to consider what you would do with arbitrary user input.

---

<div class="post-metadata">

**Author:** ![sorawee](https://avatars.discourse-cdn.com/v4/letter/s/ea5d25/32.png) [@sorawee](https://racket.discourse.group/u/sorawee)\
**Post date:** [January 26, 2023, 12:35am UTC](https://racket.discourse.group/t/provide-an-example-using-eval-syntax-syntax-in-typed-racket/1662/8 "2023-01-26T00:35:47Z")

</div>

Something like this would work:

```scheme
#lang typed/racket

(define stx (syntax (+ 1 2)))

(define-syntax-rule (cast-eval-syntax-single-val x t)
  (match (call-with-values (λ () (eval-syntax x))
                           (inst list Any))
    [(list y) (cast y t)]
    [_ (error 'not-expected-multiple-values)]))

(: result Number)
(define result
  (cast-eval-syntax-single-val stx Number))

(write result)

```

Note that your program has an extra `'` inside `(syntax ...)`, which would eval to a list, causing the cast to Number fail.

EDITED: This is even better.

```scheme
#lang typed/racket

(define stx (syntax (+ 1 2)))

(define-syntax-rule (cast-eval-syntax-single-val stx t)
  (call-with-values
   (λ () (eval-syntax stx))
   (ann
    (case-lambda
      [(v) (cast v t)]
      [vs (error 'not-expected-multiple-values)])
    (-> Any * t))))

(: result Number)
(define result
  (cast-eval-syntax-single-val stx Number))

(write result)

```
