Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Locked thread
Subjunctive
Sep 12, 2006

✨sparkle and shine✨

I strongly suspect I know the answer to this, but:

Does XCode 6.3 carry along any sort of compatibility mode for previous versions of the language? I'd like to bisect a Swift project, but the old code doesn't build with 6.3 and I really don't want to have to juggle XCode versions to make progress.

Adbot
ADBOT LOVES YOU

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

Subjunctive posted:

I strongly suspect I know the answer to this, but:

Does XCode 6.3 carry along any sort of compatibility mode for previous versions of the language? I'd like to bisect a Swift project, but the old code doesn't build with 6.3 and I really don't want to have to juggle XCode versions to make progress.

I believe there's an older compiler built into the migration tool, but I doubt you can use it to actually compile something. So nope.

Simulated
Sep 28, 2001
Lowtax giveth, and Lowtax taketh away.
College Slice
Is there a way to force the compiler to capture a local as a mutable reference or do we just manually box it?

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

Ender.uNF posted:

Is there a way to force the compiler to capture a local as a mutable reference or do we just manually box it?

Swift will always capture a local var as a mutable reference, up to the limits of as-if.

Simulated
Sep 28, 2001
Lowtax giveth, and Lowtax taketh away.
College Slice

rjmccall posted:

Swift will always capture a local var as a mutable reference, up to the limits of as-if.

Not always. rdar://20657776 filed with attached working test case as a playground.

code:
import Cocoa

// If `@noescape` is present, will print DANGER below.
func outerExecute(@noescape f: ()->Void) {
    f()
}

func executeMe(f1: ()->Void, f2: ()->Void) {
    f1()
    f2()
}

func createInstance() -> NSString? {
    var result: NSString?
    outerExecute {
        executeMe({ result = "2" },
            {
                if let r = result {
                    println("everything is OK")
                }
                else {
                    println("DANGER") //Oops
                }
        })
    }
    return result
}

createInstance()

KidDynamite
Feb 11, 2005

So I'm having a weird issue where I'm trying to make a whiteboard app and the code works fine on a workmates phone and on the simulators on his laptop but then as soon as I ty it any line I draw is skewed and moved up almost as if it's getting pushed onto the z axis. This is just code we're using from a Ray Wanderlich tutorial.

code:
    func drawLineFrom(fromPoint: CGPoint, toPoint: CGPoint) {
        
        // 1
        UIGraphicsBeginImageContext(view.frame.size)
        let context = UIGraphicsGetCurrentContext()
        tempImageView.image?.drawInRect(CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height))
        
        // 2
        CGContextMoveToPoint(context, fromPoint.x, fromPoint.y)
        CGContextAddLineToPoint(context, toPoint.x, toPoint.y)
        
        // 3
        CGContextSetLineCap(context, kCGLineCapRound)
        CGContextSetLineWidth(context, brushWidth)
        CGContextSetRGBStrokeColor(context, red, green, blue, 1.0)
        CGContextSetBlendMode(context, kCGBlendModeNormal)
        
        // 4
        CGContextStrokePath(context)
        
        // 5
        tempImageView.image = UIGraphicsGetImageFromCurrentImageContext()
        tempImageView.alpha = opacity
        UIGraphicsEndImageContext()
        
    }

    
    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
        swiped = false
        if let touch = touches.first as? UITouch {
            lastPoint = touch.locationInView(self.view)
        }
    }
    
    
    override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
        // 6
        swiped = true
        if let touch = touches.first as? UITouch {
            let currentPoint = touch.locationInView(view)
            drawLineFrom(lastPoint, toPoint: currentPoint)
            
            // 7
            lastPoint = currentPoint
        }
    }
    
    
    override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
        
        if !swiped {
            // draw a single point
            drawLineFrom(lastPoint, toPoint: lastPoint)
        }
        
        // Merge tempImageView into mainImageView
        UIGraphicsBeginImageContext(mainImageView.frame.size)
        mainImageView.image?.drawInRect(CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height), blendMode: kCGBlendModeNormal, alpha: 1.0)
        tempImageView.image?.drawInRect(CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height), blendMode: kCGBlendModeNormal, alpha: opacity)
        mainImageView.image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        
        tempImageView.image = nil
    }
Is there any reason why that logic would work fine for him and not for me? I suspect it's something to do with UIGraphicsGetImageFromCurrentImageContext() but we're both on the same version of Xcode. We're going to write our own code for the whiteboard soon but we just needed to get a demo off the ground really quickly.

Doh004
Apr 22, 2007

Mmmmm Donuts...
Getting tons of segmentation fault: 11 errors with any action besides Run in Xcode 6.3.1 in our Swift solution.

Anyone else running into this? Googling brings up other issues as well.

