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 07:15]
skyjake [Defining in a scope]
script:walkthrough:functions [2022-09-27 07:24] (current)
skyjake
Line 31: Line 31:
  
  
-===== Records as arguments/return value =====+===== Records as arguments =====
  
-Records are passed by reference both in arguments and in the return value. This means that if a record is created as a local variable, it will be deleted even though it is returned. +Records are passed by non-owned reference ​in arguments.
- +
-  def func4() +
-      record a       # '​a'​ owns the record +
-      a.value = "​func4"​ +
-      return a       # non-owned reference returned +
-  end +
-  print func4() ​     # throws NullError! +
- +
-In this case, one could instead use the ''​Record()''​ function to return a copy of the record to the caller. +
- +
-  return Record(a) +
- +
-When passing a record as argument, the function gets a non-owned reference.+
  
   record myrec   record myrec
Line 71: Line 58:
   a = do_init(Record()) ​ # returns null ref!   a = do_init(Record()) ​ # returns null ref!
  
-<​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>​Avoid using ''​Record()''​ in arguments. ​Instead, only use it to assign an owned reference to a local variable, or when returning a copy of a record ​(see next section).</​note>​
  
-===== Defining ​in a scope =====+===== Records as return value ===== 
 + 
 +Records are passed by non-owned reference ​in the return value. This means that if record is created as a local variable, it will be deleted even when it used as a return value (!). 
 + 
 +  def func4() 
 +      record a          # '​a'​ owns the record 
 +      a.value = "​func4"​ 
 +      return a          # non-owned reference returned 
 +  end                   # ​scope of '​a'​ ends 
 +  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. 
 + 
 +  return Record(a) 
 + 
 +===== Defining into a 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 94:
   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 do not have visibility to variables in the parent scope.+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.
  
   a = '​global scope'   a = '​global scope'
-  def func8()+  def outer()
       a = 100       a = 100
-      print 'Inside func8: a =', a +      print 'outer: a =', a 
-      def func() +      def inner() 
-          print 'Inside func: a =', a+          print 'inner: a =', a
       end       end
-      ​func()+      ​inner()
   end   end
-  ​func8()+  ​outer()
  
 Output: Output:
  
-  ​Inside func8: a = 100 +  ​outer: a = 100 
-  ​Inside func: a = global scope+  ​inner: a = global scope
  
  
-  *  Accessing function'​s locals within a sub function. +===== Function variables =====
-  *  Reference to function. +
-  *  Callback function as argument. +
-  *  Returning a local function from a function.+
  
 +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.
  
 +  $ def someFunc(): return '​works'​
 +  ​
 +  $ other = someFunc
 +  ​
 +  $ print someFunc(), other()
 +  works works
  
 +Functions can be passed as arguments.
 +
 +  def doCallback(a,​ b, cb)
 +      return cb(a, b)
 +  end
 +  doCallback(10,​ 15, workFunction)
  
script/walkthrough/functions.1574572511.txt.gz · Last modified: 2019-11-24 07:15 by skyjake