[swift-users] Initializing an empty dictionary

Dmitri Gribenko gribozavr at gmail.com
Wed Mar 9 11:48:08 CST 2016


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).

Dmitri

-- 
main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if
(j){printf("%d\n",i);}}} /*Dmitri Gribenko <gribozavr at gmail.com>*/


More information about the swift-users mailing list