Be able to initialise empty dict with array constrcutor

See this code:

var distanceCache: [Int: Int] = Dictionary<Int, Int>()
It is very long and tedious to write especially if what I am storing
changes.

I propose we be allowed to do the following:

*var distanceCache: [Int: Int] = *

If this isn't possible then I wouldn't mind having some way of telling the
compiler to auto create it like so:

*var distanceCache: [Int: Int] ()*

or

*var distanceCache: [Int: Int] = new Dictionary*

or even:

*var distanceCache: [Int: Int] = auto*

*auto var distanceCache: [Int: Int]*

(auto short for auto create)

Perhaps this dictionary syntax is just confusing and it was a bad idea to
make it the same as an array. Most languages use "{" so why did swift
choose to share "[" with arrays and dictionaries.

···

--
 Wizard
james@supmenow.com
+44 7523 279 698

Perhaps instead of "auto" we could allow "lazy" to create a default lazy
constructor for these cases ?

···

On Tue, Jan 5, 2016 at 12:39 PM, James Campbell <james@supmenow.com> wrote:

See this code:

var distanceCache: [Int: Int] = Dictionary<Int, Int>()
It is very long and tedious to write especially if what I am storing
changes.

I propose we be allowed to do the following:

*var distanceCache: [Int: Int] = *

If this isn't possible then I wouldn't mind having some way of telling the
compiler to auto create it like so:

*var distanceCache: [Int: Int] ()*

or

*var distanceCache: [Int: Int] = new Dictionary*

or even:

*var distanceCache: [Int: Int] = auto*

*auto var distanceCache: [Int: Int]*

(auto short for auto create)

Perhaps this dictionary syntax is just confusing and it was a bad idea to
make it the same as an array. Most languages use "{" so why did swift
choose to share "[" with arrays and dictionaries.
--
 Wizard
james@supmenow.com
+44 7523 279 698

--
 Wizard
james@supmenow.com
+44 7523 279 698

I don’t understand what the problem is

See this code:
var distanceCache: [Int: Int] = Dictionary<Int, Int>()

It is very long and tedious to write especially if what I am storing changes.

I propose we be allowed to do the following:
var distanceCache: [Int: Int] =

You can do

var distanceCache: [Int: Int] = [:]

Also

var distanceCache2 = [Int: Int]()

Perhaps this dictionary syntax is just confusing and it was a bad idea to make it the same as an array. Most languages use "{" so why did swift choose to share "[" with arrays and dictionaries.

It’s not the same, you need the colons inside. I imagine that braces were discarded on the grounds that it would confuse the compiler with respect to closures, for example

var myClosure = {} // is a variable of type () -> ()

···

On 5 Jan 2016, at 12:39, James Campbell via swift-evolution <swift-evolution@swift.org> wrote:

--
 Wizard
james@supmenow.com
+44 7523 279 698
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

The problem for me is that is so counter intuitive I didn't even know you
could do that.

···

On Tue, Jan 5, 2016 at 12:50 PM, Jeremy Pereira < jeremy.j.pereira@googlemail.com> wrote:

I don’t understand what the problem is

> On 5 Jan 2016, at 12:39, James Campbell via swift-evolution < > swift-evolution@swift.org> wrote:
>
> See this code:
> var distanceCache: [Int: Int] = Dictionary<Int, Int>()
>
> It is very long and tedious to write especially if what I am storing
changes.
>
> I propose we be allowed to do the following:
> var distanceCache: [Int: Int] =

You can do

var distanceCache: [Int: Int] = [:]

Also

var distanceCache2 = [Int: Int]()

> Perhaps this dictionary syntax is just confusing and it was a bad idea
to make it the same as an array. Most languages use "{" so why did swift
choose to share "[" with arrays and dictionaries.

It’s not the same, you need the colons inside. I imagine that braces were
discarded on the grounds that it would confuse the compiler with respect to
closures, for example

var myClosure = {} // is a variable of type () -> ()

>
> --
>  Wizard
> james@supmenow.com
> +44 7523 279 698
> _______________________________________________
> swift-evolution mailing list
> swift-evolution@swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

--
 Wizard
james@supmenow.com
+44 7523 279 698

You’re completely right, but we don’t need to change the swift language to fix that. As of 3f19714, which I just pushed, we now emit this error message (which includes a fixit hint to insert the colon):

t.swift:2:33: error: use [:] to get an empty dictionary literal
var distanceCache: [Int: Int] =
                                ^
                                 :

