-
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 master into v1 * Cleaned up a lot of code, refactored 'getTokenChain' to use AST - just 11 failures * Fixed some comments * More test fixes * Use RangeContainsRange for expressions lookups * Revert change to using AST for token chains * Investigating file to deep * Fixed final tests ... make sur eto link symbol tables on finding completeions * Lint fixes * Removed replaceAll(0 in tests * Removed standardizePath call in test * Updates from PR comments Co-authored-by: Mark Pearce <mark.pearce@redspace.com>
- Loading branch information
1 parent
7a0f95b
commit 30b3614
Showing
67 changed files
with
6,071 additions
and
2,295 deletions.
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
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
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
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,112 @@ | ||
# Constants | ||
You can use the `const` keyword to declare constant values. These values cannot be changed at runtime. Constant references will be inlined at compile-time, which is very efficient (similar to how [enums](enums.md) work). | ||
|
||
**NOTE:** These are not to be confused with the conditional compile `#const` statements, which are a totally different thing. | ||
|
||
## Basic usage | ||
Consts can be defined at the root level of files, and are scoped the same way as functions. | ||
```BrighterScript | ||
const GREETING_MESSAGE = "Hello world" | ||
sub main() | ||
print GREETING_MESSAGE | ||
end sub | ||
``` | ||
transpiles to | ||
```BrightScript | ||
sub main() | ||
print "Hello world" | ||
end sub | ||
``` | ||
|
||
## Namespaced Consts | ||
Consts can be defined within namespaces, and accessed like a property. | ||
|
||
```BrighterScript | ||
namespace networking | ||
const BASE_URL = "https://your-app.com/api/v1" | ||
sub printBaseUrl() | ||
'namespace-relative access is allowed | ||
print BASE_URL | ||
end sub | ||
end namespace | ||
sub main() | ||
print networking.BASE_URL | ||
end sub | ||
``` | ||
|
||
transpiles to | ||
|
||
```BrightScript | ||
sub networking_printBaseUrl() | ||
'namespace-relative access is allowed | ||
print "https://your-app.com/api/v1" | ||
end sub | ||
sub main() | ||
print "https://your-app.com/api/v1" | ||
end sub | ||
``` | ||
|
||
## Complex objects | ||
Constants can contain complex objects or arrays as well, but keep in mind that these are inlined at compile-time, so it is not recommended to reach into these objects for specific values all the time. | ||
|
||
non-primitive const values will be wrapped with parentheses when transpiled. | ||
|
||
```BrighterScript | ||
namespace networking | ||
const HEADERS = { | ||
"Authorization": "Basic", | ||
"Access-Control-Allow-Credentials": "true" | ||
} | ||
const METHODS = [ | ||
"GET", | ||
"POST", | ||
"PUT", | ||
"DELETE" | ||
] | ||
end namespace | ||
sub main() | ||
print networking.HEADERS | ||
print networking.METHODS | ||
end sub | ||
``` | ||
**Transpiled:** | ||
```brightscript | ||
sub main() | ||
print ({ | ||
"Authorization": "Basic" | ||
"Access-Control-Allow-Credentials": "true" | ||
}) | ||
print ([ | ||
"GET" | ||
"POST" | ||
"PUT" | ||
"DELETE" | ||
]) | ||
end sub | ||
``` | ||
|
||
## Referencing other constants | ||
Constants can reference other constants (as long as there is no circular reference). You can even use template strings! | ||
|
||
```brighterscript | ||
const BASE_URL = "https://api.domain.com" | ||
const LOGIN_PATH = "/login" | ||
const LOGIN_URL = BASE_URL + LOGIN_PATH | ||
const ERROR_URL = `${BASE_URL}/some/error` | ||
sub main() | ||
print LOGIN_URL | ||
print ERROR_URL | ||
end sub | ||
``` | ||
|
||
transpiles to | ||
|
||
```brightscript | ||
sub main() | ||
print ("https://api.domain.com" + "/login") | ||
print (bslib_toString("https://api.domain.com") + "/some/error") | ||
end sub | ||
``` |
Oops, something went wrong.