From 9438f50397736d3bc9738176a307162fd64d154b Mon Sep 17 00:00:00 2001 From: Nino Annighoefer Date: Sat, 9 Sep 2023 17:24:24 +0100 Subject: [PATCH 1/3] Add section on JavaScript interop for custom types For #200. --- book-src/tour/custom-types.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/book-src/tour/custom-types.md b/book-src/tour/custom-types.md index 5a03a13a..024e30ee 100644 --- a/book-src/tour/custom-types.md +++ b/book-src/tour/custom-types.md @@ -250,3 +250,23 @@ LoggedIn("Kim") guest, {logged_in, <<"Kim">>}. ``` + +## JavaScript interop + +When compiling to JavaScript, each constructor becomes a class of the same name. +Each of the generated JavaScript classes has a constructor with the same +arguments as the original type constructor. + +```gleam +// Gleam +Guest +LoggedIn("Kim") +Cat(name: "Jinx", cuteness: 2002) +``` + +```javascript +// JavaScript +new Guest(); // => {} +new LoggedIn("Kim"); // => { 0: "Kim" } +new Animal("Jinx", 2002); // => { name: "Jinx", cuteness: 2002 } +``` From 35e63050b57576df014a7c0b62ff077925eb8c04 Mon Sep 17 00:00:00 2001 From: Nino Annighoefer Date: Sat, 9 Sep 2023 18:05:28 +0100 Subject: [PATCH 2/3] Remove comments and add import statement --- book-src/tour/custom-types.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/book-src/tour/custom-types.md b/book-src/tour/custom-types.md index 024e30ee..185af136 100644 --- a/book-src/tour/custom-types.md +++ b/book-src/tour/custom-types.md @@ -266,7 +266,9 @@ Cat(name: "Jinx", cuteness: 2002) ```javascript // JavaScript -new Guest(); // => {} -new LoggedIn("Kim"); // => { 0: "Kim" } -new Animal("Jinx", 2002); // => { name: "Jinx", cuteness: 2002 } +import { Guest, LoggedIn, Animal } from "my_module.mjs"; + +new Guest(); +new LoggedIn("Kim"); +new Animal("Jinx", 2002); ``` From e872674d031c6f718922738b1d8f597c46b89a25 Mon Sep 17 00:00:00 2001 From: Nino Annighoefer Date: Mon, 11 Sep 2023 09:29:02 +0100 Subject: [PATCH 3/3] Fix mistake whoops! --- book-src/tour/custom-types.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/book-src/tour/custom-types.md b/book-src/tour/custom-types.md index 185af136..20a96798 100644 --- a/book-src/tour/custom-types.md +++ b/book-src/tour/custom-types.md @@ -266,9 +266,9 @@ Cat(name: "Jinx", cuteness: 2002) ```javascript // JavaScript -import { Guest, LoggedIn, Animal } from "my_module.mjs"; +import { Guest, LoggedIn, Cat } from "my_module.mjs"; new Guest(); new LoggedIn("Kim"); -new Animal("Jinx", 2002); +new Cat("Jinx", 2002); ```