Loading...
Loading...
MoonBit (.mbt) coding standards and best practices. Use when writing, reviewing, or refactoring MoonBit code.
npx skill4agent add totto2727-dotfiles/agents moonbit-bestpractice///|moon fmtPositionGeoJSONObjectToBBoxFromJSONJSONToMapfrom_jsonto_geometryboolean_point_in_polygoncoordinatesshifted_polyidjsonppointlsline_string_arraypolygon_arraypolygonsfn newnew_hogefrom_hoge::newfn newfn StructName::new(...)fn newraisenewstruct MyStruct {
x : Int
y : Int
fn new(x~ : Int, y~ : Int) -> MyStruct
}fn MyStruct::new(x~ : Int, y~ : Int) -> MyStruct {
{ x, y }
}new_hogefrom_hoge::newnewStructName(...)StructName::new(...)struct Rect {
x : Double
y : Double
width : Double
height : Double
// Validation in new ensures all Rect instances have valid size
fn new(x~ : Double, y~ : Double, width~ : Double, height~ : Double) -> Rect raise
}
// Conversion: create from a different representation
fn Rect::from_corners(x1~ : Double, y1~ : Double, x2~ : Double, y2~ : Double) -> Rect {
Rect(x=x1, y=y1, width=x2 - x1, height=y2 - y1)
}
// Specific state: create a type with optional fields in a predetermined state
fn Rect::new_unit(x~ : Double, y~ : Double) -> Rect {
Rect(x~, y~, width=1.0, height=1.0)
}StructName::{...}newStructName(...){ ..base, field: value }expr |> ignoreraiseResultsuberror DivError { DivError(String) }
suberror E3 {
A
B(String)
C(Int, loc~ : SourceLoc)
}suberrorsuberror A Bsuberror A { A(B) }raise DivError("division by zero")fail("message")Failurefn div(x : Int, y : Int) -> Int raise DivError { ... }
fn f() -> Unit raise { ... }
fn add(a : Int, b : Int) -> Int noraise { a + b }raise CustomErrorraiseraise Errornoraisetry div(42, 0) catch {
DivError(msg) => println(msg)
} noraise {
v => println(v)
}
let a = div(42, 0) catch { _ => 0 }
let res = try? (div(6, 0) * div(6, 3))
try! div(42, 0)try { expr } catch { pattern => handler } noraise { v => ... }let a = expr catch { _ => default }try? exprResult[T, Error]try! exprraise?fn[T] map(
array : Array[T],
f : (T) -> T raise?
) -> Array[T] raise? { ... }fnoraisemapnoraisefmapraiseResultguardifguardguard array.length() > 0 else { raise Error("Empty") }guardelseguard hoge is Hoge(fuga) else { raise fail("Message") }guardguard feature.geometry is Some(@geojson.Geometry::Polygon(poly))match~match geometry {
Polygon(coordinates~) => coordinates
}args => bodyfnfn(args) { body }fnfnraiseasyncfnfn outer() -> Unit raise {
fn local_fn() -> Unit raise { fail("err") }
let arrow_fn = () => { fail("err") }
}pub struct Point { ... }
pub enum Geometry {
Point(Point)
MultiPoint(MultiPoint)
} derive(Debug, Eq)selfDebugEqCompareToJsonFromJsonHashJsonHashDebugDebugShowDebugShowdebug_inspect()struct MyStruct { ... } derive(Debug, Eq)Failure::Failurelet f = x => Some(x)pub trait ToGeoJSON {
to_geojson(Self) -> GeoJSON
}Selfx..f(){ x.f(); x }Unitlet result = StringBuilder::new()
..write_char('a')
..write_object(1001)
..write_string("abcdef")
.to_string()a..<ba..<=ba..=ba>..ba>=..bfor i in 0..<10 { ... }
for i in 10>=..0 { ... }nobreakelselet r = while i > 0 {
if cond { break 42 }
i = i - 1
} nobreak {
7
}breaknobreakdeclaretodo!declaredeclare fn add(x : Int, y : Int) -> Intiter()minimum()maximum()let min_x = coords.iter().map(c => c.x()).minimum().unwrap()flatten()appendmoon.pkgmoon.pkg.json