User Tools

Site Tools


script:walkthrough:functions

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
script:walkthrough:functions [2019-11-24 10:24]
skyjake
script:walkthrough:functions [2019-11-25 08:41] (current)
skyjake [Defining into a record]
Line 36: Line 36:
  
   def func4()   def func4()
-      record a       ​# '​a'​ owns the record+      record a          # '​a'​ owns the record
       a.value = "​func4"​       a.value = "​func4"​
-      return a       ​# non-owned reference returned+      return a          # non-owned reference returned
   end   end
-  print func4() ​     # throws NullError!+  print func4().value ​  # throws NullError!
  
 In this case, one could instead use the ''​Record()''​ function to return a copy of the record to the caller. In this case, one could instead use the ''​Record()''​ function to return a copy of the record to the caller.
Line 73: Line 73:
 <​note>​It is advisable to not use ''​Record()''​ in arguments. Only use it to assign an owned reference to a local variable, or when returning a copy of a record.</​note>​ <​note>​It is advisable to not use ''​Record()''​ in arguments. Only use it to assign an owned reference to a local variable, or when returning a copy of a record.</​note>​
  
-===== Defining ​in scope =====+===== Defining ​into record ​=====
  
 The ''​def''​ statement accepts identifier lookups within the function name. For example, one could define the function //func5// as a member of //myrec//. The ''​def''​ statement accepts identifier lookups within the function name. For example, one could define the function //func5// as a member of //myrec//.
Line 92: Line 92:
   end   end
  
-//func7// is not accessible ​outside //func6//, just like any other local variable.+//func7// is not visible ​outside //func6//, just like any other local variable. However, one could return //func7// out of //func6// and then it could be used just like a regular top-level function.
  
 Functions defined inside another function do not have visibility to variables in the outer function. Instead, each function has its own local namespace and may access the module'​s global namespace. Functions defined inside another function do not have visibility to variables in the outer function. Instead, each function has its own local namespace and may access the module'​s global namespace.
Line 111: Line 111:
   outer: a = 100   outer: a = 100
   inner: a = global scope   inner: a = global scope
 +
 +
 +===== Function variables =====
  
 Since functions are just variables referencing function objects, one can trivially make an alias for a function with assignment. Unlike records, function objects are reference-counted,​ so a function remains available as long as there is at least one variable pointing to it. Since functions are just variables referencing function objects, one can trivially make an alias for a function with assignment. Unlike records, function objects are reference-counted,​ so a function remains available as long as there is at least one variable pointing to it.
Line 128: Line 131:
   doCallback(10,​ 15, workFunction)   doCallback(10,​ 15, workFunction)
  
-Functions retain an association with the global namespace they are defined in.  
script/walkthrough/functions.1574583894.txt.gz ยท Last modified: 2019-11-24 10:24 by skyjake