[JAVA] What is a lexical scope / dynamic scope?

In the first place, there are two types of scopes.

  1. Lexical scope
  2. Dynamic scope

Each is a variable reference specification, and lexical scope is adopted in many major languages such as Ruby, Java, Javascript, and python.

Example of lexical scope

It's very simple. See the code below.


var =  'I'm top level'

def a_method
  return var 
end

def b_method
  var = 'I am the contents of the object'
  return var
end

a_method 
#-> 'I'm top level'

It is natural to write Ruby etc., but you cannot refer to var of b_method from a_method. This is the ``` lexical scope " ... !!! However, it seems that there are languages in the world where the scope of variables is determined when a function is evaluated. It seems that they are called dynamic scopes, but for example Lisp and Emacs seem to adopt those scopes.

Dynamic scope example

This is not Ruby. Dynamic Ruby that uses a Ruby-like dynamic scope. (There is no such thing, but it is a coined word.)


def a_method
  p var
end


def b_method
  var = 'I'm B'
  a_method
end

def c_method
  a_method
end

b_method
#-> 'I'm B'

c_method
#-> undefined variable 'var'  

Will be. That is, the scope is determined when you evaluate the *** function, not when you define the function. *** ***

Benefits of dynamic scope

It seems easy to implement the language w Why did Lisp, Emacs, etc. adopt it? It seems that it is written that way. Lol

Recommended Posts

What is a lexical scope / dynamic scope?
What is a distribution?
What is a terminal?
What is a hacker?
What is a pointer?
What is a decision tree?
What is a Context Switch?
What is a super user?
What is a system call
[Definition] What is a framework?
What is a callback function?
What is a python map?
[Python] What is a zip function?
[Python] What is a with statement?
What is a Convolutional Neural Network?
What is a dog? Django installation volume
What is a dog? Python installation volume
What is namespace
What is copy.copy ()
What is Django? .. ..
What is dotenv?
What is POSIX?
What is Linux
What is klass?
What is SALOME?
What is Linux?
What is python
What is hyperopt?
What is Linux
What is pyvenv
What is __call__
What is Linux
What is Python
What is a dog? Django--Create a custom user model
What is a dog? Challenge Django templates! Volume
It's a Mac. What is the Linux command Linux?
What is a dog? Django--Create a custom user model 2
Tell me what a conformal map is, Python!
What is Piotroski's F-Score?
What is a dog? POST Sending Volume Using Django--forms.py
What is Raspberry Pi?
What is Calmar Ratio?
What is a dog? Django App Creation Start Volume--startapp
[PyTorch Tutorial ①] What is PyTorch?
What is hyperparameter tuning?
What is JSON? .. [Note]
What is a dog? Django App Creation Start Volume--startproject
Basics of Python learning ~ What is a string literal? ~
What is Linux for?
What is ensemble learning?
What is TCP / IP?
What is a recommend engine? Summary of the types
What is God? Make a simple chatbot with python
What is Python's __init__.py?
What is an iterator?
What is UNIT-V Linux?
[Python] What is virtualenv
To myself as a Django beginner (2) --What is MTV?
What is machine learning?
What is the difference between a symbolic link and a hard link?
What is Minisum or Minimax?