Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3215158
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:08:42+00:00 2026-05-17T15:08:42+00:00

Imagine you are designing your own programming language. Very simple language for quite specific

  • 0

Imagine you are designing your own programming language. Very simple language for quite specific purpose. It has functions, loops and variables. And you want to make use of dynamic scoping for variables.

Consider the imaginary example:

var x = "foo"
var count = 0

loop (/* condition */) {

    var x = "bar"
    // A new stack frame is created for 'x',
    // so inside the loop (and any subsequent function calls) it is bound to "bar",
    // but outside the loop 'x' is still bound to "foo"

    print (x) // output is "bar"

    var count = count + 1
}

print (x) // output is "foo"
print (count) // desired output is something above zero, but it's not !!!

My question is – how to make ‘count’ variable value set inside the loop to be visible outside?
How would you do that so it to look more natural (or less confusing) to the users?

  • Would you introduce a special keyword to assign a value to an existing variable from the outer scope in additional to the keyword to define a new variable in the current scope? Let’s say set and var, or assing and def, etc. But what ‘outer scope’ would then mean? What if ‘count’ is not defined right before the loop but instead it is defined earlier somewhere in the invocation stack? Would “set count = …” assign a value to the variable from that parent of parent of parent frame?
  • Would you introduce a return value (or tuple) to the loop statement, so that one could write something like:

    var count = 0
    var count = loop (condition == true) returns [i] { var i = i + 1 }

Wouldn’t that look awkward?

  • Your solution?

As far as know Perl supports the dynamic scoping using local keyword. How would you implement such example in Perl using dynamic scoped variables only?

Thank you!

  • 1 1 Answer
  • 3 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T15:08:43+00:00Added an answer on May 17, 2026 at 3:08 pm

    Sounds like you want to have your cake and eat it too, here. Some variables declared in inner scope should “hide” identical vars in outer scope, while others should not.

    Sounds pretty easy to me; if a variable is declared in inner scope with identical signature (name and type) as one in an outer scope, the compiler should allow this “syntactic sugar” and simply create a new variable that is actually referred to by some mashup in whatever intermediate code is used (MSIL, JIL, assembly). Then, remove the var declaration from count, and most standards about intra-method scope will make this work exactly the way you want. You may optionally require use of the “new” keyword on the inner variable declaration to confirm to the compiler that, yes, you really do want a variable with the same name that “hides” the outer scope definition. So, your snippet would only change as follows:

    var x = "foo";
    var count = 0;
    
    loop (/* condition */) {
    
        var x = "bar"; //"hides" the outer x; maybe require "new" to confirm
    
        print (x); // "bar"
    
        count = count + 1; //works with the outer definition of count
    }
    
    print (x); // "foo"
    print (count); // > 0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that each programming language has certain guideline and styles. My question is
Imagine you have class A which has code which runs as method M. And
Imagine a very big gwt project of application. This is just a some form
I'm designing a city building game and got into a problem. Imagine Sierra's Caesar
I'm designing a Core Data app which needs to keep dated records. Imagine a
I am designing a WPF user control which contains other user controls (imagine a
Imagine this case: A color has an id. Easy enough. The same color can,
I am designing a little game for my own fun's and training's sake. The
I'm designing my database and LINQ To SQL ASP.NET web application. Imagine I have
We are designing a system that has functionality that is essentially the same at

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.