Show HN: 2048 in Swift
github.com
github.com
I think it just hasn't been released yet. It's part of the Known Issues section of Xcode 6 release notes[0].
[0] http://adcdownload.apple.com//wwdc_2014/xcode_6_beta_ie8g3n/...
enum ActionToken {
case NoAction(source: Int, value: Int)
case Move(source: Int, value: Int)
case SingleCombine(source: Int, value: Int)
case DoubleCombine(source: Int, second: Int, value: Int)
// Get the 'value', regardless of the specific type
func getValue() -> Int {
switch self {
case let .NoAction(_, v): return v
case let .Move(_, v): return v
case let .SingleCombine(_, v): return v
case let .DoubleCombine(_, _, v): return v
}
}
// Get the 'source', regardless of the specific type
func getSource() -> Int {
switch self {
case let .NoAction(s, _): return s
case let .Move(s, _): return s
case let .SingleCombine(s, _): return s
case let .DoubleCombine(s, _, _): return s
}
}
}In C an enum can assume one of multiple predefined values of type `int`.
In Swift an enum can work like that too, but it can also assume values of types other than int. This is similar to the behavior of the unions in C, but more high level, with proper safety checks in place.
In Swift enums can also have methods, so there are two methods to return fields from the values contained in the enum —regardless of the actual type being held at that time.
To the GP. That code example is a very good example of just how different Swift code be from Objective-C.
case let .DoubleCombine(s, _, _): return s
Is the underscore a special character in this case to tell the compiler not to read in the value of those arguments? Or is it actually loading in "second" and then "value" into an argument named _? typedef sruct s
{
int type;
union
{
int i;
float f;
double d;
} value;
}
But there, it is up to you to make sure that you only read the int/float/double if the 'type' field indicates a int/float/double was stored. Higher-level languages enforce that you can only read the union as being of type T if you wrote it as type T.[1] http://docs.scala-lang.org/tutorials/tour/case-classes.html
[2] http://learnyouahaskell.com/syntax-in-functions#pattern-matc... "case expressions" at the end of the page.
[3] http://ocaml.org/learn/tutorials/data_types_and_matching.htm...
[4] http://static.rust-lang.org/doc/master/tutorial.html#control...
That's amazingly awkward syntax for a brand-new language. "case let"? In many other languages with pattern matching and destructuring, the "let" occurs implicitly.
foo = 1
switch bar {
// Equals the value of (existing) variable foo
case foo: return 0
// Catch-all, binds `foo` to whatever is in `bar`
case let foo: return foo + 1
}
(My syntax might be off, I've never written Swift)To do this in Haskell, you'd have to write something like:
foo = 1
case bar of
foo' | foo' == foo -> 0
foo -> foo + 1
Because you're only able to pattern-match against literal objects (constructors or numeric/string literals).This might be different in some other ML-like languages, or there could be GHC extensions for this, of course.
> case .NoAction(_, let v): return v
Putting it at the beginning makes it apply to all the variables in the parens (although in this case they are ignoring the first one by using the underscore).
Using "let" makes it immutable; you can also use "var" there to make it a variable you can modify in the case body.
- I wanted to try out Swift and see how it felt in practice to build a reasonably-sized project in it - and to see if the Xcode Developer Preview was anywhere near up to the task. I also wanted to see how the Cocoa APIs were going to work with Swift.
- I had an existing Objective-C implementation that I could work from. I wanted to see if Swift could make the implementation easier to read, more elegant, etc. While the original is hardly a thing of beauty, I thought it still might be useful to see how business logic to perform the same task might differ between the two languages.
I hope people who take a look, fork the repo, download and play with the code will enjoy reading through it and maybe take it as a taste of how things might be done (or not done) in the future.
Swift uses the .swift extension, which is pretty unique, so detection is as simple as checking for the file extension with a very low risk of a bad classification.
Some of the older, more popular languages uses more common file-suffixes, which may be shared between multiple programming languages, which has a negative impact on classification accuracy.
I added Game Maker Language, for example.
Shameless Plug: Want to learn Swift? Check out [http://www.LearnSwift.tips]
I'm one of those people that really likes objective-c but I was surprised that when I had to go back to working in objective-c this afternoon I started to realise how much time Swift would save me. I feel like at a glance it's harder to understand the code but it requires way fewer keystrokes and overtime will probably become much easier to understand at a glance.
Overall I really like it. I'll have to write a lot more code in it first but I could see myself switching to this for all my iOS code when iOS 8 is released.
I ask because I have some things I'd like to post but am reluctant.
It's under "Information Deemed Apple Confidential". "Further, Apple agrees that You will not be bound by the foregoing confidentiality terms with regard to technical information about pre-release Apple Software and services disclosed by Apple at WWDC (Apple's Worldwide Developers Conference), except that You may not post screen shots, write public reviews or redistribute any pre-release Apple Software or services."
Well, count me out. I like to work free.