[swift-users] Initializing an empty dictionary

Chris Lattner clattner at apple.com
Wed Mar 9 22:53:07 CST 2016


> On Mar 9, 2016, at 9:48 AM, Dmitri Gribenko via swift-users <swift-users at swift.org> wrote:
> 
> Hi Adriano,
> 
> On Wed, Mar 9, 2016 at 8:45 AM, Adriano Ferreira via swift-users
> <swift-users at swift.org> wrote:
>> Hi everyone,
>> 
>> Does anyone know why Swift won’t let me use the alternative way below to
>> init the dictionary?
>> 
>> public extension SequenceType where Self.Generator.Element: Hashable {
>> 
>>    @warn_unused_result
>>    func frequencies() -> [Self.Generator.Element: Int] {
>>        var dictionary: [Self.Generator.Element: Int] = [:]
>>        // var dictionary: Dictionary<Self.Generator.Element, Int>() // <<<
>> Not allowed!
> 
> It produces a bad error message, unfortunately.  The issue is that you
> have a ':' instead of '='.
> 
> But, that also fails.  Only after I remove 'Self.' the compiler accepts it:
> 
> var dictionary = Dictionary<Generator.Element, Int>()
> 
> I would recommend you to file two issues on bugs.swift.org, the first
> one about a bad diagnostic for your code, and the second one about the
> compiler rejecting "var dictionary =
> Dictionary<Self.Generator.Element, Int>()" in the code above (when the
> equal sign is used).

This is a known (and commonly reported) issue, where “PreCheckExpression" isn’t forming a typeexpr for member lookups that produce a type.

-Chris


More information about the swift-users mailing list