instead of:

t.swift:2:33: error: contextual type '[Int : Int]' cannot be used with array literal
var distanceCache: [Int: Int] =
                                ^~

That should address the problem, thanks for pointing this out!

-Chris

···

On Jan 5, 2016, at 4:51 AM, James Campbell via swift-evolution <swift-evolution@swift.org> wrote:

The problem for me is that is so counter intuitive I didn't even know you could do that.

On Tue, Jan 5, 2016 at 12:50 PM, Jeremy Pereira <jeremy.j.pereira@googlemail.com <mailto:jeremy.j.pereira@googlemail.com>> wrote:
I don’t understand what the problem is

> On 5 Jan 2016, at 12:39, James Campbell via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:
>
> See this code:
> var distanceCache: [Int: Int] = Dictionary<Int, Int>()
>
> It is very long and tedious to write especially if what I am storing changes.
>
> I propose we be allowed to do the following:
> var distanceCache: [Int: Int] =

You can do

var distanceCache: [Int: Int] = [:]

Also

var distanceCache2 = [Int: Int]()

> Perhaps this dictionary syntax is just confusing and it was a bad idea to make it the same as an array. Most languages use "{" so why did swift choose to share "[" with arrays and dictionaries.

It’s not the same, you need the colons inside. I imagine that braces were discarded on the grounds that it would confuse the compiler with respect to closures, for example

var myClosure = {} // is a variable of type () -> ()

>
> --
>  Wizard
> james@supmenow.com <mailto:james@supmenow.com>
> +44 7523 279 698 <tel:%2B44%207523%20279%20698>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution@swift.org <mailto:swift-evolution@swift.org>
> https://lists.swift.org/mailman/listinfo/swift-evolution

--
 Wizard
james@supmenow.com <mailto:james@supmenow.com>
+44 7523 279 698
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

The problem for me is that is so counter intuitive I didn't even know you could do that.

The first one is a bit counter intuitive and I agree that

var distanceCache: [Int: Int] =

might be an improvement, albeit not one I think many people would agree is worth doing. However, the second one is a natural extrapolation from the equivalent array syntax i.e.

var array = [Int]()
var dictionary = [String: Int]()

···

On 5 Jan 2016, at 12:51, James Campbell <james@supmenow.com> wrote:

On Tue, Jan 5, 2016 at 12:50 PM, Jeremy Pereira <jeremy.j.pereira@googlemail.com> wrote:
I don’t understand what the problem is

> On 5 Jan 2016, at 12:39, James Campbell via swift-evolution <swift-evolution@swift.org> wrote:
>
> See this code:
> var distanceCache: [Int: Int] = Dictionary<Int, Int>()
>
> It is very long and tedious to write especially if what I am storing changes.
>
> I propose we be allowed to do the following:
> var distanceCache: [Int: Int] =

You can do

var distanceCache: [Int: Int] = [:]

Also

var distanceCache2 = [Int: Int]()

> Perhaps this dictionary syntax is just confusing and it was a bad idea to make it the same as an array. Most languages use "{" so why did swift choose to share "[" with arrays and dictionaries.

It’s not the same, you need the colons inside. I imagine that braces were discarded on the grounds that it would confuse the compiler with respect to closures, for example

var myClosure = {} // is a variable of type () -> ()

>
> --
>  Wizard
> james@supmenow.com
> +44 7523 279 698
> _______________________________________________
> swift-evolution mailing list
> swift-evolution@swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

--
 Wizard
james@supmenow.com
+44 7523 279 698

Thats a small but huge improvement :)

···

On Tue, Jan 5, 2016 at 6:58 PM, Chris Lattner <clattner@apple.com> wrote:

You’re completely right, but we don’t need to change the swift language to
fix that. As of 3f19714, which I just pushed, we now emit this error
message (which includes a fixit hint to insert the colon):

t.swift:2:33: error: use [:] to get an empty dictionary literal
var distanceCache: [Int: Int] =
                                ^
                                 :

instead of:

t.swift:2:33: error: contextual type '[Int : Int]' cannot be used with
array literal
var distanceCache: [Int: Int] =
                                ^~

That should address the problem, thanks for pointing this out!

-Chris

On Jan 5, 2016, at 4:51 AM, James Campbell via swift-evolution < > swift-evolution@swift.org> wrote:

The problem for me is that is so counter intuitive I didn't even know you
could do that.

