Open
Description
Previous ID | SR-354 |
Radar | None |
Original Reporter | ianterrell (JIRA User) |
Type | Bug |
Additional Detail from JIRA
Votes | 1 |
Component/s | Compiler |
Labels | Bug |
Assignee | None |
Priority | Medium |
md5: 5b5256f99ab266dee19af877b39508b3
relates to:
Issue Description:
Given the following class:
class Foo {
var bar: (String?, String?)
}
I expect it not to compile, considering bar
is a non-optional type without a default value. Instead, it compiles and the synthesized initializer sets bar
to (nil, nil)
:
let f = Foo()
f.bar
// => (nil, nil)
If the tuple is instead a constant, it does not compile:
class Foo {
let bar: (String?, String?)
}
This results in:
tuple.swift:1:7: error: class 'Foo' has no initializers
class Foo {
^
Further, the messaging is not consistent, as non-tuple types communicate more information in the compiler error. With a string:
class Foo {
let baz: String
}
the following additional error is communicated:
tuple.swift:2:9: note: stored property 'baz' without initial value prevents synthesized initializers
let baz: String
^
= ""
While that is the stored property usage, similar behavior results in normal variable usage.
var bar: (String?, String?)
print(bar)
// => (nil, nil)
But with a constant:
let bar: (String?, String?)
print(bar)
It does not compile:
tuple.swift:11:7: error: constant 'bar.0' used before being initialized
print(bar)
^
If this behavior is expected, I do not see it documented in the language guide.
Metadata
Metadata
Assignees
Labels
Area → standard library: The `Optional` typeA deviation from expected or documented behavior. Also: expected but undesirable behavior.The Swift compiler itselfFeature: Default initialization of variablesFlag → feature: A feature that warrants a Swift evolution proposalFeature: tuplesArea → compiler: Semantic analysisFeature: typesBug: Unexpected behavior or incorrect output