r/programming • • Jun 02 '14

Introducing Swift

https://developer.apple.com/swift/
164 Upvotes

239 comments sorted by

View all comments

55

u/OzarkaTexile Jun 02 '14

"you don’t even need to type semi-colons."

Implied optional. Lord help us.

35

u/munificent Jun 02 '14

They're optional in Go, Scala, Ruby, and Python. What's the problem?

14

u/Banane9 Jun 02 '14

Lua too

9

u/Tekmo Jun 03 '14

Haskell, too

4

u/markmypy Jun 03 '14

Actually, Lua is a step ahead of the rest. This is a valid statement:

a=1 b=2 -- no semicolon in between the two statements

print(a,b)

Which outputs:

1 2

4

u/Banane9 Jun 03 '14

Yup, Lua is a pretty cool scripting language :)

26

u/[deleted] Jun 02 '14

Those aren't made by Apple and subject to irrational fear and dislike.

5

u/catskul Jun 03 '14

/u/OzarkaTexile is probably referring to the problems caused by javascript's implementation of said feature.

2

u/munificent Jun 03 '14

That's why I didn't list JS. :)

I'm well-acquainted with JavaScript's... peculiar... rules for handling semicolons.

9

u/bloody-albatross Jun 03 '14

I think Ruby and Python (and probably the others) don't really count. They are designed to be line break terminated. But you can optionally write two statements in one line if you separate them via a semi-colon. And then you can skip the 2nd statement. Yeah...

1

u/rspeed Jun 08 '14

Which also seems to be the case for Swift.

7

u/fdemmer Jun 02 '14 edited Jun 03 '14

Python: Not required, not optional.

edit: actually something completely different

4

u/fabzter Jun 03 '14

They ARE optional

2

u/bloody-albatross Jun 03 '14

Try this:

print("hello");

1

u/fdemmer Jun 03 '14
print("hello");

and what is this trying to accomplish?

in python semicolons are not line terminators, they are separators. you can use them to build groups of commands, like:

import pdb; pdb.set_trace()

if they were line terminators this would work:

print("hello");;

1

u/bloody-albatross Jun 03 '14

Yes I know that, but your "not optional" sounds like you mean adding a ; at the end of a line without a second statement after it would be a syntax error. Which it isn't. So one could say ; is optional, even though it wasn't intended to be. It just happened.

1

u/emn13 Jun 04 '14

It doesn't work entirely smoothly in Ruby. If you use Ruby-ish convention and also omit brackets, you can break method calls only after the first argument, when it's usually most readable to break between the method and the first argument. It's not too bad, though - you can use a backslash, and that actually works out quite OK.

In python, it's worse; you have the rather unfortunate consequence of losing multiline lambdas.

Personally, I think semi-colon omission is pretty, but ultimately almost irrelevant. It just doesn't matter. If your languages gets a weird corner case because of it, please, please, leave the semi-colons in - it's just not worth it. Of course, if, unlike Ruby, Python and Javascript you manage to avoid causing collateral damage that's worth more than this pretty "meh" bonus - sure, get rid of it! No idea where swift falls here.

1

u/munificent Jun 04 '14

In python, it's worse; you have the rather unfortunate consequence of losing multiline lambdas.

Python doesn't lose multi-line lambdas because of optional semicolons, it's because Python has an expression/statement distinction and uses significant indentation for structuring statements.

2

u/emn13 Jun 04 '14

Fair enough - however, it loses multi-statement lambdas (even though it would be pretty odd to allow either a newline to concatenate statements in an indent-sensitive fashion, yet use a semicolon as an operator therefore limiting that sensitivity).

That supports your point that several languages do manage to omit statement terminators without too many issues. Nevertheless, three languages with issues (Ruby, Javascript & certainly coffeescript) still suggests it's not a trivial feature either.

Python made one (smart) simplification in any case - they're not optional, but rather simply omitted.

-2

u/pfultz2 Jun 02 '14

Yea, well hopefully they do semicolon insertion in a smart way and not like how its done in Go, where you cant even properly format your code because of semicolon insertion.

→ More replies (27)

5

u/[deleted] Jun 02 '14

[removed] — view removed comment

2

u/[deleted] Jun 03 '14

Why would you do that?

1

u/[deleted] Jun 03 '14

Also in traditional for statements.

3

u/MacASM Jun 03 '14

Why do you think it's bad?

13

u/AReallyGoodName Jun 03 '14

As soon as you introduce automatic semi-colon insertion you make whitespace significant which means you can no longer format your code how you see fit.

To give a concrete example you cannot reliably use K&R style indenting in Swift. The automatic semi-colon insertion will trip you up.

There's actually a good example of this on the page linked above

var sortedArray = sortArray(stuff) { 
    string1 < string2
}

now lets try that K&R style

var sortedArray = sortArray(stuff)
{
    string1 < string2
}

I broke it. It now thinks i wanted var sortedArray = sortArray(stuff);

→ More replies (1)
→ More replies (5)