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
Next revision Both sides next revision
script:walkthrough:functions [2019-11-23 21:44]
skyjake [Records as arguments/return value]
script:walkthrough:functions [2019-11-25 08:40]
skyjake
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 68: Line 68:
       return rec       return rec
   end   end
 +  # The following should be avoided:
   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.</​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>​
  
-  *  Passing arguments. +===== Defining ​into a record ​=====
-  *  Passing record as argument. +
-  *  Default values to function arguments. +
-  *  ​Defining ​function in a record+
-  *  Defining a function within a function. +
-  *  Accessing function'​s locals within a sub function. +
-  *  Reference to function. +
-  *  Callback function as argument. +
-  *  Returning a local function from a function.+
  
 +The ''​def''​ statement accepts identifier lookups within the function name. For example, one could define the function //func5// as a member of //myrec//.
  
 +  def myrec.func5(a,​ b)
 +      return a + b
 +  end
  
 +In fact, ''​def''​ behaves like an assignment operator. The above code means:
 +  - Look up //myrec//.
 +  - Create a variable called //func5// within //myrec//.
 +  - Create a function object taking arguments (a, b) and assign it as the value of the variable //​myrec.func5//​.
 +
 +Since functions are actually variables that reference function objects, function definitions can be made within the scope of a function.
 +
 +  def func6()
 +      def func7(): return 'local function'​
 +  end
 +
 +The variable //func7// is not accessible outside //func6//, just like any other local variable.
 +
 +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'
 +  def outer()
 +      a = 100
 +      print '​outer:​ a =', a
 +      def inner()
 +          print '​inner:​ a =', a
 +      end
 +      inner()
 +  end
 +  outer()
 +
 +Output:
 +
 +  outer: a = 100
 +  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.
 +
 +  $ 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.txt · Last modified: 2019-11-25 08:41 by skyjake