Colon vs. equals


(Brent Royal-Gordon) #1

This is perhaps a bit nitpicky, but I've noticed that Swift sometimes uses colon to separate names and values, and sometimes uses equals. It's vaguely maddening.

What do I mean? Well, our language has this:

  myFunc(foo, bar: baz)

But it also has:

  @available(iOS, introduced=7.0, deprecated=8.0)

You create a dictionary like this:

  let dict = ["key": "value"]

But you set it like this:

  dict["key"] = "value"

Is there some principle here? The @available case seems particularly strange to me, because those values read strongly like parameters to me.

···

--
Brent Royal-Gordon
Architechies


(Charles Constant) #2

+0.9 for changing Dict to use "=" for assignment instead of ":"

It's hard to unreservedly support. It would affect an awful lot of code. It
would cause problems porting snippets from other languages. It will confuse
programmers who come to Swift from other languages. It would attract the
passionate and permanent wrath of a large portion of Swift programmers.
And, to my eye, "=" is less well suited, visually, than ":" as a separator,
if you're looking at a tree of data.

On the other hand, it's a lot Swift-er. It does seem odd, when you step
back, to see the ":" used differently in a Dict than in a function argument
or a ternary expression. If one were to get use to "=" being used
consistently, I suspect anything else would seem backward.

Consistency is important to me, and I would support the change.


(Andrew Bennett) #3

Interesting point, I guess ":" in dictionary literals is to make the syntax
unambiguous.

This is currently valid swift:

    let x = [a = b]

Its type is: Array<Void>. It seems like the syntax is clearer if the
delimiter isn't a valid operator.

Also there's a problem with default value syntax:

    func test(a: Int = 123)

This would be hard to read with only "=":

    func test(a = Int = 123)

Also if the default value was an operator:

    func test(a = Int -> Int = =)

Granted you could change the less commonly used default syntax, but it's
probably syntactically easier to differentiate user-controller delimiters
(assignment) and compiler-controller delimiters (:,). The example with
assignment in an array illustrates this.

I do agree '@available' though, it seems like it should be ":"

=)

···

On Sat, Feb 6, 2016 at 10:27 AM, Brent Royal-Gordon via swift-evolution < swift-evolution@swift.org> wrote:

This is perhaps a bit nitpicky, but I've noticed that Swift sometimes uses
colon to separate names and values, and sometimes uses equals. It's vaguely
maddening.

What do I mean? Well, our language has this:

        myFunc(foo, bar: baz)

But it also has:

        @available(iOS, introduced=7.0, deprecated=8.0)

You create a dictionary like this:

        let dict = ["key": "value"]

But you set it like this:

        dict["key"] = "value"

Is there some principle here? The @available case seems particularly
strange to me, because those values read strongly like parameters to me.

--
Brent Royal-Gordon
Architechies

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


(ilya) #4

I know it's a bit late to the party :slight_smile: but I have to disagree with the
original premise.
The colon in the dictionary doesn't really separate names and values, it
separates values and values:

["something" + "else" : 1 + 2]

is a perfectly valid dictionary that doesn't read nor write anything by
name. In other words, there are no lhs-expressions there and that's ok.

We use = to rebind the names or, more generally, call setters, and when we
do this an lhs-expression is required, well, on the left side of an
assignment operator. This is in no way related to what happens with the
dictionary.

The situation is different with function parameters. We don't really pass a
dictionary; if we did, we'd use something like myFunc("":foo, "bar": baz)
as a syntax.

One can attempt to read the syntax as binding names that the function can
use as parameters, but this doesn't really work either: we only know the
parameter's external names and this binding doesn't "leak" into the
original scope. So if we are thinking about function call as an assignment,
then it should be in an extremely short-lived scope contained within a
function call. It doesn't look like this view will bring us any benefits.

I think it's best to imagine the colon in function calls as simply a third
punctuation symbol. We do need to select a simple, readily available symbol
that cannot be easily mixed with colon-inside-the-dictionary and
regular-assignment-symbol, yet is still instantly recognisable. Reusing the
colon is a reasonable compromise, although => would also work (and I wish
it was given serious consideration).

Though I agree that it seems a bit excessive to have a separate syntax for
@available's parameters.

Ilya.

···

On Sat, Feb 6, 2016 at 12:27 AM, Brent Royal-Gordon via swift-evolution < swift-evolution@swift.org> wrote:

This is perhaps a bit nitpicky, but I've noticed that Swift sometimes uses
colon to separate names and values, and sometimes uses equals. It's vaguely
maddening.

