Difference between revisions of "Import"

From MiniScript Wiki
Jump to navigation Jump to search
(Created page with "<c>import</c> is a Mini Micro function that loads a MiniScript file from the current directory, '''/sys/lib''', '''/usr/lib''', or some path defined in env.importPaths...")
 
Line 11: Line 11:
  
 
Some modules, like [[listUtil]], [[stringUtil]], and [[mapUtil]], extend these
 
Some modules, like [[listUtil]], [[stringUtil]], and [[mapUtil]], extend these
built-in types with new methods.  For example, the built-in list
+
built-in types with new methods.  For example, the built-in [[list]]
 
type does not have a .reverse method, but it does once you import
 
type does not have a .reverse method, but it does once you import
 
"listUtil":
 
"listUtil":
Line 17: Line 17:
 
<ms>import "listUtil"
 
<ms>import "listUtil"
 
print [1,2,3].reverse  // prints [3, 2, 1]</ms>
 
print [1,2,3].reverse  // prints [3, 2, 1]</ms>
 +
 +
[[Category:Mini Micro]]

Revision as of 20:09, 27 April 2020

import is a Mini Micro function that loads a MiniScript file from the current directory, /sys/lib, /usr/lib, or some path defined in env.importPaths. import looks for a file with the module name plus ".ms". Any values defined by that code then become available in a map of the same name.

Examples

The following example loads a script called "mathUtil.ms" (normally found in /sys/lib), and makes it available as a map called mathUtil.

import "mathUtil"
print mathUtil.radToDeg(2*pi)  // prints 360

Some modules, like listUtil, stringUtil, and mapUtil, extend these built-in types with new methods. For example, the built-in list type does not have a .reverse method, but it does once you import "listUtil":

import "listUtil"
print [1,2,3].reverse  // prints [3, 2, 1]