Skip to content

Latest commit

 

History

History
80 lines (56 loc) · 2.24 KB

DynamicVariables.md

File metadata and controls

80 lines (56 loc) · 2.24 KB

Dynamic variables

Pharo contains a system of dynamic variables which are global variables whose value can be changed during the execution of a process.

Be careful when you use these variables. They should not be used just as a global variable to make access to it easier in the code.

A logger system is good example of the usefulness of dynamic variables. In such a system, we can imagine that we have a default logger that will be used to record logs. However, in some specific cases we want to use a custom logger. In that case a dynamic variable can do the work. If you wish to see it at work you can check this logger project using it.

Create a new dynamic variable

To create a new dynamic variable you just need to create a new subclass of DynamicVariable:

DynamicVariable subclass: #MyVariable
	slots: {  }
	classVariables: {  }
	package: 'MyProject'

Add a default value

It is possible to assign a default value to your dynamic variable. To do so, you need to add a default method.

MyVariable>>default
	^ MyDefaultObject

Use your dynamic variable

To use your variable, just send the value message to it. If you have no default value, do not forget to manage the case where it can be nil if it can happen.

MyVariable value doSomething

Change the value of the variable in a process

If you need to change the value of the variable for the execution of a specific process, just use the value:during: message.

MyVariable value: MyNewObject during: [
	self doAnActionUsingTheVariable.
]

Example

Here is the way dynamic variables are used in the TinyLogger project cited above.

DynamicVariable subclass: #TinyCurrentLogger
	slots: {  }
	classVariables: {  }
	package: 'TinyLogger-Core'
TinyCurrentLogger>>default
	^ TinyLogger default
Object>>record: aString
	TinyCurrentLogger value record: aString
Object>>execute: aBlock recordedAs: aString
	TinyCurrentLogger value execute: aBlock recordedAs: aString
	TinyCurrentLogger value: (TinyLogger new addTranscriptLogger; yourself) during: [ 
		'test' record.
		TinyCurrentLogger value record: 'Test2'
	]