What do I mean? Well, our language has this:

        myFunc(foo, bar: baz)

But it also has:

        @available(iOS, introduced=7.0, deprecated=8.0)

You create a dictionary like this:

        let dict = ["key": "value"]

But you set it like this:

        dict["key"] = "value"

Is there some principle here? The @available case seems particularly
strange to me, because those values read strongly like parameters to me.

--
Brent Royal-Gordon
Architechies

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


(Chris Lattner) #5

As others have pointed out, dictionary literal syntax is structurally different than “call like” syntax. That said, I tend to agree with you that using colon instead of equals in @available would make sense.

-Chris

···

On Feb 5, 2016, at 3:27 PM, Brent Royal-Gordon via swift-evolution <swift-evolution@swift.org> wrote:

This is perhaps a bit nitpicky, but I've noticed that Swift sometimes uses colon to separate names and values, and sometimes uses equals. It's vaguely maddening.

What do I mean? Well, our language has this:

  myFunc(foo, bar: baz)

But it also has:

  @available(iOS, introduced=7.0, deprecated=8.0)


(Radek Pietruszewski) #6

My interpretation:

1. “foo: bar” means “foo is bar”
2. “foo = bar” means “assign bar to foo”

So (2) should only be used for assignment to variables/constants, and when assigning default values to function parameters. (1) is more versatile and should be used in all other contexts.

IMHO:

@available(iOS, introduced=7.0, deprecated=8.0)

should be changed to:

   @available(iOS, introduced: 7.0, deprecated: 8.0)

But aside from that, I don’t have beef with the colon being used in different contexts — looks cleaner and nicer than ` = ` to my eyes.

— Radek

···

On 06 Feb 2016, at 00:27, Brent Royal-Gordon via swift-evolution <swift-evolution@swift.org> wrote:

This is perhaps a bit nitpicky, but I've noticed that Swift sometimes uses colon to separate names and values, and sometimes uses equals. It's vaguely maddening.

What do I mean? Well, our language has this:

  myFunc(foo, bar: baz)

But it also has:

  @available(iOS, introduced=7.0, deprecated=8.0)

You create a dictionary like this:

  let dict = ["key": "value"]

But you set it like this:

  dict["key"] = "value"

Is there some principle here? The @available case seems particularly strange to me, because those values read strongly like parameters to me.

--
Brent Royal-Gordon
Architechies

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


(Brent Royal-Gordon) #7

If ":" should be replaced by "=" than only at the call site to make it more consistent with variable declaration and initialization:

        myFunc(number = 4, string = 7)

        func myFunc(number: Int, string: String) {}

Although writing "=" instead of ":" in methods in 1.5 years Swift and over 20 years Objective-C would be a huge change.

To be clear, I like colon in parameter lists; I don't see passing a parameter as setting something.

I suppose mainly what I'd like is for attributes to change:

  @available(iOS, introduced: 7.0, deprecated: 8.0)

And *possibly* for dictionary literals to change:

  let dict = ["key" = "value"] // Also, the shorthand syntax would be [String = String]

Although dictionary literals seem like a relatively high-impact change compared to its value.

···

--
Brent Royal-Gordon
Architechies


#8

If ":" should be replaced by "=" than only at the call site to make it more consistent with variable declaration and initialization:

        myFunc(number = 4, string = 7)

        func myFunc(number: Int, string: String) {}

Although writing "=" instead of ":" in methods in 1.5 years Swift and over 20 years Objective-C would be a huge change.

- Maximilian

···

Am 06.02.2016 um 01:36 schrieb Andrew Bennett via swift-evolution <swift-evolution@swift.org>:

Interesting point, I guess ":" in dictionary literals is to make the syntax unambiguous.

This is currently valid swift:

    let x = [a = b]

Its type is: Array<Void>. It seems like the syntax is clearer if the delimiter isn't a valid operator.

Also there's a problem with default value syntax:

    func test(a: Int = 123)

This would be hard to read with only "=":

    func test(a = Int = 123)

Also if the default value was an operator:

    func test(a = Int -> Int = =)

Granted you could change the less commonly used default syntax, but it's probably syntactically easier to differentiate user-controller delimiters (assignment) and compiler-controller delimiters (:,). The example with assignment in an array illustrates this.

I do agree '@available' though, it seems like it should be ":"

=)

On Sat, Feb 6, 2016 at 10:27 AM, Brent Royal-Gordon via swift-evolution <swift-evolution@swift.org> wrote:
This is perhaps a bit nitpicky, but I've noticed that Swift sometimes uses colon to separate names and values, and sometimes uses equals. It's vaguely maddening.

