Skip to content

Commit

Permalink
Merge branch 'master' into release-0.66.0
Browse files Browse the repository at this point in the history
  • Loading branch information
TwitchBronBron authored Jul 5, 2023
2 parents c2cdf6d + cee1957 commit 4a6e43c
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 1 deletion.
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

0 comments on commit 4a6e43c

Please sign in to comment.