# Parameterize not working as expected in 8.13

**URL:** <https://racket.discourse.group/t/parameterize-not-working-as-expected-in-8-13/3071>\
**Category:** Questions & Answers\
**Created:** [August 2, 2024, 9:10pm UTC](https://racket.discourse.group/t/parameterize-not-working-as-expected-in-8-13/3071 "2024-08-02T21:10:39Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Greg\_Davidson](https://avatars.discourse-cdn.com/v4/letter/g/5f8ce5/32.png) [@Greg\_Davidson](https://racket.discourse.group/u/Greg_Davidson)\
**Post date:** [August 2, 2024, 9:10pm UTC](https://racket.discourse.group/t/parameterize-not-working-as-expected-in-8-13/3071/1 "2024-08-02T21:10:39Z")

</div>

I expected (_host_) to change to "hello" and then to "hi".  
I'm not understanding how it's becoming #t at all!

```scheme
#lang racket/base

(define *host* (make-parameter "ngender.net" string? 'host))

(eprintf "[0] host ~a\n" (*host*))

(parameterize ( [*host* "hello"] )
  (eprintf "[1] host ~a\n" (*host*))
  (*host* "hi")
  (eprintf "[2] host ~a\n" (*host*)) )

;; prints:
;; [0] host ngender.net
;; [1] host #t
;; [2] host #t

;; racket version: 8.13 [cs]

```

---

<div class="post-metadata">

**Author:** ![LiberalArtist](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/liberalartist/32/151_2.png) [@LiberalArtist](https://racket.discourse.group/u/LiberalArtist)\
**Post date:** [August 2, 2024, 9:33pm UTC](https://racket.discourse.group/t/parameterize-not-working-as-expected-in-8-13/3071/2 "2024-08-02T21:33:56Z")

</div>

For a parameter created with a guard function, the _result_ of the guard function is used instead of the value passed in. (The guard function is not applied to the initial value.) So when you write `(parameterize ([*host* "hello"]) ...)` or `(*host* "hi")`, the parameter is set to `#t` because that is the result of `(string? "hello")` and `(string? "hi")`.

You could either change the guard function to something like:

```scheme
(λ (v)
  (if (string? v)
      v
      (raise-argument-error '*host* "string?" v)))

```

or use a contract, e.g.:

```scheme
(define/contract *host*
  (parameter/c string?)
  (make-parameter "ngender.net" #f 'host))

```

---

<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:** [August 2, 2024, 9:48pm UTC](https://racket.discourse.group/t/parameterize-not-working-as-expected-in-8-13/3071/3 "2024-08-02T21:48:38Z")

</div>

I made this mistake before. The name "guard" is kinda misleading.

I think the documentation should be updated to include some examples on how how to use the guard argument correctly.

---

<div class="post-metadata">

**Author:** ![Greg\_Davidson](https://avatars.discourse-cdn.com/v4/letter/g/5f8ce5/32.png) [@Greg\_Davidson](https://racket.discourse.group/u/Greg_Davidson)\
**Post date:** [August 3, 2024, 8:35pm UTC](https://racket.discourse.group/t/parameterize-not-working-as-expected-in-8-13/3071/4 "2024-08-03T20:35:31Z")

</div>

Thank you! Further confusing the issue, the "guard" isn't used to check the original value! (Just the opposite as with structs!).  
I'm now using my own constructor for parameters:

```scheme
(define (my-parameter value guard name [check-name #f])
  (define (string x) (format "~a" x))
  (define (check v)
    (unless (guard v)
      (raise-argument-error (string name)
                            (string (or check-name (object-name guard) 'check))
                            v ) )
    v )
  (make-parameter (check value) check name) )

```

---

<div class="post-metadata">

**Author:** ![soegaard](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/soegaard/32/19_2.png) [@soegaard](https://racket.discourse.group/u/soegaard)\
**Post date:** [August 3, 2024, 8:51pm UTC](https://racket.discourse.group/t/parameterize-not-working-as-expected-in-8-13/3071/5 "2024-08-03T20:51:06Z")

</div>

> I made this mistake before. The name "guard" is kinda misleading.

Ditto.

Maybe we could use the term `transforming-guard` in the documentation?

---

<div class="post-metadata">

**Author:** ![usao](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/usao/32/1375_2.png) [@usao](https://racket.discourse.group/u/usao)\
**Post date:** [August 4, 2024, 7:42am UTC](https://racket.discourse.group/t/parameterize-not-working-as-expected-in-8-13/3071/6 "2024-08-04T07:42:01Z")

</div>

It works just as with structs, actually: the guard for a struct should return all values (as multiple values) for the fields, and a parameter is conceptually also a struct with one field. If you don’t need to do anything with the values, just return them as is. The reason guards work this way is so that guards can do more than checking, such as normalizing the input values (as with “Boolean parameters”).

---

<div class="post-metadata">

**Author:** ![cadence](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/cadence/32/998_2.png) [@cadence](https://racket.discourse.group/u/cadence)\
**Post date:** [August 14, 2024, 9:59pm UTC](https://racket.discourse.group/t/parameterize-not-working-as-expected-in-8-13/3071/7 "2024-08-14T21:59:46Z")

</div>

> [@Greg\_Davidson](#):
>
> ` (define (string x) (format "~a" x))`

Pro tip: `(require racket/format)` provides `~a` as a procedure for [turning things into strings](https://docs.racket-lang.org/reference/strings.html#%28mod-path._racket%2Fformat%29).