What do I mean? Well, our language has this:

        myFunc(foo, bar: baz)

But it also has:

        @available(iOS, introduced=7.0, deprecated=8.0)

You create a dictionary like this:

        let dict = ["key": "value"]

But you set it like this:

        dict["key"] = "value"

Is there some principle here? The @available case seems particularly strange to me, because those values read strongly like parameters to me.

--
Brent Royal-Gordon
Architechies

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

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


#9

Inline

If ":" should be replaced by "=" than only at the call site to make it more consistent with variable declaration and initialization:

       myFunc(number = 4, string = 7)

       func myFunc(number: Int, string: String) {}

Although writing "=" instead of ":" in methods in 1.5 years Swift and over 20 years Objective-C would be a huge change.

To be clear, I like colon in parameter lists; I don't see passing a parameter as setting something.

I cannot say whether I like ":" in parameter lists or if I'm just used to them...

Though setting parameters doesn't seem wrong to me. In fact value types get copied to a new parameter which is declared in the function signature.

I suppose mainly what I'd like is for attributes to change:

   @available(iOS, introduced: 7.0, deprecated: 8.0)

And *possibly* for dictionary literals to change:

   let dict = ["key" = "value"] // Also, the shorthand syntax would be [String = String]

Although dictionary literals seem like a relatively high-impact change compared to its value.

Yeah that would be true and also ":" in dictionaries reads to me like a "map this value to another one". So it would rather be "=>" than "=" which reads like an assignment to the left "<=".

- Maximilian

···

Am 06.02.2016 um 02:07 schrieb Brent Royal-Gordon <brent@architechies.com>:

--
Brent Royal-Gordon
Architechies


(Dave Abrahams) #10

This should be spelled

     myDictionary.updateValues([“foo”: 1, “bar”: 2])

or maybe

     myDictionary.update([“foo”: 1, “bar”: 2])

···

on Sat Feb 06 2016, Haravikk <swift-evolution@swift.org> wrote:

+1 for changing attributes to using colons, as it would make them read
more like a function call (albeit one with slightly different rules).

-1 though for changing dictionary literals; personally I think the
colon syntax is a lot clearer, especially when the literal is being
assigned, as it means there is only a single, clear assignment.

Regarding assigning new values to a dictionary though, I wonder if we
might extend the literal syntax to modifying an existing dictionary?
For example, I might do something like:

  myDictionary[“foo”: 1, “bar”: 2] // Add/set new values for foo and bar

As this could be a neater way to replace:

  myDictionary[“foo”] = 1
  myDictionary[“bar”] = 2

--
-Dave


(Haravikk) #11

+1 for changing attributes to using colons, as it would make them read more like a function call (albeit one with slightly different rules).

-1 though for changing dictionary literals; personally I think the colon syntax is a lot clearer, especially when the literal is being assigned, as it means there is only a single, clear assignment.

Regarding assigning new values to a dictionary though, I wonder if we might extend the literal syntax to modifying an existing dictionary? For example, I might do something like:

  myDictionary[“foo”: 1, “bar”: 2] // Add/set new values for foo and bar

As this could be a neater way to replace:

  myDictionary[“foo”] = 1
  myDictionary[“bar”] = 2

Essentially we’d have a subscript for setting a single value, and one for setting a dictionary, without the need to do:

  myDictionary[[“foo”: 1, “bar”: 2]]

Since the second braces should be redundant I think. This would allow developers to avoid the use of assignments with dictionaries if they wish.

···

On 6 Feb 2016, at 01:22, Maximilian Hünenberger via swift-evolution <swift-evolution@swift.org> wrote:

Inline

Am 06.02.2016 um 02:07 schrieb Brent Royal-Gordon <brent@architechies.com>:

If ":" should be replaced by "=" than only at the call site to make it more consistent with variable declaration and initialization:

      myFunc(number = 4, string = 7)

      func myFunc(number: Int, string: String) {}

Although writing "=" instead of ":" in methods in 1.5 years Swift and over 20 years Objective-C would be a huge change.

To be clear, I like colon in parameter lists; I don't see passing a parameter as setting something.

I cannot say whether I like ":" in parameter lists or if I'm just used to them...

Though setting parameters doesn't seem wrong to me. In fact value types get copied to a new parameter which is declared in the function signature.

I suppose mainly what I'd like is for attributes to change:

  @available(iOS, introduced: 7.0, deprecated: 8.0)

And *possibly* for dictionary literals to change:

  let dict = ["key" = "value"] // Also, the shorthand syntax would be [String = String]

