-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into release-0.66.0
- Loading branch information
Showing
2 changed files
with
97 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters