an observation on colons in variable type declarations


(Myles La Verne Schultz) #1

English has a number of rules. Ones which people, unfortunately break daily which results in often difficult to read text. A colon in code is just like a colon in English—punctuation. Punctuation helps us to read with understanding. As I had stated previously, it’s like placing quotation marks around a quotation. Without them, you may be able to identify the quote; however, with them, you can identify quotes with a simple glance. You don’t have to read for context. Thus punctuation provides readability. Readability is a part of the foundation of simple, easy to use and understand code.

Your idea of ideal is rather unrealistic. You find the iPhone to require “no instruction manual”; however, knowing people myself who don’t readily use computers and thus do not have the intuitive approach to electronics find the iPhone and any other electronic device to be a mystery. No one “just knows”. You have to have learned and experienced electronics to know how they to do x, y, z tasks. So, yes, there is a Swift Language Guide for good reason. Swift’s use of punctation, it’s syntax, it’s way of approaching code is just slightly different from C-type languages, functional programming languages, etc. That’s what makes it a separate language. If it’s a separate language, how can you possibly “just know it”? I am not personally that gifted, but perhaps you are.

Myles

···

On Feb 11, 2016, at 3:48 PM, Oliver M <selectedfordeletion@yahoo.com> wrote:

The point is to bring swift closer to natural English and further away from geek speak, making it easier to learn and use.

(Ideally a programming language would require no guide book at all, just like the iPhone requires no instruction manual...an unattainable goal but a worthy pursuit)

var myMessage String

What do you need to know that the above line does not tell you?

From: Myles Schultz <myleslschultz@icloud.com>;
To: Oliver M <selectedfordeletion@yahoo.com>;
Subject: Re: [swift-evolution] an observation on colons in variable type declarations
Sent: Thu, Feb 11, 2016 1:06:12 AM

Perhaps I have missed the point of what you were trying to say, but if you read the swift programming guide, you knew exactly what it was telling you. Like any language, one has to learn its conventions, it's grammar, etc. I don't see what is so confusing by the colon. It means "of type". It's narrow enough, in literal size, that it doesn't distract you like -> would. Yet, it is obvious enough that you can tell that an instant of a particular type is being declared. It's very clean and looks really nice in type. Personally, I think the colon is a keeper.

Sent from my iPhone

On Feb 10, 2016, at 2:20 AM, Oliver M <selectedfordeletion@yahoo.com <javascript:return>> wrote:

When I first read:

var myMessage: String

I knew a variable was being declared.
I knew the name of the variable.
I knew it was of type String.

I just couldn't figure out what on earth the colon was trying to tell me. Was this some sort of new swift construct??? A hybrid variable/function declaration? I was completely stumped!

From: Myles Schultz <myleslschultz@icloud.com <javascript:return>>;
To: Oliver M <selectedfordeletion@yahoo.com <javascript:return>>;
Subject: Re: [swift-evolution] an observation on colons in variable type declarations
Sent: Tue, Feb 9, 2016 11:48:15 PM

When you're working in Xcode where you have a color differentiation between the instance name and it's type, then taking out the colon may not seem like as big a deal. However, seeing your example below in black and white makes it quite that, no is not obvious that welcomeMessage is of type String. Instead it just looks like one long phrase on a line in an email. I think this comes down to punctuation. Think of writing a story an then every time a character says something, you take out the quotation marks. Now, you can certainly grasp from reading the surrounding text that someone is now saying something in many cases, but it is so much more clear with punctuation (the quotation marks). That's why they're there, readability. I feel the same is true here--the colon adds a significant amount of readability. One can make a quick glance at a block of code and if you know the Swift language, you'll instantly pick out the variable names and their types.

Sent from my iPhone

On Feb 9, 2016, at 5:45 PM, Oliver M via swift-evolution <swift-evolution@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@swift.org <>
https://lists.swift.org/mailman/listinfo/swift-evolution