Although dictionary literals seem like a relatively high-impact change compared to its value.

Yeah that would be true and also ":" in dictionaries reads to me like a "map this value to another one". So it would rather be "=>" than "=" which reads like an assignment to the left "<=".

- Maximilian

--
Brent Royal-Gordon
Architechies

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


(Nate Cook) #12

There's a proposal for updating a dictionary in place using a dictionary literal, among other things: https://github.com/apple/swift-evolution/pull/125

Nate

···

On Feb 6, 2016, at 10:11 AM, Dave Abrahams via swift-evolution <swift-evolution@swift.org> wrote:

on Sat Feb 06 2016, Haravikk <swift-evolution@swift.org> wrote:

+1 for changing attributes to using colons, as it would make them read
more like a function call (albeit one with slightly different rules).

-1 though for changing dictionary literals; personally I think the
colon syntax is a lot clearer, especially when the literal is being
assigned, as it means there is only a single, clear assignment.

Regarding assigning new values to a dictionary though, I wonder if we
might extend the literal syntax to modifying an existing dictionary?
For example, I might do something like:

   myDictionary[“foo”: 1, “bar”: 2] // Add/set new values for foo and bar

As this could be a neater way to replace:

   myDictionary[“foo”] = 1
   myDictionary[“bar”] = 2

This should be spelled

    myDictionary.updateValues([“foo”: 1, “bar”: 2])

or maybe

    myDictionary.update([“foo”: 1, “bar”: 2])


(James Campbell) #13

In Ruby there is a special syntax for hash
[ "key" => "value"] which makes it less ambiguous compared to =

···

Sent from Outlook Mobile

On Fri, Feb 5, 2016 at 5:07 PM -0800, "Brent Royal-Gordon via swift-evolution" <swift-evolution@swift.org> wrote:

If ":" should be replaced by "=" than only at the call site to make it more consistent with variable declaration and initialization:

        myFunc(number = 4, string = 7)

        func myFunc(number: Int, string: String) {}

Although writing "=" instead of ":" in methods in 1.5 years Swift and over 20 years Objective-C would be a huge change.

To be clear, I like colon in parameter lists; I don't see passing a parameter as setting something.

I suppose mainly what I'd like is for attributes to change:

  @available(iOS, introduced: 7.0, deprecated: 8.0)

And *possibly* for dictionary literals to change:

  let dict = ["key" = "value"] // Also, the shorthand syntax would be [String = String]

Although dictionary literals seem like a relatively high-impact change compared to its value.

--
Brent Royal-Gordon
Architechies

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


(Tino) #14

