From cee1957a6b0b77d2eaaa775b669c787402cdec05 Mon Sep 17 00:00:00 2001 From: Bronley Plumb Date: Fri, 30 Jun 2023 10:05:11 -0400 Subject: [PATCH] Add baseline interface docs (#829) --- docs/interfaces.md | 94 +++++++++++++++++++++++++++++++++ scripts/compile-doc-examples.ts | 4 +- 2 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 docs/interfaces.md diff --git a/docs/interfaces.md b/docs/interfaces.md new file mode 100644 index 000000000..7465a3058 --- /dev/null +++ b/docs/interfaces.md @@ -0,0 +1,94 @@ +# Interfaces +Interfaces are a way to describe properties and methods on an object. + +## Simple example +Interfaces can have both properties and methods. + +```BrighterScript +interface Person + name as string + function getName() as string +end interface + +sub test(bob as Person) + print bob.name, bob.getName() +end sub +``` + +transpiles to + +```BrightScript +sub test(bob as object) + print bob.name, bob.getName() +end sub +``` + +## Use in functions +Interfaces can be used as function parameter types and return types. These types will be converted into `dynamic` when the project is transpiled. +```brighterscript +interface Dog + name as string + function walk() +end interface + +sub walkThePuppy(puppy as Dog) as Dog + puppy.walk() + return puppy +end sub +``` + +
+ View the transpiled BrightScript code + +```BrightScript +sub walkThePuppy(puppy as object) as object + puppy.walk() + return puppy +end sub +``` +
+ +## Namespaces +Interfaces can also be defined inside namespaces + +```BrighterScript +namespace humanoids + interface Person + name as string + function getName() as string + end interface +end namespace + +sub test(bob as humanoids.Person) + print bob.name, bob.getName() +end sub +``` + +transpiles to + +```BrightScript +sub test(bob as object) + print bob.name, bob.getName() +end sub +``` + +## Advanced types +Interface member types can be defined as any valid brighterscript type, and even reference themselves. +```brighterscript +interface Dog + owner as Human +end interface + +interface Human + pet as Dog + mother as Human +end interface +``` + +
+ View the transpiled BrightScript code + +```BrightScript + +``` +
\ No newline at end of file diff --git a/scripts/compile-doc-examples.ts b/scripts/compile-doc-examples.ts index 2c63995f4..5ec846721 100644 --- a/scripts/compile-doc-examples.ts +++ b/scripts/compile-doc-examples.ts @@ -70,7 +70,9 @@ class DocCompiler { while (this.nextLine !== undefined) { this.advance(); if (this.currentLine.includes('```')) { - await this.processCodeBlock(); + try { + await this.processCodeBlock(); + } catch { } } }