eschaton
Mar 7, 2007

Don't you just hate when you wind up in a store with people who are in a socioeconomic class that is pretty obviously about two levels lower than your own?

Ender.uNF posted:

swiftc is written in C++... is there any reason that it doesn't catch exceptions and bail on processing a section of code (or a file) if it blows up?

I think here you're conflating two different kinds of exceptions: language-level exceptions, and OS/system-level exceptions (traps, signals, whatever you'd like to call them).

You can use signal() handlers and Mach exception handlers to try to capture OS-level exceptions, but your recovery options are limited except for very specialized software (like debuggers).

Language-level exceptions tend to be designed for recoverability, though in practice most such exceptions should also wind up aborting the process because most software isn't itself designed for recovery.

Simulated
Sep 28, 2001
Lowtax giveth, and Lowtax taketh away.
College Slice

eschaton posted:

I think here you're conflating two different kinds of exceptions: language-level exceptions, and OS/system-level exceptions (traps, signals, whatever you'd like to call them).

You can use signal() handlers and Mach exception handlers to try to capture OS-level exceptions, but your recovery options are limited except for very specialized software (like debuggers).

Language-level exceptions tend to be designed for recoverability, though in practice most such exceptions should also wind up aborting the process because most software isn't itself designed for recovery.

I'm not conflating anything but I don't think I'm communicating what I'm asking for very well. I've been staying up until 3am every night working on my watch app.

toiletbrush
May 17, 2010
Possibly stupid swift noob question - how come I can do
code:
for x in someString

but I can't do
code:
someString.map { ... }

pokeyman
Nov 26, 2006

That elephant ate my entire platoon.
Try map(someString) { ... }

Dessert Rose
May 17, 2004

awoken in control of a lucid deep dream...
What's the deal with CKRecord and subscripting from Swift?

It works in ObjC because it implements the magic setObject:forKey: method, but Swift doesn't know about that. However, when I try to define it in a Swift extension, I get a compiler error because it's trying to define the magic method for me, but it already exists.

So I'm stuck calling setObject() from my Swift code. Which makes me sad.

jawbroken
Aug 13, 2007

messmate king
If the problem is a conflict on the Objective-C side then you could usually work around it by giving it a different name there, using @objc(…). I suspect that doesn't work for subscripts though, but I haven't tried it. The other workaround is to use some Swift feature that can't be exported to Objective-C, such as generics, but it would take some fiddling to find out if that is viable.

toiletbrush
May 17, 2010

pokeyman posted:

Try map(someString) { ... }

I get that you can do that, it just seems odd to me that you can do all these 'treat a string as an array of characters' operations, but the standard map-reduce syntax doesn't work, and was wondering what the reasons behind that are.

Doh004
Apr 22, 2007

Mmmmm Donuts...
*edit* Nevermind, I'm dumb and can't read.

brap
Aug 23, 2004

Grimey Drawer
Map is available on strings because they're sequences. For whatever reason you just can't do extensions on sequences, but you can do regular old functions for sequences. I think this relates to type constraints not being available on extensions. They actually bothered to make map a method on Array but not on certain other types.

toiletbrush
May 17, 2010
Yeah that explains it, plus I was really confused...I assumed map() and reduce() where extensions on SequenceType/GeneratorType, rather than just being methods of Array(). Is there any chance of having extensions on protocols in the future?

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe
Pretty good odds, I'd say, and sooner rather than later. The standard library people have wanted that for ages.

Simulated
Sep 28, 2001
Lowtax giveth, and Lowtax taketh away.
College Slice

fleshweasel posted:

Map is available on strings because they're sequences. For whatever reason you just can't do extensions on sequences, but you can do regular old functions for sequences. I think this relates to type constraints not being available on extensions. They actually bothered to make map a method on Array but not on certain other types.

A million times this... I repeatedly find myself wanting to define an extension on some type but only when it satisfies certain generic constraints. It seems like a small thing but it just makes the coding process so much more intuitive when the methods pop up in autocomplete because they're defined explicitly for the type you're working with.


Anyone else have Swift frameworks they've been loving? PromiseKit (despite its slightly rough shape) and Cartography have been wonderful and fairly good demonstrations of how much cleaner and nicer it can be to go 100% Swift.

toiletbrush
May 17, 2010
I'm trying to write a lovely stack based language in Swift, and its been great fun so far...are abstract classes/methods planned for Swift at all? What about implicit return values?

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe
We've thought about abstract classes/methods, but haven't yet designed them. Is there something you can't do with a class-constrained protocol?

We don't have plans for implicit return values, but I'm curious to know what you're thinking of using them for.

Doh004
Apr 22, 2007

Mmmmm Donuts...
Hey rjmccall,

