8000 migrate syntax change by Guest0x0 · Pull Request #2258 · moonbitlang/core · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

migrate syntax change #2258

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 12, 2025
Merged

migrate syntax change #2258

merged 6 commits into from
Jun 12, 2025

Conversation

Guest0x0
Copy link
Contributor

We are making some syntax changes before beta release, this PR migrates core:

  • ! is replaced by raise, for example (..) -> T!Error will become (..) -> T raise Error, -> T?Error is replaced by -> T raise?
  • type! is replaced by a new keyword suberror
  • Since extern type have very different semantic in different backends, we decided to deprecate this syntax and use an attribute #external instead. This gives more flexibility for future adjustment, if some backend demand richer FFI type declaration
  • Currently, default value of optional argument can depend on previous arguments. But this feature is incompatible with virtual package, because we cannot tell the dependencies of an optional argument's default value from .mbti interface. So we decided to deprecate complex default value that depend on previous arguments. This kind of default value can be replaced by label? : T and a match on label inside function body, so there is no loss in expressiveness
  • typealias A = B is deprecated and replaced by typealias B as A, similarly for traitalias. This simplifies MoonBit's syntax without loss of expresiveness
  • loop with multiple parameters is deprecated. Users can alternatively use loop with a single tuple parameter. MoonBit compiler is capable to optimize away the tuple, so there will be no loss in performance. This change makes match and loop more consistent
  • try can now be omitted when catching error in a simple expression, e.g. f() catch { _ => panic() }. This syntax is inspired by Zig

Copy link
Optional argument syntax can be improved

Category
Maintainability
Code Snippet
pub fn String::char_length(
self : String,
start_offset~ : Int = 0,
end_offset~ : Int = self.length()
) -> Int
Recommendation
pub fn String::char_length(
self : String,
start_offset~ : Int = 0,
end_offset? : Int
) -> Int {
let end_offset = if end_offset is Some(o) { o } else { self.length() }
...
}
Reasoning
Complex default values for optional arguments should be replaced with ? parameter and handled inside function body for better maintainability and compatibility with virtual packages

Error handling syntax needs to be updated

Category
Correctness
Code Snippet
pub fn[A : FromStr] parse(str : String) -> A!StrConvError
Recommendation
pub fn[A : FromStr] parse(str : String) -> A raise StrConvError
Reasoning
The ! syntax for error handling is being deprecated in favor of the more explicit raise keyword to better express error propagation

External type declarations need attribute syntax

Category
Maintainability
Code Snippet
extern type JSArray
Recommendation
#external
type JSArray
Reasoning
Using attribute syntax #external instead of extern type provides more flexibility for future backend-specific FFI type declarations

@Guest0x0 Guest0x0 merged commit 749c070 into main Jun 12, 2025
24 of 28 checks passed
@Guest0x0 Guest0x0 deleted the Guest0x0/migrate-syntax-change branch June 12, 2025 07:44
@coveralls
Copy link
Collaborator

Pull Request Test Coverage Report for Build 7234

Warning: This coverage report may be inaccurate.

This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.

Details

  • 14 of 14 (100.0%) changed or added relevant lines in 8 files are covered.
  • 1 unchanged line in 1 file lost coverage.
  • Overall coverage increased (+0.001%) to 93.112%

Files with Coverage Reduction New Missed Lines %
immut/list/list.mbt 1 89.66%
Totals Coverage Status
Change from base Build 7231: 0.001%
Covered Lines: 8570
Relevant Lines: 9204

💛 - Coveralls

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
0