On Tue, Jan 5, 2016 at 12:50 PM, Jeremy Pereira < > jeremy.j.pereira@googlemail.com> wrote:

I don’t understand what the problem is

> On 5 Jan 2016, at 12:39, James Campbell via swift-evolution < >> swift-evolution@swift.org> wrote:
>
> See this code:
> var distanceCache: [Int: Int] = Dictionary<Int, Int>()
>
> It is very long and tedious to write especially if what I am storing
changes.
>
> I propose we be allowed to do the following:
> var distanceCache: [Int: Int] =

You can do

var distanceCache: [Int: Int] = [:]

Also

var distanceCache2 = [Int: Int]()

> Perhaps this dictionary syntax is just confusing and it was a bad idea
to make it the same as an array. Most languages use "{" so why did swift
choose to share "[" with arrays and dictionaries.

It’s not the same, you need the colons inside. I imagine that braces were
discarded on the grounds that it would confuse the compiler with respect to
closures, for example

var myClosure = {} // is a variable of type () -> ()

>
> --
>  Wizard
> james@supmenow.com
> +44 7523 279 698
> _______________________________________________
> swift-evolution mailing list
> swift-evolution@swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

--
 Wizard
james@supmenow.com
+44 7523 279 698
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

--
 Wizard
james@supmenow.com
+44 7523 279 698

So the [Int]() is shorthand for Array<Int>()

···

On Tue, Jan 5, 2016 at 1:01 PM, Jeremy Pereira < jeremy.j.pereira@googlemail.com> wrote:

> On 5 Jan 2016, at 12:51, James Campbell <james@supmenow.com> wrote:
>
> The problem for me is that is so counter intuitive I didn't even know
you could do that.

The first one is a bit counter intuitive and I agree that

var distanceCache: [Int: Int] =

might be an improvement, albeit not one I think many people would agree is
worth doing. However, the second one is a natural extrapolation from the
equivalent array syntax i.e.

var array = [Int]()
var dictionary = [String: Int]()

>
> On Tue, Jan 5, 2016 at 12:50 PM, Jeremy Pereira < > jeremy.j.pereira@googlemail.com> wrote:
> I don’t understand what the problem is
>
> > On 5 Jan 2016, at 12:39, James Campbell via swift-evolution < > swift-evolution@swift.org> wrote:
> >
> > See this code:
> > var distanceCache: [Int: Int] = Dictionary<Int, Int>()
> >
> > It is very long and tedious to write especially if what I am storing
changes.
> >
> > I propose we be allowed to do the following:
> > var distanceCache: [Int: Int] =
>
> You can do
>
> var distanceCache: [Int: Int] = [:]
>
> Also
>
> var distanceCache2 = [Int: Int]()
>
>
> > Perhaps this dictionary syntax is just confusing and it was a bad idea
to make it the same as an array. Most languages use "{" so why did swift
choose to share "[" with arrays and dictionaries.
>
> It’s not the same, you need the colons inside. I imagine that braces
were discarded on the grounds that it would confuse the compiler with
respect to closures, for example
>
> var myClosure = {} // is a variable of type () -> ()
>
>
> >
> > --
> >  Wizard
> > james@supmenow.com
> > +44 7523 279 698
> > _______________________________________________
> > swift-evolution mailing list
> > swift-evolution@swift.org
> > https://lists.swift.org/mailman/listinfo/swift-evolution
>
>
>
>
> --
>  Wizard
> james@supmenow.com
> +44 7523 279 698

--
 Wizard
james@supmenow.com
+44 7523 279 698

So the [Int]() is shorthand for Array<Int>()

Yes. In general, you can write [T] anywhere you can write Array<T> and [U : V] anywhere you can write Dictionary<U, V>. The are both syntactic sugar.

···

On 5 Jan 2016, at 13:42, James Campbell <james@supmenow.com> wrote:

On Tue, Jan 5, 2016 at 1:01 PM, Jeremy Pereira <jeremy.j.pereira@googlemail.com> wrote:

> On 5 Jan 2016, at 12:51, James Campbell <james@supmenow.com> wrote:
>
> The problem for me is that is so counter intuitive I didn't even know you could do that.

The first one is a bit counter intuitive and I agree that

var distanceCache: [Int: Int] =

might be an improvement, albeit not one I think many people would agree is worth doing. However, the second one is a natural extrapolation from the equivalent array syntax i.e.

