schemeracketplai

How to get the arguments of an instance of a Scheme define-type?


I have a stripped-down define-type of a function definition:

(define-type FunDef
    (fundef (fn-name symbol?)))

Here is a function definition which satisfies the define-type:

(fundef 'f)

How do I get the name of the function? For the example I gave, how to get 'f?

I tried these and they don't work:

(second (fundef 'f))
(second (list (fundef 'f)))
(second '(fundef 'f))

What is the correct way to get the name of the function?

Update

I tried the suggestion to "deconstruct it with type-case". Here's what I tried:

(define (fundef-fun-name fundef)
    (type-case FunDef fundef
        [fundef (fn-name) 
            fn-name]))

That generated this error message:

string:3:9: type-case: this is not a variant of the specified type

If I understand that error message, it is saying that fundef (fn-name) is not correct. It appears to me to be correct. What am I missing?


Solution

  • You have a naming issue – the procedure parameter can't have the same name as the fundef variant.
    Also, reusing the name "fn-name" in the matched pattern is a bit confusing.

    Here is a working example:

    (define (fundef-fun-name fun)
        (type-case FunDef fun
            [fundef (name) 
                name]))