Functions

From Adam Colton
Revision as of 16:08, 23 September 2019 by Adamcolton (talk | contribs) (Created page with "== Basics == A function takes some argument and may return a value (or multiple values). == Multiple Returns == Not sure about these, there's an elegance to only allowing a s...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Basics

A function takes some argument and may return a value (or multiple values).

Multiple Returns

Not sure about these, there's an elegance to only allowing a single return type. With the tagged unions, it's not necessary the way it is with go.

Implicit Attributes

Pure

A pure function has no side effects. A pure function invoked with static arguments can be statically replaced at compile time.

Unsafe libraries can override this. The use cases are logging and memoization.

Async

If a function can cause a wait operation it is async. This is implicit to avoid the "what color" problem.

Error Types

Assuming Gryphon allows only a single return, then the most common case would be (like Rust) a tagged union with errors. If the errors are specific, we can create a list of all the possible errors.

Closures

I'm considering not allowing closures. The same thing can be accomplished with anonymous structs:

 func curryAdd(a int) func(b int) int{
   return func(b int) int{
     return a+b
   }
 }
 
 // explicit
 type curryAddStruct struct{
   a int
 }
 func (cas curryAddStruct) add(b int) int{
   return cas.a + b
 }
 func curryAdd(a int) func(b int) int{ 
   return curryAddStruct{a}.add 
 }
 
 // anonymous
 func curryAdd(a int) func(b int) int{
   return func(cas {a:a} struct{a int}) _(b int) int{
     return cas.a+b
   }
 }
 
 // very anonymous
 func curryAdd(a int) func(b int) int{
   return func(a) _(b int) int{
     return a+b
   }
 }

So the receiver on a method can accept anonymous context.