Afaics, the equal sign is way more common to assign values to parameters — but as Objective-C and Swift have labels, colon fits better (some labels don't make good variable names)

I suppose mainly what I'd like is for attributes to change:

  @available(iOS, introduced: 7.0, deprecated: 8.0)

A name, parenthesis, names for parameters except the first… looks very much like a function call, so I agree, colon fits better.

And *possibly* for dictionary literals to change:

  let dict = ["key" = "value"] // Also, the shorthand syntax would be [String = String]

I think the equal sign should only be used for assignment (as it was decided to keep a C-tradition and not use it for comparison).
"Key" and "label" are very similar to me, so I prefer label syntax.

Tino


(Thorsten Seitz) #15

+1 for changing attributes to using colons, as it would make them read more like a function call (albeit one with slightly different rules).

-1 though for changing dictionary literals; personally I think the colon syntax is a lot clearer, especially when the literal is being assigned, as it means there is only a single, clear assignment.

I agree with Haravikk on both points.

-Thorsten

···

Am 06.02.2016 um 12:18 schrieb Haravikk via swift-evolution <swift-evolution@swift.org>:

Regarding assigning new values to a dictionary though, I wonder if we might extend the literal syntax to modifying an existing dictionary? For example, I might do something like:

   myDictionary[“foo”: 1, “bar”: 2] // Add/set new values for foo and bar

As this could be a neater way to replace:

   myDictionary[“foo”] = 1
   myDictionary[“bar”] = 2

Essentially we’d have a subscript for setting a single value, and one for setting a dictionary, without the need to do:

   myDictionary[[“foo”: 1, “bar”: 2]]

Since the second braces should be redundant I think. This would allow developers to avoid the use of assignments with dictionaries if they wish.

On 6 Feb 2016, at 01:22, Maximilian Hünenberger via swift-evolution <swift-evolution@swift.org> wrote:

Inline

Am 06.02.2016 um 02:07 schrieb Brent Royal-Gordon <brent@architechies.com>:

If ":" should be replaced by "=" than only at the call site to make it more consistent with variable declaration and initialization:

     myFunc(number = 4, string = 7)

     func myFunc(number: Int, string: String) {}

Although writing "=" instead of ":" in methods in 1.5 years Swift and over 20 years Objective-C would be a huge change.

To be clear, I like colon in parameter lists; I don't see passing a parameter as setting something.

I cannot say whether I like ":" in parameter lists or if I'm just used to them...

Though setting parameters doesn't seem wrong to me. In fact value types get copied to a new parameter which is declared in the function signature.

I suppose mainly what I'd like is for attributes to change:

@available(iOS, introduced: 7.0, deprecated: 8.0)

And *possibly* for dictionary literals to change:

let dict = ["key" = "value"] // Also, the shorthand syntax would be [String = String]

Although dictionary literals seem like a relatively high-impact change compared to its value.

Yeah that would be true and also ":" in dictionaries reads to me like a "map this value to another one". So it would rather be "=>" than "=" which reads like an assignment to the left "<=".

- Maximilian

--
Brent Royal-Gordon
Architechies

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

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


(David Sweeris) #16

Agreed.

- Dave Sweeris

···

On Feb 8, 2016, at 11:56, Radosław Pietruszewski via swift-evolution <swift-evolution@swift.org> wrote:

My interpretation:

1. “foo: bar” means “foo is bar”
2. “foo = bar” means “assign bar to foo”

So (2) should only be used for assignment to variables/constants, and when assigning default values to function parameters. (1) is more versatile and should be used in all other contexts.

IMHO:

@available(iOS, introduced=7.0, deprecated=8.0)

should be changed to:

@available(iOS, introduced: 7.0, deprecated: 8.0)

But aside from that, I don’t have beef with the colon being used in different contexts — looks cleaner and nicer than ` = ` to my eyes.

— Radek


(Brent Royal-Gordon) #17

In Ruby there is a special syntax for hash

[ "key" => "value"] which makes it less ambiguous compared to =

I'm familiar with this in Ruby (and Perl, where it originated), and I think that `:` is strictly superior to `=>`. (When Ruby 2 introduced `a: x` as a shorthand for `:a => x`, I jumped on it immediately and never looked back.) The *only* reason to change to `=` is to match dictionary assignment; `=>` doesn't do that.

···

--
Brent Royal-Gordon
Architechies


(Charles Constant) #18

let dict = ["key" = "value"]

I'm having buyer's remorse over supporting this. At first glance, it looks
like it's overloading an assignment operator... to a string literal. I know
that's not what's really going on under the hood, but on a surface level it
just looks very ugly :frowning:


(Charles Srstka) #19

If you think that's ugly, just imagine what it will look like to initialize an empty one.

let foo: [String = Int] = [=]

Charles

···

On Feb 6, 2016, at 6:26 PM, Charles Constant via swift-evolution <swift-evolution@swift.org> wrote:

let dict = ["key" = "value"]

I'm having buyer's remorse over supporting this. At first glance, it looks like it's overloading an assignment operator... to a string literal. I know that's not what's really going on under the hood, but on a surface level it just looks very ugly :frowning:


(Craig Cruden) #20

I would prefer a “map” symbol of some sort (Scala uses -> obviously that is Swifts return)….

Between : or =…. I would stick with :.

The key is not equal to the value it only maps to it.

···

On 2016-02-07, at 0:38:25, James Campbell via swift-evolution <swift-evolution@swift.org> wrote:

In Ruby there is a special syntax for hash

[ "key" => "value"] which makes it less ambiguous compared to =

Sent from Outlook Mobile <https://aka.ms/qtex0l>

On Fri, Feb 5, 2016 at 5:07 PM -0800, "Brent Royal-Gordon via swift-evolution" <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:

> If ":" should be replaced by "=" than only at the call site to make it more consistent with variable declaration and initialization:
>
> myFunc(number = 4, string = 7)
>
> func myFunc(number: Int, string: String) {}
>
> Although writing "=" instead of ":" in methods in 1.5 years Swift and over 20 years Objective-C would be a huge change.

To be clear, I like colon in parameter lists; I don't see passing a parameter as setting something.

I suppose mainly what I'd like is for attributes to change:

  @available(iOS, introduced: 7.0, deprecated: 8.0)

And *possibly* for dictionary literals to change:

  let dict = ["key" = "value"] // Also, the shorthand syntax would be [String = String]

Although dictionary literals seem like a relatively high-impact change compared to its value.

--
Brent Royal-Gordon
Architechies

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