var array = [Int]()
var dictionary = [String: Int]()

>
> On Tue, Jan 5, 2016 at 12:50 PM, Jeremy Pereira <jeremy.j.pereira@googlemail.com> wrote:
> I don’t understand what the problem is
>
> > On 5 Jan 2016, at 12:39, James Campbell via swift-evolution <swift-evolution@swift.org> wrote:
> >
> > See this code:
> > var distanceCache: [Int: Int] = Dictionary<Int, Int>()
> >
> > It is very long and tedious to write especially if what I am storing changes.
> >
> > I propose we be allowed to do the following:
> > var distanceCache: [Int: Int] =
>
> You can do
>
> var distanceCache: [Int: Int] = [:]
>
> Also
>
> var distanceCache2 = [Int: Int]()
>
>
> > Perhaps this dictionary syntax is just confusing and it was a bad idea to make it the same as an array. Most languages use "{" so why did swift choose to share "[" with arrays and dictionaries.
>
> It’s not the same, you need the colons inside. I imagine that braces were discarded on the grounds that it would confuse the compiler with respect to closures, for example
>
> var myClosure = {} // is a variable of type () -> ()
>
>
> >
> > --
> >  Wizard
> > james@supmenow.com
> > +44 7523 279 698
> > _______________________________________________
> > swift-evolution mailing list
> > swift-evolution@swift.org
> > https://lists.swift.org/mailman/listinfo/swift-evolution
>
>
>
>
> --
>  Wizard
> james@supmenow.com
> +44 7523 279 698

--
 Wizard
james@supmenow.com
+44 7523 279 698

Yes. All of these variants are covered in the book The Swift Programming Language in some detail.

Alex

–––––
Alexander Kempgen
alex@kempgen.de

···

Am 05.01.2016 um 14:42 schrieb James Campbell via swift-evolution <swift-evolution@swift.org>:

So the [Int]() is shorthand for Array<Int>()

On Tue, Jan 5, 2016 at 1:01 PM, Jeremy Pereira <jeremy.j.pereira@googlemail.com <mailto:jeremy.j.pereira@googlemail.com>> wrote:

> On 5 Jan 2016, at 12:51, James Campbell <james@supmenow.com <mailto:james@supmenow.com>> wrote:
>
> The problem for me is that is so counter intuitive I didn't even know you could do that.

The first one is a bit counter intuitive and I agree that

var distanceCache: [Int: Int] =

might be an improvement, albeit not one I think many people would agree is worth doing. However, the second one is a natural extrapolation from the equivalent array syntax i.e.

var array = [Int]()
var dictionary = [String: Int]()

>
> On Tue, Jan 5, 2016 at 12:50 PM, Jeremy Pereira <jeremy.j.pereira@googlemail.com <mailto:jeremy.j.pereira@googlemail.com>> wrote:
> I don’t understand what the problem is
>
> > On 5 Jan 2016, at 12:39, James Campbell via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:
> >
> > See this code:
> > var distanceCache: [Int: Int] = Dictionary<Int, Int>()
> >
> > It is very long and tedious to write especially if what I am storing changes.
> >
> > I propose we be allowed to do the following:
> > var distanceCache: [Int: Int] =
>
> You can do
>
> var distanceCache: [Int: Int] = [:]
>
> Also
>
> var distanceCache2 = [Int: Int]()
>
>
> > Perhaps this dictionary syntax is just confusing and it was a bad idea to make it the same as an array. Most languages use "{" so why did swift choose to share "[" with arrays and dictionaries.
>
> It’s not the same, you need the colons inside. I imagine that braces were discarded on the grounds that it would confuse the compiler with respect to closures, for example
>
> var myClosure = {} // is a variable of type () -> ()
>
>
> >
> > --
> >  Wizard
> > james@supmenow.com <mailto:james@supmenow.com>
> > +44 7523 279 698 <tel:%2B44%207523%20279%20698>
> > _______________________________________________
> > swift-evolution mailing list
> > swift-evolution@swift.org <mailto:swift-evolution@swift.org>
> > https://lists.swift.org/mailman/listinfo/swift-evolution
>
>
>
>
> --
>  Wizard
> james@supmenow.com <mailto:james@supmenow.com>
> +44 7523 279 698 <tel:%2B44%207523%20279%20698>

--
 Wizard
james@supmenow.com <mailto:james@supmenow.com>
+44 7523 279 698
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution