Notifications
Clear all

[Closed] Functions arguments

Hey

  1. How can i put one function into another as argument:

something like this easy example


 FN fn_001 someString = 
 (
    format"String: %
" someString
 )
 
 FN fn_002 number someMethod --or &someMethod =
 (
    for i = 1 to number do (someMethod)
 )
 
  1. How can i set type of argument.
    e.x: i want to fn_001 takes only Strings as argument and throw error (in try/catch statement) for any other types of values as argument
6 Replies

FN fn_001 someString =
(
–throw error on wrong class
if (classOf someString) != String do ( throw “grrr!” )

    format"String: %

” someString
)

–Call a passed function “fun” from within a function
FN fn_002 number fun =
(
for i = 1 to number do (fun i)
)

– pass fn_001 to fun_002
fn_002 1 fn_001

I thought about if (classOf someString) != String, but isnt there another way (in declaration line)? Like for ex


  FN fn_001 val (String) = ()... 
 or 
 FN fn_001 String:val =() or something like this?
  

I dont want to print value i but string from another function (how to apply function with argument to another function as argument).

I have tried this

    fn_002 3 (fn_001 "asdasds")
    
to call...but on exit there is only 1 string "asdasds"

fn fn1 aString= (format"String: %
" aString)

fn fn2 num method arg=
 (
	 if classof arg==string then (
		
		/*
		 since (method as string) returns the string "fn()"
		 we substuitute "()" with the arg, wrapped in escaped quotes with a leading space
		 */

		command=(substitutestring (method as string) "()" (" \""+arg+"\""))
		
		/*
		 the resullt is  the string 'fn1 "hello"' which we pass to execute
		*/

		for i = 1 to num do execute(command)

	)else(
		 format "ERROR Arg Expected Type String, got Type: %
" (classof arg)
	 )
 )
 
fn2 10 fn1 "hello"

Thanks a lot Mambo4

fn executeAction action arg = 
(
      action arg
)
fn doSomething thing =
(
      format "thing: %
" thing
)

executeAction doSomething "example"

it’s an example… now is a practical using:

fn processNodes nodes action: =
(
      action nodes
)
processNodes objects action:select
processNodes helpers action:hide
processNodes selection action:delete

that means you can pass a function as argument and optional argument to another function

oooo that’s pretty.