Skip to content

Commit

Permalink
add local type
Browse files Browse the repository at this point in the history
  • Loading branch information
Yu-zh authored and peter-jerry-ye committed Dec 17, 2024
1 parent 0eeccf0 commit e763785
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
17 changes: 17 additions & 0 deletions next/language/fundamentals.md
Original file line number Diff line number Diff line change
Expand Up @@ -1096,6 +1096,23 @@ you can leave a type alias `typealias T = @pkgB.T` in `@pkgA`, and **incremental
The type alias can be removed after all uses of `@pkgA.T` is migrated to `@pkgB.T`.
```

### Local types

Moonbit supports declaring structs/enums/newtypes at the top of a toplevel
function, which are only visible within the current toplevel function. These
local types can use the generic parameters of the toplevel function but cannot
introduce additional generic parameters themselves. Local types can derive
methods using derive, but no additional methods can be defined manually. For
example:

```{literalinclude} /sources/language/src/data/top.mbt
:language: moonbit
:start-after: start local-type 1
:end-before: end local-type 1
```

Currently, local types do not support being declared as error types.

## Pattern Matching

Pattern matching allows us to match on specific pattern and bind data from data structures.
Expand Down
14 changes: 14 additions & 0 deletions next/sources/language/src/data/top.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -303,3 +303,17 @@ pub typealias Index = Int
// type alias are private by default
typealias MapString[X] = Map[String, X]
// end typealias 1

// start local-type 1
fn toplevel[T: Show](x: T) -> Unit {
enum LocalEnum {
A(T)
B(Int)
} derive(Show)
struct LocalStruct {
a: (String, T)
} derive(Show)
type LocalNewtype T derive(Show)
...
}
// end local-type 1

0 comments on commit e763785

Please sign in to comment.