We're getting segmentation faults during compilation on a select few view controllers. All of them inherit from a base subclassed UITableViewController. This crashes in any configuration where "Swift Compiler Code Generation - Optimization level is set to anything above [None].

We no longer get the segmentation faults after having removed any of the optimization levels. Have you seen this before?

Doh004 fucked around with this message at 23:08 on May 5, 2015

toiletbrush
May 17, 2010

rjmccall posted:

We've thought about abstract classes/methods, but haven't yet designed them. Is there something you can't do with a class-constrained protocol?
No...I posted that before I realised I should be using an interface! There are definitely circumstances in Obj-C (faked, obvs) and C# where they have been useful though.

quote:

We don't have plans for implicit return values, but I'm curious to know what you're thinking of using them for.
It was just a thought, they sort of show up in closures and I wondered if they might be nice in things like getter methods for var's, but to be honest I was more interested in hearing pro's and con's for having them in a language.

toiletbrush
May 17, 2010
This code seems to an exception in the compiler when run at SwiftStub, with "expression was too complex to be solved in reasonable time"

code:
[Int](0...10).map {
    println(String(format:"%02d ", $0 / 2) + ["even","odd"][$0 % 2] + ",")
}
I can't try on a Mac at the moment...is it worth filing a Radar for?

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

toiletbrush posted:

This code seems to an exception in the compiler when run at SwiftStub, with "expression was too complex to be solved in reasonable time"

code:
[Int](0...10).map {
    println(String(format:"%02d ", $0 / 2) + ["even","odd"][$0 % 2] + ",")
}
I can't try on a Mac at the moment...is it worth filing a Radar for?

It's always worth filing a radar for things like this.

Dessert Rose
May 17, 2004

awoken in control of a lucid deep dream...
I think there are two reasons I post my issues here first.

One is that I'm not even sure if what I'm writing is valid Swift in the first place, and I don't want to spend the time writing up a bug about it.

Two is that writing up a bug is going to kill my productivity for the next hour as I reduce it into a test case and put it into a project, write up the repro steps properly, etc.

Compared to "just paste some code and rjmccall will call me an idiot or tell me to file a radar", it's pretty obvious which one I'm going to do first.

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe
Well. You are welcome to continue to use me for first-level bug triage, if the alternative is just not filing the radar. That said, if you can get a test case down to the point where "xcrun swift myfile.swift" produces something obviously wrong — it emits a bad diagnostic or crashes in the compiler or crashes running the program or generates meaningless output — you really don't need anything else in the radar besides the file, the command line, and a copy/paste of the bad output. I hope there isn't some weird process preventing that.

"Expression too complex" radars in particular are almost always good to file, because while our type-checker is kindof inherently algorithmically inefficient, most of those problems are not really limited by that inherent complexity, and there's usually some way we could be solving the constraint system much more efficiently. In toiletbrush's example, the type-checker has probably decided to try to solve the part of the system that's within the closure before deducing that $0 : Int, and so the complexity is blowing up.

(Why are you writing that with map instead of "for i in 0...10 {", anyway?)

toiletbrush
May 17, 2010

rjmccall posted:

In toiletbrush's example, the type-checker has probably decided to try to solve the part of the system that's within the closure before deducing that $0 : Int, and so the complexity is blowing up.
Does that explain why this...
code:
[Int](0...10) {
    println(String(format:"%02d ", $0 / 2) + ["even","odd"][$0 % 2] + ",")
}
...still throws the same exception even though the code is invalid and shouldn't compile?

quote:

(Why are you writing that with map instead of "for i in 0...10 {", anyway?)
The original code that the sample is based on is quite different and was passed an Int array, and also changing the .map to a for/in statement makes compilation succeed, I guess for the reason you mentioned. It looks odd but it was just the simplest reproduction of the error I could write.

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

toiletbrush posted:

Does that explain why this...
code:
[Int](0...10) {
    println(String(format:"%02d ", $0 / 2) + ["even","odd"][$0 % 2] + ",")
}
...still throws the same exception even though the code is invalid and shouldn't compile?

Yeah. The type-checker proceeds by considering an entire expression at once, since in theory any part of it can affect any other part. Of course, it can usually retire/simplify constraints immediately, break things into subproblems, etc. But if it can't finish type-checking, it can't finish type-checking, and that means it can't decide whether the code should compile or not.

If anyone's really curious, our type system is basically System F<, which we try to reconstruct with a custom constraint solver. The way this works in practice is that Swift basically assigns a "type variable" to every expression, like so:

pre:
  0 : T0
  10 : T1
  ... : T2
  0...10 : T3
  [Int](0...10) : T4
  [Int](0...10).map : T5
etc.
And then it adds a bunch of constraints between these type variables, like so:

