You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
BrighterScript Intersection Types are a way to define a type that combines the members of multiple types. They are similar to Intersection Types found in other languages, such as [TypeScript](https://www.typescriptlang.org/docs/handbook/2/objects.html#intersection-types).
4
+
5
+
## Syntax
6
+
7
+
Intersection types can be declared with the following syntax: `<type> and <type>`. For example, the parameter to the function below could be meets both the interfaces `HasId` and `HasUrl`:
8
+
9
+
```BrighterScript
10
+
interface HasId
11
+
id as string
12
+
end interface
13
+
14
+
interface HasUrl
15
+
url as string
16
+
end interface
17
+
18
+
function getUrlWithQueryId(value as HasId and HasUrl) as string
19
+
return value.url + "?id=" + value.id
20
+
end function
21
+
```
22
+
23
+
Any number of inner types, including classes or interfaces, could be part of an intersection:
24
+
25
+
```BrighterScript
26
+
interface HasId
27
+
id as string
28
+
end interface
29
+
30
+
interface HasUrl
31
+
url as string
32
+
end interface
33
+
34
+
interface HasSize
35
+
width as integer
36
+
height as integer
37
+
end interface
38
+
39
+
40
+
function getUrlWithQuerySize(response as HasId and HasUrl and HasSize) as string
A diagnostic error will be raised when a member is accessed that is not a member of any of the types of a union. Note also that if a member is not the same type in each of the types in the union, it will itself be considered an intersection.
48
+
49
+
```BrighterScript
50
+
sub testIntersection(value as {id as string} and {id as integer})
51
+
' This is an error - "value.id" is of type "string AND integer"
52
+
printInteger(value.id)
53
+
end sub
54
+
55
+
sub printInteger(x as integer)
56
+
print x
57
+
end sub
58
+
```
59
+
60
+
## Transpilation
61
+
62
+
Since Brightscript does not have intersection types natively, intersection types will be transpiled as `dynamic`.
63
+
64
+
```BrighterScript
65
+
66
+
interface HasRadius
67
+
radius as float
68
+
end interface
69
+
70
+
interface Point
71
+
x as float
72
+
y as float
73
+
end interface
74
+
75
+
function getCircleDetails(circle as HasRadius and Point) as string
Copy file name to clipboardExpand all lines: docs/readme.md
+52-8Lines changed: 52 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,9 +1,11 @@
1
1
# BrighterScript
2
+
2
3
BrighterScript is a superset of Roku's BrightScript language. Its goal is to provide new functionality and enhanced syntax support to enhance the Roku channel developer experience.
3
4
4
5
See the following pages for more information:
5
6
6
7
## [Annotations](annotations.md)
8
+
7
9
```brighterscript
8
10
'mostly useful for plugins that change code based on annotations
9
11
@logOnException()
@@ -13,12 +15,14 @@ end
13
15
```
14
16
15
17
## [Callfunc Operator](callfunc-operator.md)
18
+
16
19
```brighterscript
17
20
'instead of `node.callfunc("someMethod", 1, 2, 3)`, you can do this:
18
21
node@.someMethod(1, 2, 3)
19
22
```
20
23
21
24
## [Classes](classes.md)
25
+
22
26
```brighterscript
23
27
class Movie
24
28
public title as string
@@ -33,6 +37,7 @@ end class
33
37
```
34
38
35
39
## [Constants](constants.md)
40
+
36
41
```brighterscript
37
42
const API_URL = "https://api.acme.com/v1/"
38
43
sub main()
@@ -41,6 +46,7 @@ end sub
41
46
```
42
47
43
48
## [Enums](enums.md)
49
+
44
50
```brighterscript
45
51
enum RemoteButton
46
52
up = "up"
@@ -51,6 +57,7 @@ end enum
51
57
```
52
58
53
59
## [Exceptions](exceptions.md)
60
+
54
61
```brighterscript
55
62
try
56
63
somethingDangerous()
@@ -59,7 +66,40 @@ catch 'look, no exception variable!
59
66
end try
60
67
```
61
68
69
+
## [Imports](imports.md)
70
+
71
+
```brighterscript
72
+
import "pkg:/source/util.bs"
73
+
sub main()
74
+
print util_toUpper("hello world")
75
+
end sub
76
+
```
77
+
78
+
## [Interfaces](interfaces.md)
79
+
80
+
```brighterscript
81
+
interface IMyComponent
82
+
top as roSGNodeMyComponent
83
+
84
+
isSelected as boolean
85
+
selectedIndex as integer
86
+
87
+
data as {id as string, isEpisode as boolean}
88
+
end interface
89
+
```
90
+
91
+
## [Intersection Types](intersection-types.md)
92
+
93
+
```brighterscript
94
+
type MyClassAA = MyClass and roAssociativeArray
95
+
96
+
sub addData(klass as MyClass and roAssociativeArray, data as roAssociativeArray)
0 commit comments