WIP on functions

This commit is contained in:
Pat Garrity 2024-07-23 22:11:36 -05:00
parent 9632deccc7
commit 0ba9ac30ea
Signed by: pfm
GPG key ID: 5CA5D21BAB7F3A76

View file

@ -12,6 +12,7 @@ started 2024-07-18.
- [Imports](#imports) - [Imports](#imports)
- [Definitions](#definitions) - [Definitions](#definitions)
- [Comments](#comments) - [Comments](#comments)
- [Constants](#constants)
## Names ## Names
@ -292,11 +293,27 @@ to be either [internal](#internal-accessibility) or
`internal` definitions are only accessible _within their namespace_. This means `internal` definitions are only accessible _within their namespace_. This means
that they are shared across files that share a namespace. that they are shared across files that share a namespace.
```
internal fn foo: Int32 -> Int32
λ x => x + 1
end fn
```
Unused `internal` definitions are considered errors.
### Private Accessibility ### Private Accessibility
`private` definitions are only accessible _within their file_. Even if another `private` definitions are only accessible _within their file_. Even if another
file shares a namespace, `private` definitions are invisible to that file. file shares a namespace, `private` definitions are invisible to that file.
```
private fn foo: Int32 -> Int32
λ x => x + 1
end fn
```
Unused `private` definitions are considered errors.
## Comments ## Comments
_Comments_ are sections of code that are not parsed as code. They are used to _Comments_ are sections of code that are not parsed as code. They are used to
@ -330,3 +347,98 @@ fn foo: String -> String
λ x => x + x λ x => x + x
end fn end fn
``` ```
## Constants
_Constants_ are top-level [definitions](#definitions) in Ava. They are named,
concrete values with a concrete type.
### Rules
- Constants must be defined at the top level of Ava source files.
- Constants must have an explicit type (they may not infer).
- Constants must have a concrete type.
- Constants must have a literal value.
- Constants _may_ reference the value of another constant.
- Constants _may_ be a record, itself defined in terms of literal values or
constant values.
Note that these rules exist to permit constant definitions that are not limited
to literal values.
### Syntax
Constants are defined using the [const](#const) keyword.
```
const <name>: <type> = <value>
```
### Examples
```
const foo: String = "foo"
const bar: Int32 = 12
record Foo(x: String, y: Int32)
const baz: Foo = Foo(foo, bar)
```
## Functions
_Functions_ are top-level [definitions](#definitions) in Ava, values in Ava, and
are the heart of programming in Ava. In general, functions have type `A -> B`,
where input of type `A` produces a result of type `B`.
### Functions Without Arguments
Technically, there are _no_ functions without arguments in Ava. However, the
`Unit` type (the empty tuple) may be used to express that case.
```
fn example: Unit -> Int32
λ _ => 10
end fn
let demonstrate: Int32 = example ()
```
### Functions With Multiple Arguments
Arguments should be chained with `->` in Ava:
```
import ava.string
fn example: String -> Int32 -> Int32
λ x y => (string.length x) + y
end fn
```
Technically, this represents a curried function with left associativity:
```
A -> B -> C -> D == ((A -> B) -> C) -> D
```
This means that Ava naturally supports curried functions with partial
application:
```
fn example: Int32 -> Int32 -> Int32 -> Int32
λ x y z => x + y + z
end fn
let add1: Int32 -> Int32 -> Int32 = example 1
-- Result: 6
let result = add1 2 3
```
In this case, `example 1` invokes `example` with an input of literal value `1`,
which results in a partial application of example that looks like:
```
fn partial: Int32 -> Int32 -> Int32
λ y z => 1 + y + z
end fn
```