pre:
  T2 = (T6, T7) -> T3                       // from binary operator syntax
  T2 in { <all the overloaded ... types> }  // the result of operator lookup
  (T0, T1) < (T6, T7)                       // call argument conversion
  [Int] hasMember "init" : T8               // from initialization syntax; will turn into an "in" constraint like the above
  T8 = T9 -> T4                             // initialization syntax calls the init method
  T3 < T9                                   // call argument conversion
  T4 hasMember "map" : T5                   // from member reference syntax
etc.
So what should be happening here is that it should figure out that T4 can be deduced independently from the rest of the system, which will let it perform the lookup, which should give it an unambiguous type for T5, which should tell the rest of the system that the $0 : Int, which should be sufficient to dictate the type of everything else unambiguously. But for some reason that's not happening.

Carthag Tuek
Oct 15, 2005

Tider skal komme,
tider skal henrulle,
slægt skal følge slægters gang



I'm playing around with generating Swift programmatically. In C/Objc-C/etc you can put #line 95 "file.ext" to make errors & such point to the correct line in the input file that the Swift is generated from. I guess I can't do that in Swift?

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

Snapchat A Titty posted:

I'm playing around with generating Swift programmatically. In C/Objc-C/etc you can put #line 95 "file.ext" to make errors & such point to the correct line in the input file that the Swift is generated from. I guess I can't do that in Swift?

Uh, no, not right now, sorry.

Carthag Tuek
Oct 15, 2005

Tider skal komme,
tider skal henrulle,
slægt skal følge slægters gang



Alright, thanks :tipshat:

Dessert Rose
May 17, 2004

awoken in control of a lucid deep dream...

rjmccall posted:

if you can get a test case down to the point where "xcrun swift myfile.swift" produces something obviously wrong — it emits a bad diagnostic or crashes in the compiler or crashes running the program or generates meaningless output — you really don't need anything else in the radar besides the file, the command line, and a copy/paste of the bad output. I hope there isn't some weird process preventing that.

Ah, that's much better. I'd had a couple of bugs bounced because I included just a playground or not even that, and they said to submit an entire project. If the actual requirements are somewhere in the middle, then I'll submit more bugs, thanks :)

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

Dessert Rose posted:

Ah, that's much better. I'd had a couple of bugs bounced because I included just a playground or not even that, and they said to submit an entire project. If the actual requirements are somewhere in the middle, then I'll submit more bugs, thanks :)

Well, like I said, the screeners might be jerks. But the team can certainly fix bugs based only on a single file and a command line, and that's how we file them internally.

toiletbrush
May 17, 2010

rjmccall posted:

Well, like I said, the screeners might be jerks. But the team can certainly fix bugs based only on a single file and a command line, and that's how we file them internally.
I think I might have been reporting my bugs to the wrong place, what is the correct product for filing Swift bugs?

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

toiletbrush posted:

I think I might have been reporting my bugs to the wrong place, what is the correct product for filing Swift bugs?

There's a Swift component. I... actually don't know what bugreport.apple.com looks like for you, because it immediately forwards to an internal web version of the application for me; but I'm pretty sure you can just file against Swift. I would not file against Playgrounds unless it only duplicates in Playgrounds and/or is a problem with the Playgrounds user interface; and I wouldn't file it against Xcode in general unless it's obviously a UI or build-system problem (and the dividing lines for the latter are blurrier than you might expect).

Dessert Rose, we do accept a lot of playgrounds as bug reports; maybe you just ran into someone feeling cantankerous, sorry. The only time we really need it as a project is when it's a multi-file compilation issue, which does come up a fair amount.

toiletbrush
May 17, 2010

rjmccall posted:

There's a Swift component.
Oh, it doesn't show up for me...I'm a member of iOS and Mac developer programs, but in the Development section I only get iOS/SDK, OS X/SDK and a couple others but nothing for Swift specifically...

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

toiletbrush posted:

Oh, it doesn't show up for me...I'm a member of iOS and Mac developer programs, but in the Development section I only get iOS/SDK, OS X/SDK and a couple others but nothing for Swift specifically...

Bah. I'm not sure where you're supposed to file these bugs, then, sorry. Probably Xcode.

Toady
Jan 12, 2009

I file Swift bugs under Developer Tools.

Adbot
ADBOT LOVES YOU

Kallikrates
Jul 7, 2002
Pro Lurker
How are you guys handling the optional protocol pattern in swift? I want to make it a rule that if you need this pattern in swift to not use @objc, but to use inheritance or composition of different protocols and optionals. But I'm getting push back because of what I think is a crufty reason.

"It's a useful pattern and thats why apple gave us @objc"

Outside of IBOutlets, and Vending to objc I want to remove as much @objc as possible.

  • Locked thread