Go Down

Topic: Apple swift (Read 4291 times) previous topic - next topic

mistergreen

I'm reading up on apple's new C language, Swift. It can return from a function multiple values with any combination of data types.

That is all. I'm sold!

leo72

The Swift programming language is the successor of Objective-C, I think it's not a C-family member but a C++-family member.
It has been developed to update the old-fashioned Obj-C and to result more easy and clear in use than it.
I didn't try it yet but voices on its performances are constrasting: someone says that it's faster than Obj-C, someone else says it's slower...
The only thing that it's clear is that it's an Apple-only language, due to the fact that it will run only on MAC/iOS.

mistergreen

yeah, it's iOS, OS X only which is a shame. It should be platform independent.

leo72

It's very friendly, it has a syntax that remembers me that one of Ruby.

mistergreen

I'm skimming through the use manual now. It's simple and powerful. It takes a little getting use to even for me since I know objective C.

mistergreen

I see a lot of rookie mistakes here on the forum like
Code: [Select]
if (x = 1)
C allows for this without throwing any errors. In swift this is illegal and will error. Neat change.

leo72

IMHO that should not be considered an error at all (I mean in the "C world"). I mean, it's legal even it's not logic. Who would have to know if an assignement has been done or not? In the other hand, who would have to do an assignement in an equality testing?
Why did they use two different symbols? I understand that there are historic considerations behinds, but I do not know them  ;)
Personally, I began programming with BASIC, and used the "=" sign both the operations, and I disagreed the way C interpretes those operands.

mistergreen

#7
Jul 06, 2014, 07:48 pm Last Edit: Jul 06, 2014, 07:52 pm by mistergreen Reason: 1
In swift you can do
Code: [Select]

if var x = 1


or

Code: [Select]

if let x = 1 //let is const in C

and that would be legal. I'm not sure how declaring the variable in the if condition works but that's legal.

I guess declaring a variable in the condition would be an automatic true?

mistergreen

#8
Jul 06, 2014, 09:08 pm Last Edit: Jul 06, 2014, 09:12 pm by mistergreen Reason: 1
Oh btw, the datatype on variables are assumed like

Code: [Select]
var x = 1
The compiler assumes x is an int since you assigned it an integer but you can you can manually do it too.

Code: [Select]
var x: Int = 1

pYro_65


In swift you can do
Code: [Select]

if var x = 1

I guess declaring a variable in the condition would be an automatic true?


Its no different to C++:

Code: [Select]
if( int x = 1 )

Dunno about swift, however, C++ will return false if 0 was assigned.

Quote
Oh btw, the datatype on variables are assumed


C++11 has the 'auto' data type.

I'll stick with my platform independent stuff for now.

mistergreen



Code: [Select]
if( int x = 1 )

Dunno about swift, however, C++ will return false if 0 was assigned.



In swift an int 1 and 0 can't  represent true and false respectively. Only Boolean values.

ronlisle

Swift is now open source. I'm looking forward to being able to use it on my Arduinos! Is anybody out there looking at porting it?

robtillaart

moderator: removed crosspost
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

cr0sh

I haven't played around with Swift, but from what I've seen of it, it looks like a decent language. I've also looked into Rust - it too looks decent to work with.

But the "new kid" language I have the most experience with recently has been Go (btw, if you look into this, use the term "GoLang" in your searches - it will make things easier due to the commonality of the languages name).

I had to learn this language fairly quickly with a colleague of mine at my job, because we were implementing a driver for Docker and containers for my employer's public-facing API. After more than a bit of frustration, both I and my friend became "go converts".

The greatest thing we like about it, is that it doesn't let you "get away" with stuff. For instance, if you declare a variable but don't use it - it won't compile. At first, that (and many other small differences) feels like a huge burden - it makes you want to scream. Ultimately, though, it teaches you better coding style and control - so that you don't have orphaned code or anything else like that in your codebase. This leads to better understanding of the actual source code, and easier maintainability in the future.

Just one minor feature among many that has me sold on it.

Swift, though, I only gave a cursory look into, because back then it was an "Apple-only" product. Now that it is open source, though - it may be time I gave it a better look.
I will not respond to Arduino help PM's from random forum users; if you have such a question, start a new topic thread.

AWOL

IMHO that should not be considered an error at all (I mean in the "C world"). I mean, it's legal even it's not logic. Who would have to know if an assignement has been done or not? In the other hand, who would have to do an assignement in an equality testing?
Why did they use two different symbols?
Because they're two different operations.

Mathematically, "x = x + 1" in C or BASIC is nonsensical; Pascal and Algol were much better, IMO, in having an assignment operator ":=", so "x := x + 1;"  (think of it as a left-pointing arrow, was how I was taught to look at it)
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy