-
Notifications
You must be signed in to change notification settings - Fork 10.6k
Open
Labels
access controlFeature → modifiers: Access control and access levelsFeature → modifiers: Access control and access levelsbugA deviation from expected or documented behavior. Also: expected but undesirable behavior.A deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itselfThe Swift compiler itselffeatureA feature request or implementationA feature request or implementationgood first issueGood for newcomersGood for newcomersswift evolution proposal neededFlag → feature: A feature that warrants a Swift evolution proposalFlag → feature: A feature that warrants a Swift evolution proposal
Description
| Previous ID | SR-3316 |
| Radar | None |
| Original Reporter | @jckarter |
| Type | Bug |
Additional Detail from JIRA
| Votes | 0 |
| Component/s | Compiler |
| Labels | Bug, StarterProposal |
| Assignee | None |
| Priority | Medium |
md5: e092a51eda538e02ab484391d0b1b1a5
Issue Description:
Variables declared in a script default to internal visibility, like other global declarations, but this is problematic, because semantically they behave more like local variables. Another file can access the global before its initializer has even run:
// main.swift
class C {}
var x: C
foo()
x = C()// foo.swift
func foo() {
print(x) // loads garbage from `x` from main.swift
}In a future version of Swift, script globals should be changed to be private by default. We could possibly also allow a script global to be declared with explicit access control, which would make it visible to other files but would also have to make the variable behave more like a library global, with a dispatch_once-d initializer, e.g.:
// main.swift
var x = 42 // private, eagerly initialized
private var y = 17 // private, initialized on demand
internal var z = 38 // internal, initialized on demandMetadata
Metadata
Assignees
Labels
access controlFeature → modifiers: Access control and access levelsFeature → modifiers: Access control and access levelsbugA deviation from expected or documented behavior. Also: expected but undesirable behavior.A deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itselfThe Swift compiler itselffeatureA feature request or implementationA feature request or implementationgood first issueGood for newcomersGood for newcomersswift evolution proposal neededFlag → feature: A feature that warrants a Swift evolution proposalFlag → feature: A feature that warrants a Swift evolution proposal