From 342b4b430c3629551d5f19888cef2136c76cdd86 Mon Sep 17 00:00:00 2001 From: tiye Date: Sat, 30 Nov 2024 02:17:21 +0800 Subject: [PATCH 1/4] adding some examples --- src/main/container.mbt | 13 +++- src/main/snippets.mbt | 141 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 149 insertions(+), 5 deletions(-) diff --git a/src/main/container.mbt b/src/main/container.mbt index 880796e..7fc5bc7 100644 --- a/src/main/container.mbt +++ b/src/main/container.mbt @@ -34,22 +34,29 @@ fn comp_container() -> @respo_node.RespoNode[ActionOp] { comp_topic("Variables And Constants"), comp_compare(mbt_variable, rs_variable), comp_topic("Explicit Types"), + comp_compare(mbt_explict_type, rs_explict_type), comp_topic("Type Coercion"), + comp_compare(mbt_type_coercion, rs_type_coercion), comp_topic("String Interpolation"), + comp_compare(mbt_string_interpolation, rs_string_interpolation), comp_topic("Range Operator"), - comp_topic("Inclusive Range Operator"), + comp_compare(mbt_range_operator, rs_range_operator), comp_section("COLLECTIONS"), comp_topic("Arrays"), + comp_compare(mbt_arrays, rs_arrays), comp_topic("Maps"), - comp_topic("Empty Collections"), + comp_compare(mbt_maps, rs_maps), comp_section("FUNCTIONS"), comp_topic("Functions"), + comp_compare(mbt_functions, rs_functions), comp_topic("Tuple Return"), - comp_topic("Variable Number Of Arguments"), + comp_compare(mbt_tuple_return, rs_tuple_return), comp_topic("Function Type"), + comp_compare(mbt_function_type, rs_function_type), comp_topic("Map"), comp_topic("Sort"), comp_topic("Named Arguments"), + comp_compare(mbt_named_arguments, rs_named_arguments), comp_section("TRAITS"), comp_topic("Declaration"), comp_topic("Usage"), diff --git a/src/main/snippets.mbt b/src/main/snippets.mbt index cb14b83..a9fcd50 100644 --- a/src/main/snippets.mbt +++ b/src/main/snippets.mbt @@ -10,7 +10,144 @@ let rs_variable : String = #| #| let my_constant: usize = 42: -// fn demo() { -// let mut my_variable : Int = 42 +let mbt_explict_type : String = + #| let explicit_double: Double = 70 + +let rs_explict_type : String = + #| let explicit_double: f64 = 70.0; + +let mbt_type_coercion : String = + #| let label: String = "The width is " + #| let width: Int = 94 + #| let width_label : String = label + width.to_string() + +let rs_type_coercion : String = + #| let label: &str = "The width is "; + #| let width: usize = 94; + #| let width_label: String = label.to_string() + &width.to_string(); + +let mbt_string_interpolation : String = + #| let apples: Int = 3 + #| let oranges: Int = 5 + #| let fruit_summary : String = "I have \{apples + oranges} " + + #| "pieces of fruit." + +let rs_string_interpolation : String = + #| let apples: usize = 3; + #| let oranges: usize = 5; + #| let fruit_summary: String = format!("I have {} pieces of fruit.", apples + oranges); + +let mbt_range_operator : String = + #| // nope + +let rs_range_operator : String = + #| let names = vec!["Anna", "Alex", "Brian", "Jack"]; + #| let count = names.len(); + #| for i in 0..count { + #| println!("Person {} is called {}", i + 1, names[i]); + #| } + +let mbt_arrays : String = + #| let shopping_list = ["catfish", "water", "tulips", "blue paint"] + #| shopping_list[1] = "bottle of water" + +let rs_arrays : String = + #| let mut shopping_list = vec!["catfish", "water", "tulips", "blue paint"]; + #| shopping_list[1] = "bottle of water"; + +// var occupations = [ +// "Malcolm": "Captain", +// "Kaylee": "Mechanic", +// ] +// occupations["Jayne"] = "Public Relations" + +let mbt_maps : String = + #| let occupations = { + #| "Malcolm": "Captain", + #| "Kaylee": "Mechanic", + #| } + #| occupations["Jayne"] = "Public Relations" + +let rs_maps : String = + #| let mut occupations = HashMap::new(); + #| occupations.insert("Malcolm", "Captain"); + #| occupations.insert("Kaylee", "Mechanic"); + #| occupations.insert("Jayne", "Public Relations"); + +let mbt_functions : String = + #| let greet = fn(name: String, day: String) -> String { + #| "Hello \{name}, today is \{day}." + #| } + #| + #| fn main() { + #| greet("Bob", "Tuesday") + #| } + +let rs_functions : String = + #| fn greet(name: &str, day: &str) -> String { + #| format!("Hello {}, today is {}.", name, day) + #| } + #| + #| fn main() { + #| greet("Bob", "Tuesday"); + #| } + #| + +let mbt_tuple_return : String = + #| fn get_gas_prices() -> (Double, Double, Double) { + #| (3.59, 3.69, 3.79) + #| } + +let rs_tuple_return : String = + #| fn get_gas_prices() -> (f64, f64, f64) { + #| (3.59, 3.69, 3.79) + #| } + +let mbt_function_type : String = + #| fn make_incrementer() -> (Int) -> Int { + #| fn increment(number : Int) -> Int { + #| 1 + number + #| } + #| + #| increment + #| } + #| + #| fn main() { + #| let increment = make_incrementer() + #| increment(7) + #| let increment = make_incrementer() + #| } + +let rs_function_type : String = + #| fn make_incrementer() -> fn(i32) -> i32 { // TODO: check this + #| fn increment(number: i32) -> i32 { + #| 1 + number + #| } + #| increment + #| } + #| + #| fn main() { + #| let increment = make_incrementer(); + #| increment(7); + #| let increment = make_incrementer(); + #| } + +// def area(width: Int, height: Int) -> Int { +// return width * height +// } + +// area(width: 10, height: 10) +let mbt_named_arguments : String = + #| fn area(width~ : Int, height~ : Int) -> Int { + #| width * height + #| } + #| + #| fn main() { + #| area(width=10, height=10) + #| } + +let rs_named_arguments : String = "// nope" + +// fn demo() { // } From 70ffda027445fdab6a6453d5ecc306265a58606e Mon Sep 17 00:00:00 2001 From: tiye Date: Sun, 8 Dec 2024 01:51:05 +0800 Subject: [PATCH 2/4] update moonbit formatter --- README.md | 17 ++++++++++++-- moon.mod.json | 2 +- src/main/container.mbt | 12 ++++++++-- src/main/main.mbt | 3 +++ src/main/snippets.mbt | 51 ++++++++++++++++++++++++++++++++++++++++++ src/main/store.mbt | 12 +++++++++- 6 files changed, 91 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 2e8e436..ef916a1 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,20 @@ -## Boilerplate project for Respo +# MoonBit code comparison + +Run in development mode: + +```bash +moon build --target js --debug --watch + +yarn +yarn vite +``` + +Build assets: ```bash -yarn build +moon build --target js +yarn +yarn vite build --base ./ ``` ## License diff --git a/moon.mod.json b/moon.mod.json index 1bbf170..7321335 100644 --- a/moon.mod.json +++ b/moon.mod.json @@ -2,7 +2,7 @@ "name": "tiye/respo-workflow", "version": "0.1.0", "deps": { - "tiye/respo": "0.0.17", + "tiye/respo": "0.0.19", "tiye/dom-ffi": "0.0.6" }, "readme": "README.md", diff --git a/src/main/container.mbt b/src/main/container.mbt index 7fc5bc7..3abaa9b 100644 --- a/src/main/container.mbt +++ b/src/main/container.mbt @@ -1,3 +1,4 @@ +///| fn comp_container() -> @respo_node.RespoNode[ActionOp] { div( class_name=str_spaced( @@ -58,7 +59,8 @@ fn comp_container() -> @respo_node.RespoNode[ActionOp] { comp_topic("Named Arguments"), comp_compare(mbt_named_arguments, rs_named_arguments), comp_section("TRAITS"), - comp_topic("Declaration"), + comp_topic("Methods"), + comp_compare(mbt_struct_methods, rs_struct_methods), comp_topic("Usage"), comp_topic("Default Implementation"), comp_topic("Downcasting"), @@ -73,6 +75,7 @@ fn comp_container() -> @respo_node.RespoNode[ActionOp] { ) } +///| fn comp_section(title : String) -> @respo_node.RespoNode[ActionOp] { div( class_name=str_spaced([]), @@ -88,6 +91,7 @@ fn comp_section(title : String) -> @respo_node.RespoNode[ActionOp] { ) } +///| fn comp_topic(title : String) -> @respo_node.RespoNode[ActionOp] { div( class_name=str_spaced([@respo.ui_center]), @@ -102,6 +106,7 @@ fn comp_topic(title : String) -> @respo_node.RespoNode[ActionOp] { ) } +///| fn comp_compare( mbt_code : String, rs_code : String @@ -129,7 +134,7 @@ fn comp_compare( ) } -/// a custom style for code block +///| a custom style for code block fn code_block[T]( class_name? : String, style? : @respo_node.RespoStyle, @@ -149,11 +154,13 @@ fn code_block[T]( ) } +///| fn str_some_spaced(arr : Array[String?]) -> String { arr.filter(fn(x) { not(x.is_empty()) }).map(fn(x) { x.unwrap() }) |> String::concat(separator=" ") } +///| let style_code_block : String = declare_static_style( [ ( @@ -171,6 +178,7 @@ let style_code_block : String = declare_static_style( ], ) +///| let style_lang_name : String = declare_static_style( [ ( diff --git a/src/main/main.mbt b/src/main/main.mbt index 627d2ae..9770245 100644 --- a/src/main/main.mbt +++ b/src/main/main.mbt @@ -1,5 +1,7 @@ +///| let app_store_key : String = "mbt-workflow" +///| fn view( _store : Store ) -> @respo_node.RespoNode[ActionOp]!@respo_node.RespoCommonError { @@ -11,6 +13,7 @@ fn view( comp_container() } +///| fn main { let window = @dom_ffi.window() let mount_target = window diff --git a/src/main/snippets.mbt b/src/main/snippets.mbt index a9fcd50..e2cd328 100644 --- a/src/main/snippets.mbt +++ b/src/main/snippets.mbt @@ -1,45 +1,55 @@ +///| let mbt_variable : String = #| let mut my_variable: Int = 42 #| my_variable = 50 #| #| let my_constant = 42 +///| let rs_variable : String = #| let mut my_variable: usize = 42; #| my_variable = 50; #| #| let my_constant: usize = 42: +///| let mbt_explict_type : String = #| let explicit_double: Double = 70 +///| let rs_explict_type : String = #| let explicit_double: f64 = 70.0; +///| let mbt_type_coercion : String = #| let label: String = "The width is " #| let width: Int = 94 #| let width_label : String = label + width.to_string() +///| let rs_type_coercion : String = #| let label: &str = "The width is "; #| let width: usize = 94; #| let width_label: String = label.to_string() + &width.to_string(); +///| let mbt_string_interpolation : String = #| let apples: Int = 3 #| let oranges: Int = 5 #| let fruit_summary : String = "I have \{apples + oranges} " + #| "pieces of fruit." +///| let rs_string_interpolation : String = #| let apples: usize = 3; #| let oranges: usize = 5; #| let fruit_summary: String = format!("I have {} pieces of fruit.", apples + oranges); +///| let mbt_range_operator : String = #| // nope +///| let rs_range_operator : String = #| let names = vec!["Anna", "Alex", "Brian", "Jack"]; #| let count = names.len(); @@ -47,10 +57,12 @@ let rs_range_operator : String = #| println!("Person {} is called {}", i + 1, names[i]); #| } +///| let mbt_arrays : String = #| let shopping_list = ["catfish", "water", "tulips", "blue paint"] #| shopping_list[1] = "bottle of water" +///| let rs_arrays : String = #| let mut shopping_list = vec!["catfish", "water", "tulips", "blue paint"]; #| shopping_list[1] = "bottle of water"; @@ -61,6 +73,7 @@ let rs_arrays : String = // ] // occupations["Jayne"] = "Public Relations" +///| let mbt_maps : String = #| let occupations = { #| "Malcolm": "Captain", @@ -68,12 +81,14 @@ let mbt_maps : String = #| } #| occupations["Jayne"] = "Public Relations" +///| let rs_maps : String = #| let mut occupations = HashMap::new(); #| occupations.insert("Malcolm", "Captain"); #| occupations.insert("Kaylee", "Mechanic"); #| occupations.insert("Jayne", "Public Relations"); +///| let mbt_functions : String = #| let greet = fn(name: String, day: String) -> String { #| "Hello \{name}, today is \{day}." @@ -83,6 +98,7 @@ let mbt_functions : String = #| greet("Bob", "Tuesday") #| } +///| let rs_functions : String = #| fn greet(name: &str, day: &str) -> String { #| format!("Hello {}, today is {}.", name, day) @@ -93,16 +109,19 @@ let rs_functions : String = #| } #| +///| let mbt_tuple_return : String = #| fn get_gas_prices() -> (Double, Double, Double) { #| (3.59, 3.69, 3.79) #| } +///| let rs_tuple_return : String = #| fn get_gas_prices() -> (f64, f64, f64) { #| (3.59, 3.69, 3.79) #| } +///| let mbt_function_type : String = #| fn make_incrementer() -> (Int) -> Int { #| fn increment(number : Int) -> Int { @@ -118,6 +137,7 @@ let mbt_function_type : String = #| let increment = make_incrementer() #| } +///| let rs_function_type : String = #| fn make_incrementer() -> fn(i32) -> i32 { // TODO: check this #| fn increment(number: i32) -> i32 { @@ -138,6 +158,7 @@ let rs_function_type : String = // area(width: 10, height: 10) +///| let mbt_named_arguments : String = #| fn area(width~ : Int, height~ : Int) -> Int { #| width * height @@ -147,7 +168,37 @@ let mbt_named_arguments : String = #| area(width=10, height=10) #| } +///| let rs_named_arguments : String = "// nope" +// class Shape { +// var numberOfSides = 0 +// func simpleDescription() -> String { +// return "A shape with \(numberOfSides) sides." +// } +// } + +///| +let mbt_struct_methods = + #| struct Shape { + #| number_of_sides : Int + #| } + #| + #| fn Shape::simple_description(self : Shape) -> String { + #| "A shape with {} sides.\{self.number_of_sides}" + #| } + +///| +let rs_struct_methods = + #| struct Shape { + #| number_of_sides: i32, + #| } + #| + #| impl Shape { + #| fn simple_description(&self) -> String { + #| format!("A shape with {} sides.", self.number_of_sides) + #| } + #| } + // fn demo() { // } diff --git a/src/main/store.mbt b/src/main/store.mbt index e0a700f..9541831 100644 --- a/src/main/store.mbt +++ b/src/main/store.mbt @@ -1,13 +1,16 @@ +///| struct Store { mut counted : Int tasks : Array[Task] states : @respo.RespoStatesTree } derive(ToJson, @json.FromJson) +///| fn Store::default() -> Store { { counted: 0, tasks: [], states: @respo.RespoStatesTree::default() } } +///| struct Task { id : String done : Bool @@ -15,10 +18,12 @@ struct Task { time : Double } derive(Default, Eq, Hash, ToJson, @json.FromJson) +///| enum IndentOp { Noop } +///| enum ActionOp { Noop StatesChange(@respo.RespoUpdateState) @@ -28,14 +33,17 @@ enum ActionOp { IncTwice } +///| fn ActionOp::default() -> ActionOp { Noop } +///| impl @respo_node.RespoAction for ActionOp with build_states_action(cursor, a) { ActionOp::StatesChange({ cursor, data: Some(a.to_json()) }) } +///| fn ActionOp::to_string(self : ActionOp) -> String { match self { Noop => "Noop" @@ -48,11 +56,12 @@ fn ActionOp::to_string(self : ActionOp) -> String { } } +///| fn get_states(self : Store) -> @respo.RespoStatesTree { self.states } -/// TODO mutation might break memoization infuture +///| TODO mutation might break memoization infuture fn update(self : Store, op : ActionOp) -> Unit { match op { Increment => self.counted += 1 @@ -62,6 +71,7 @@ fn update(self : Store, op : ActionOp) -> Unit { } } +///| fn to_string(self : Store) -> String { self.to_json().stringify(indent=2) } From 3cb9afd37a61023710ea86089b8084121b3007f4 Mon Sep 17 00:00:00 2001 From: tiye Date: Sun, 8 Dec 2024 02:24:32 +0800 Subject: [PATCH 3/4] get basic styles for page --- index.html | 17 +++++ src/main/container.mbt | 156 ++++++++++++++++++++++++++++++----------- src/main/snippets.mbt | 120 ++++++++++++++++++++++++++++--- 3 files changed, 243 insertions(+), 50 deletions(-) diff --git a/index.html b/index.html index 2d620be..b3a3554 100644 --- a/index.html +++ b/index.html @@ -2,6 +2,23 @@ MoonBit is like Rust + + + diff --git a/src/main/container.mbt b/src/main/container.mbt index 3abaa9b..a64a8f3 100644 --- a/src/main/container.mbt +++ b/src/main/container.mbt @@ -18,57 +18,97 @@ fn comp_container() -> @respo_node.RespoNode[ActionOp] { ), [ text_node("MoonBit is like Rust (Except that it has GC)"), - text_node("Fork repo if you want to contribute."), + @respo_node.a( + href="https://github.com/fp-china/moonbit-is-like-rust", + target=Blank, + inner_text="Fork repo if you want to contribute.", + ), ], ), div( style=respo_style(background_color=RawString("#eee")), [ div( - style=respo_style(max_width=Px(1400), margin=Auto), + class_name=style_section, [ - comp_section("BASICS"), - comp_topic("Hello World"), - comp_compare( - "println(\"Hello, World!\")", "println!(\"Hello, World!\");", + div( + class_name=style_section_inner, + [ + comp_section("BASICS"), + comp_topic("Hello World"), + comp_compare( + "println(\"Hello, World!\")", "println!(\"Hello, World!\");", + ), + comp_topic("Variables And Constants"), + comp_compare(mbt_variable, rs_variable), + comp_topic("Explicit Types"), + comp_compare(mbt_explict_type, rs_explict_type), + comp_topic("Type Coercion"), + comp_compare(mbt_type_coercion, rs_type_coercion), + comp_topic("String Interpolation"), + comp_compare( + mbt_string_interpolation, rs_string_interpolation, + ), + comp_topic("Range Operator"), + comp_compare(mbt_range_operator, rs_range_operator), + ], ), - comp_topic("Variables And Constants"), - comp_compare(mbt_variable, rs_variable), - comp_topic("Explicit Types"), - comp_compare(mbt_explict_type, rs_explict_type), - comp_topic("Type Coercion"), - comp_compare(mbt_type_coercion, rs_type_coercion), - comp_topic("String Interpolation"), - comp_compare(mbt_string_interpolation, rs_string_interpolation), - comp_topic("Range Operator"), - comp_compare(mbt_range_operator, rs_range_operator), - comp_section("COLLECTIONS"), - comp_topic("Arrays"), - comp_compare(mbt_arrays, rs_arrays), - comp_topic("Maps"), - comp_compare(mbt_maps, rs_maps), - comp_section("FUNCTIONS"), - comp_topic("Functions"), - comp_compare(mbt_functions, rs_functions), - comp_topic("Tuple Return"), - comp_compare(mbt_tuple_return, rs_tuple_return), - comp_topic("Function Type"), - comp_compare(mbt_function_type, rs_function_type), - comp_topic("Map"), - comp_topic("Sort"), - comp_topic("Named Arguments"), - comp_compare(mbt_named_arguments, rs_named_arguments), - comp_section("TRAITS"), - comp_topic("Methods"), - comp_compare(mbt_struct_methods, rs_struct_methods), - comp_topic("Usage"), - comp_topic("Default Implementation"), - comp_topic("Downcasting"), - comp_topic("Protocol"), - comp_topic("Extensions"), - @respo_node.space(height=200), ], ), + div( + class_name=style_section, + [ + div( + class_name=style_section_inner, + [ + comp_section("COLLECTIONS"), + comp_topic("Arrays"), + comp_compare(mbt_arrays, rs_arrays), + comp_topic("Maps"), + comp_compare(mbt_maps, rs_maps), + ], + ), + ], + ), + div( + class_name=style_section, + [ + div( + class_name=style_section_inner, + [ + comp_section("FUNCTIONS"), + comp_topic("Functions"), + comp_compare(mbt_functions, rs_functions), + comp_topic("Tuple Return"), + comp_compare(mbt_tuple_return, rs_tuple_return), + comp_topic("Function Type"), + comp_compare(mbt_function_type, rs_function_type), + comp_topic("HsahMap"), + comp_compare(mbt_hashmap, rs_hashmap), + comp_topic("Named Arguments"), + comp_compare(mbt_named_arguments, rs_named_arguments), + ], + ), + ], + ), + div( + class_name=style_section, + [ + div( + class_name=style_section_inner, + [ + comp_section("TRAITS"), + comp_topic("Methods"), + comp_compare(mbt_struct_methods, rs_struct_methods), + comp_topic("Trait Objects"), + comp_compare(mbt_trait_objects, rs_trait_objects), + comp_topic("Default Implementation"), + comp_compare(mbt_default_trait_impl, rs_default_trait_impl), + ], + ), + ], + ), + @respo_node.space(height=400), ], ), ], @@ -112,7 +152,7 @@ fn comp_compare( rs_code : String ) -> @respo_node.RespoNode[ActionOp] { div( - class_name=str_spaced([@respo.ui_row]), + class_name="ui-compare-by-width", // Respo does not support media query yet style=respo_style(padding=Px(12)), [ div( @@ -172,6 +212,7 @@ let style_code_block : String = declare_static_style( border_radius=4, line_height=Em(1.6), font_size=13, + overflow=Auto, ), ), ("pre&", respo_style(margin=Px(0))), @@ -192,3 +233,34 @@ let style_lang_name : String = declare_static_style( ), ], ) + +///| +let style_section : String = declare_static_style( + [ + ("&", respo_style(background_color=White)), + // set code color + ("& .\{style_code_block}", respo_style(background_color=RawString("#eee"))), + // every 2nd of parent + ("&:nth-child(2n)", respo_style(background_color=RawString("#eee"))), + // and code block has changed color too + ( + "&:nth-child(2n) .\{style_code_block}", + respo_style(background_color=White), + ), + ], +) + +///| +let style_section_inner : String = declare_static_style( + [ + ( + "&", + respo_style( + max_width=Px(1400), + margin=Auto, + padding_top=Px(60), + padding_bottom=Px(60), + ), + ), + ], +) diff --git a/src/main/snippets.mbt b/src/main/snippets.mbt index e2cd328..b355c2b 100644 --- a/src/main/snippets.mbt +++ b/src/main/snippets.mbt @@ -67,17 +67,11 @@ let rs_arrays : String = #| let mut shopping_list = vec!["catfish", "water", "tulips", "blue paint"]; #| shopping_list[1] = "bottle of water"; -// var occupations = [ -// "Malcolm": "Captain", -// "Kaylee": "Mechanic", -// ] -// occupations["Jayne"] = "Public Relations" - ///| let mbt_maps : String = #| let occupations = { - #| "Malcolm": "Captain", - #| "Kaylee": "Mechanic", + #| "Malcolm": "Captain", + #| "Kaylee": "Mechanic", #| } #| occupations["Jayne"] = "Public Relations" @@ -200,5 +194,115 @@ let rs_struct_methods = #| } #| } +///| +let mbt_trait_objects = + #| trait Animal { + #| speak(Self) -> Unit + #| } + #| + #| type Duck String + #| fn Duck::make(name: String) -> Duck { Duck(name) } + #| fn speak(self: Duck) -> Unit { + #| println(self._ + ": quack!") + #| } + #| + #| type Fox String + #| fn Fox::make(name: String) -> Fox { Fox(name) } + #| fn Fox::speak(_self: Fox) -> Unit { + #| println("What does the fox say?") + #| } + #| + #| fn main { + #| let duck1 = Duck::make("duck1") + #| let duck2 = Duck::make("duck2") + #| let fox1 = Fox::make("fox1") + #| let animals = [ duck1 as Animal, duck2 as Animal, fox1 as Animal ] + #| let mut i = 0 + #| while i < animals.length() { + #| animals[i].speak() + #| i = i + 1 + #| } + #| } + +///| +let rs_trait_objects = + #| trait Animal { + #| fn speak(&self); + #| } + #| + #| struct Duck { + #| name: String, + #| } + #| impl Duck { + #| fn new(name: String) -> Duck { + #| Duck { name } + #| } + #| } + #| impl Animal for Duck { + #| fn speak(&self) { + #| println!("{}: quack!", self.name); + #| } + #| } + #| + #| struct Fox { + #| name: String, + #| } + #| impl Fox { + #| fn new(name: String) -> Fox { + #| Fox { name } + #| } + #| } + #| impl Animal for Fox { + #| fn speak(&self) { + #| println!("What does the fox say?"); + #| } + #| } + #| + #| fn main() { + #| let duck1 = Duck::new("duck1"); + #| let duck2 = Duck::new("duck2"); + #| let fox1 = Fox::new("fox1"); + #| let animals: Vec> = vec![ + #| Box::new(duck1), + #| Box::new(duck2), + #| Box::new(fox1), + #| ]; + #| for animal in animals { + #| animal.speak(); + #| } + #| } + +///| +let mbt_default_trait_impl = + #| trait I { + #| f(Self) -> Unit + #| f_twice(Self) -> Unit + #| } + #| + #| impl I with f_twice(self) { + #| self.f() + #| self.f() + #| } + +///| +let rs_default_trait_impl = + #| trait I { + #| fn f(&self); + #| fn f_twice(&self) { + #| self.f(); + #| self.f(); + #| } + #| } + +///| +let mbt_hashmap = + #| let map : Map[String, Int] = { "x": 1, "y": 2, "z": 3 } + +///| +let rs_hashmap = + #| let mut map = HashMap::new(); + #| map.insert("x", 1); + #| map.insert("y", 2); + // fn demo() { // } From 6f2117cd2c61d55af3a19995600b7274bd632b03 Mon Sep 17 00:00:00 2001 From: tiye Date: Sun, 8 Dec 2024 23:01:28 +0800 Subject: [PATCH 4/4] fix int type usage --- src/main/snippets.mbt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/snippets.mbt b/src/main/snippets.mbt index b355c2b..89e41ee 100644 --- a/src/main/snippets.mbt +++ b/src/main/snippets.mbt @@ -7,10 +7,10 @@ let mbt_variable : String = ///| let rs_variable : String = - #| let mut my_variable: usize = 42; + #| let mut my_variable: i32 = 42; #| my_variable = 50; #| - #| let my_constant: usize = 42: + #| let my_constant: i32 = 42; ///| let mbt_explict_type : String =