Functions: Difference between revisions
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..." |
Adamcolton (talk | contribs) No edit summary |
||
| Line 51: | Line 51: | ||
} | } | ||
So the receiver on a method can accept anonymous context. | So the receiver on a method can accept anonymous context. | ||
== Inline == | |||
The compiler should be able to make an informed decision about inlining function calls. We may want a compiler flag to help tweak the threshold (setting the desired balance between performance and size). | |||
Of course, a function only called from one place should always be inlined. | |||
[[Category:Gryphon]] | [[Category:Gryphon]] | ||
Revision as of 17:40, 23 September 2019
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.
Inline
The compiler should be able to make an informed decision about inlining function calls. We may want a compiler flag to help tweak the threshold (setting the desired balance between performance and size).
Of course, a function only called from one place should always be inlined.