Initializing an empty dictionary


(Adriano Ferreira) #1

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!

        for element in self {
            dictionary[element] = dictionary[element]?.successor() ?? 1
        }

        return dictionary
    }
}

I’m using Swift 2.1 and Xcode 7.2.

Best,

— A


(Dmitri Gribenko) #2

Hi Adriano,

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

···

On Wed, Mar 9, 2016 at 8:45 AM, Adriano Ferreira via swift-users <swift-users@swift.org> wrote:

--
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@gmail.com>*/


(Adriano Ferreira) #3

Thanks Dmitri, will do as you suggested.

BTW, sorry for the typo... I actually intended "=" rather than “:", but luckily you got this :stuck_out_tongue:

Best,

— A

···

On Mar 9, 2016, at 12:48 PM, Dmitri Gribenko <gribozavr@gmail.com> wrote:

Hi Adriano,

On Wed, Mar 9, 2016 at 8:45 AM, Adriano Ferreira via swift-users > <swift-users@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@gmail.com>*/


(Chris Lattner) #4

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

-Chris

···

On Mar 9, 2016, at 9:48 AM, Dmitri Gribenko via swift-users <swift-users@swift.org> wrote:

Hi Adriano,

On Wed, Mar 9, 2016 at 8:45 AM, Adriano Ferreira via swift-users > <swift-users@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).


(James Campbell) #5

Hey Chris already solved this for 2.2, it will now give you a fix-it to use
[:]. So no need to file a bug :slight_smile:

···

*___________________________________*

*James⎥Head of Trolls*

*james@supmenow.com <james@supmenow.com>⎥supmenow.com <http://supmenow.com>*

*Sup*

*Runway East *

*10 Finsbury Square*

*London*

* EC2A 1AF *

On Wed, Mar 9, 2016 at 6:54 PM, Adriano Ferreira via swift-users < swift-users@swift.org> wrote:

Thanks Dmitri, will do as you suggested.

BTW, sorry for the typo... I actually intended "=" rather than “:", but
luckily you got this :stuck_out_tongue:

Best,

— A

> On Mar 9, 2016, at 12:48 PM, Dmitri Gribenko <gribozavr@gmail.com> > wrote:
>
> Hi Adriano,
>
> On Wed, Mar 9, 2016 at 8:45 AM, Adriano Ferreira via swift-users > > <swift-users@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@gmail.com>*/

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Dmitri Gribenko) #6

I was seeing buggy behavior on master. And, the non-shorthand syntax
should still work.

Dmitri

···

On Wed, Mar 9, 2016 at 11:00 AM, James Campbell <james@supmenow.com> wrote:

Hey Chris already solved this for 2.2, it will now give you a fix-it to use
[:]. So no need to file a bug :slight_smile:

--
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@gmail.com>*/