[swift-evolution] an observation on colons in variable type declarations

Jean-Denis Muys jdmuys at gmail.com
Wed Feb 10 08:20:55 CST 2016


I read the colon a bit like “corresponds to”, which (with some stretch) fits how it is used.

> Defining: `func max(x1 x1: Int, x2: Int = 0): Int` instead of `func max(x1 x1: Int, x2: Int = 0) -> Int`
I prefer the arrow, that really suggests something is returned, to be handled later. I would prefer a real arrow though, in these Unicode times, rather than “dash”-“greater than” (which could be accepted as a digraph maybe)
> Using: `let positive = max(x1 = -5)` instead of `let positive = max(x1: -5)`
Here, “=“ meaning assignment works very well, better than the colon probably

> Assigning: `var 0 -> x; 1 +-> x` instead of `var x = 0; x += 1`
This is totally wrong to me. It is at complete odd with “let a = 5”. Assignment is written with receiver on the left, in Swift as in the vast majority of languages. Reversing that is gratuitous and very difficult to read. The second case, with the strange “stopped arrow” +-> which looks very much like |-> i even worse.

> Entries: `let dict = ["A" = 1, "B" = 2]` instead of `let dict = ["A" : 1, "B" : 2]`
So you mean you assign the integer 2 to the string “A”? Let me die in horror.

Standard math notation uses the equal sign for both assignment and comparison (it can also be argued that in maths, there is no assignment, only comparison, and the phrase “let x=3” actually means “let’s consider the case where x=3”).
Pascal used to replace = with := out of fear of confusing = with comparison. C decided to alter the comparison rather than the assignment.

Choosing yet another meaning for “=“, writing “String” = 1 is really an anathema to my eyes.

Jean-Denis




> On 10 Feb 2016, at 01:28, Howard Lovatt via swift-evolution <swift-evolution at swift.org> wrote:
> 
> I also find it odd that -> is used for return types instead of a :. My favourite would be for = to be used for assigning to argument labels, initialization of lets, and dictionary entries, : used for types, and -> used for assignment to vars. IE:
> Defining: `func max(x1 x1: Int, x2: Int = 0): Int` instead of `func max(x1 x1: Int, x2: Int = 0) -> Int`
> Using: `let positive = max(x1 = -5)` instead of `let positive = max(x1: -5)`
> Assigning: `var 0 -> x; 1 +-> x` instead of `var x = 0; x += 1`
> Entries: `let dict = ["A" = 1, "B" = 2]` instead of `let dict = ["A" : 1, "B" : 2]`
> However the current syntax, whilst inconsistent, is OK and therefore not worth changing.
> 
>   -- Howard.
> 
> On 10 February 2016 at 09:45, Oliver M via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
> A brief observation regarding variable type declarations
> 
> I find the colon in variable type declarations a little odd.
> Consider the following:
> 
> var welcomeMessage: String
> 
> wouldn't this be cleaner simply written as
> 
> var welcomeMessage String
> 
> 
> the documentation says to read the colon ':' as "of type" in such situations
> 
> but does the type "String" itself not imply 'of type String'
> 
> 
> 
> Consider the colon in a function declaration:
> 
> func sayHello(personName: String) {}
> 
> I would read the portion within the brackets as:
> parameter named personName takes value of type String (because you input values when calling functions)
> with the colon reading 'takes value' and "String" again reading 'of type String'
> 
> 
> 
> I find the colon ':' strongly associated with function input values, and with associations [key:value pairs etc]
> but variables…you don't input anything and they are not inherently associations.
> 
> 
> When coding half asleep I find myself compelled to input a value wherever I see a ':' and I can see myself typing something really really silly
> 
> var myIndex: "Int" 
> myIndex = 0
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
> 
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160210/8d849c7b/attachment.html>


More information about the swift-evolution mailing list