Skip to content

Commit

Permalink
Merge pull request #313 from moonbitlang/impl-syntax
Browse files Browse the repository at this point in the history
update impl syntax
  • Loading branch information
qazxcdswe123 authored Oct 11, 2024
2 parents 82e9cef + 68fed8d commit def4c8f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
12 changes: 9 additions & 3 deletions moonbit-docs/docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1991,9 +1991,15 @@ trait ToMyBinaryProtocol {
}
impl ToMyBinaryProtocol for Int with to_my_binary_protocol(x, b) { ... }
impl ToMyBinaryProtocol for Int with to_my_binary_protocol(x, b) { ... }
impl ToMyBinaryProtocol for Int with to_my_binary_protocol(x, b) { ... }
impl[X : ToMyBinaryProtocol] for Array[X] with to_my_binary_protocol(arr, b) { ... }
impl ToMyBinaryProtocol for UInt with to_my_binary_protocol(x, b) { ... }
impl[X : ToMyBinaryProtocol] ToMyBinaryProtocol for Array[X] with to_my_binary_protocol(
arr,
b
) {
...
}
```

When searching for the implementation of a trait, `impl`s have a higher priority, so they can be used to override ordinary methods with undesirable behavior. `impl`s can only be used to implement the specified trait. They cannot be called directly like ordinary methods. Furthermore, _only the package of the type or the package of the trait can define an implementation_. For example, only `@pkg1` and `@pkg2` are allowed to define `impl @pkg1.Trait for @pkg2.Type` for type `@pkg2.Type`. This restriction ensures that MoonBit's trait system is still coherent with the extra flexibility of `impl`s.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1965,9 +1965,15 @@ trait ToMyBinaryProtocol {
}
impl ToMyBinaryProtocol for Int with to_my_binary_protocol(x, b) { ... }
impl ToMyBinaryProtocol for Int with to_my_binary_protocol(x, b) { ... }
impl ToMyBinaryProtocol for Int with to_my_binary_protocol(x, b) { ... }
impl[X : ToMyBinaryProtocol] for Array[X] with to_my_binary_protocol(arr, b) { ... }
impl ToMyBinaryProtocol for UInt with to_my_binary_protocol(x, b) { ... }
impl[X : ToMyBinaryProtocol] ToMyBinaryProtocol for Array[X] with to_my_binary_protocol(
arr,
b
) {
...
}
```

在搜索某个接口的实现时,`impl` 比普通方法有更高的优先级,
Expand Down

0 comments on commit def4c8f

Please sign in to comment.