{"id":498,"date":"2003-06-03T11:45:24","date_gmt":"2003-06-03T15:45:24","guid":{"rendered":"\/?p=498"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-30T04:00:00","slug":"dynamic_scoping","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2003\/06\/03\/dynamic_scoping\/","title":{"rendered":"Dynamic Scoping"},"content":{"rendered":"<p>\n\nMost sane individuals know that dynamic scoping is evil. However, it can be useful, and <a href=\"http:\/\/www.pragprog.com\/pragdave\/Tech\/Random\/StackContext.rdoc,v\">Dave Thomas<\/a> shows that when made explicit it can actually be pretty nice. His code demonstrates some powerful Ruby features: using blocks to add syntax to the language and (ab)using <a href=\"http:\/\/www.rubygarden.org\/ruby?Continuations\">callcc<\/a>. Neither of these is possible in Python.\n\n<\/p>\n\n<p>\n\nThe way his first implementation works is that <code>find_in_context<\/code>  throws an exception that&rsquo;s caught in <code>with_context<\/code>. The exception stores a function that knows (a) the name being looked up, and (b) <code>find_in_context<\/code>&rsquo;s <a href=\"http:\/\/www.sidhe.org\/~dan\/blog\/archives\/000156.html\">continuation<\/a>. <code>with_context<\/code> passes the function its context. If the context contains a binding for the name, the function calls the continuation with that value. This makes <code>find_in_context<\/code> return the value to its caller. The use of continuations is totally gratuitous, as Thomas later shows.\n\n<\/p>\n\n\n\n<p>\n\nFood for thought: since exception handlers are themselves dynamically scoped, this implementation is, in a sense, circular.\n\n<\/p>","protected":false},"excerpt":{"rendered":"<p>Most sane individuals know that dynamic scoping is evil. However, it can be useful, and Dave Thomas shows that when made explicit it can actually be pretty nice. His code demonstrates some powerful Ruby features: using blocks to add syntax to the language and (ab)using callcc. Neither of these is possible in Python. The way [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"","apple_news_api_id":"","apple_news_api_modified_at":"","apple_news_api_revision":"","apple_news_api_share_url":"","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-498","post","type-post","status-publish","format-standard","hentry","category-programming-category"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/498","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=498"}],"version-history":[{"count":0,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/498\/revisions"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=498"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=498"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=498"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}