Browse Source

Add structural types post example

Getty Ritter 6 years ago
parent
commit
d56e06d45a

example/collage → examples/basic-example/collage


example/main.md → examples/basic-example/main.md


example/s1/.gitignore → examples/basic-example/s1/.gitignore


example/s1/Cargo.toml → examples/basic-example/s1/Cargo.toml


example/s1/src/main.rs → examples/basic-example/s1/src/main.rs


example/s2/Main.hs → examples/basic-example/s2/Main.hs


example/s2/s2.cabal → examples/basic-example/s2/s2.cabal


+ 29 - 0
examples/structural-types/collage

@@ -0,0 +1,29 @@
+(document "post.md"
+
+  { name ocaml
+    dir "ocaml-source"
+    cmd [
+      "rm -f main main.cmi main.cmo"
+      "ocamlc main.ml -o main"
+    ]
+    expose (sections "main.ml")
+  }
+
+  { name java
+    dir "java-source"
+    cmd [
+      "rm -f Main.class"
+      "javac Main.java"
+    ]
+    expose (sections "Main.java")
+  }
+
+  { name crystal
+    dir "crystal-source"
+    cmd [
+      "crystal build src/crystal-source.cr"
+    ]
+    expose (sections "src/crystal-source.cr")
+  }
+
+)

+ 1 - 0
examples/structural-types/crystal-source/.gitignore

@@ -0,0 +1 @@
+crystal-source

+ 17 - 0
examples/structural-types/crystal-source/src/crystal-source.cr

@@ -0,0 +1,17 @@
+# «program»
+class Cat
+  def speak() puts "meow" end
+end
+
+class Cow
+  def speak() puts "moo" end
+  def num_stomachs() 4 end
+end
+
+def hear_what_it_has_to_say(obj)
+  obj.speak
+end
+
+hear_what_it_has_to_say(Cat.new)
+hear_what_it_has_to_say(Cow.new)
+# «end»

+ 1 - 0
examples/structural-types/java-source/.gitignore

@@ -0,0 +1 @@
+*.class

+ 19 - 0
examples/structural-types/java-source/Main.java

@@ -0,0 +1,19 @@
+class Main {
+  public static void Main(String[] args) {
+    System.out.println("whoo");
+  }
+
+  // «nominal»
+class Cat {
+  void speak() {
+    System.out.println("meow");
+  }
+}
+
+class Dog {
+  void speak() {
+    System.out.println("woof");
+  }
+}
+  // «end»
+}

+ 3 - 0
examples/structural-types/ocaml-source/.gitignore

@@ -0,0 +1,3 @@
+main
+main.cmi
+main.cmo

+ 44 - 0
examples/structural-types/ocaml-source/main.ml

@@ -0,0 +1,44 @@
+let () = print_endline "Hello!";;
+
+(* «classes» *)
+class cat = object
+  method speak = print_endline "meow"
+end
+class dog = object
+  method speak = print_endline "woof"
+end
+(* «end» *)
+
+(* «uses» *)
+let hear_what_it_has_to_say obj =
+  let () = obj#speak in ()
+(* «end» *)
+;;
+
+(* «calling» *)
+let () = hear_what_it_has_to_say (new cat)
+(* prints "meow" *)
+let () = hear_what_it_has_to_say (new dog)
+(* prints "woof" *)
+(* «end» *)
+;;
+
+(* «bigger-object» *)
+class cow = object
+  method speak = print_endline "moo"
+  method num_stomachs = 4
+end
+(* «end» *)
+;;
+
+(* «call-cow» *)
+let () = hear_what_it_has_to_say (new cow)
+(* prints "moo" *)
+(* «end» *)
+;;
+
+(* «speaker» *)
+let ecce_orator obj =
+  let () = obj#speak in obj
+(* «end» *)
+;;

File diff suppressed because it is too large
+ 111 - 0
examples/structural-types/post.md