Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions docs/interfaces.md
Original file line number Diff line number Diff line change
@@ -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
```

<details>
<summary>View the transpiled BrightScript code</summary>

```BrightScript
sub walkThePuppy(puppy as object) as object
puppy.walk()
return puppy
end sub
```
</details>

## 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
```

<details>
<summary>View the transpiled BrightScript code</summary>

```BrightScript

```
</details>
4 changes: 3 additions & 1 deletion scripts/compile-doc-examples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ class DocCompiler {
while (this.nextLine !== undefined) {
this.advance();
if (this.currentLine.includes('```')) {
await this.processCodeBlock();
try {
await this.processCodeBlock();
} catch { }
}
}

Expand Down