How do I test whether a variable is defined before referencing it?

前端 未结 3 1173
庸人自扰
庸人自扰 2021-02-09 15:04

I would like to be able to test whether a variable is defined, prior to accessing it.

I like to have a global that specifies a \"debug level\". If debug level is 0, no

3条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-09 15:16

    This is totally up to the implementation to provide, and it looks like most implementations don't satisfactorily provide it.

    In SISC scheme, it looks like you can use GETPROP to this effect, but environments don't update automatically oh, look, there's this thing called INTERACTION-ENVIRONMENT that you can use:

    #;> (getprop 'cons (interaction-environment))
    #
    #;> (getprop 'x (interaction-environment))
    #f
    #;> (define x 100)
    #;> (getprop 'x (interaction-environment))
    100
    

    But it only works on the top level.

    #;> (define (foo y)
      (let ((e (interaction-environment)))
        (display "Is X bound? ") (display (getprop 'x e))
        (newline)
        (display "Is Y bound? ") (display (getprop 'y e))
        (newline) ))
    #;> (foo 1)
    #;> Is X bound? 100
    Is Y bound? #f
    

    For Chez you have TOP-LEVEL-BOUND? and INTERACTION-ENVIRONMENT again.

提交回复
热议问题