On 2/24/20, Marc Kaufmann <marc.kaufman...@gmail.com> wrote:
> Very nice, adding
>
> (: get-user-var (case-> ...))
>
> indeed does the trick. So I can use `case->` to do overloading (if that's
> what it is called), by defining different functions for different type
> signatures, and then put them together into a single function via a cond.
> E.g.:
>
> (: int-add (-> Integer Integer Integer))
> (define (int-add x y)
>   (+ x y))
>
> (: string-add (-> String String String))
> (define (string-add x y)
>   (string-append x y))
>
> (: int-string-add (case->
>                     (-> Integer Integer Integer)
>                     (-> String String String)))
> (define (int-string-add x y)
>   (cond [(integer? x) (int-add x y)]
>         [(string? x) (string-add x y)]
>         [else
>           (error "Not a string or int" x)]))
>
> I am wondering if there is a way to avoid the final definition of
> `int-string-add`, by simply defining the same function twice (or more) with
>
> different type signatures, and then when I call the function, which code
> gets called depends on the type signature? I would be surprised if this
> existed in Typed Racket right now, but it would be neat and good to know if
>
> there is a more idiomatic/built-in way than what I do above, ie just write:
>
> (: special-add (-> Integer Integer Integer))
> (define-special (special-add x y)
>   (+ x y))
>
> (: special-add (-> String String String))
> (define-special (special-add x y)
>   (string-append x y))
>
> I guess it would be pretty hard, since the type signature has to be coupled
> more tightly to a specific function than TR requires right now, i.e. I'd
> have to write (define-special (special-add [x : String] [y : String]) to
> make the link explicit. Anyway, if it's not possible right now, totally
> fine.

Right, I don't think that's possible now.

The extensible-functions package helps with overloadings, but (iiuc)
you need to pick a type at the top:

https://pkgs.racket-lang.org/package/extensible-functions

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/CAFUu9R6niER6eOakB2bOaDmQowRZJ5GK4JAe4ZfizQZ4V9zrCw%40mail.gmail.com.

Reply via email to