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
dizzywhip
Dec 23, 2005

There was a thread on the dev forums a little while ago where an Apple engineer said they were aware that optional booleans are error-prone and they consider it a problem. So hopefully that'll get fixed before 1.0.

I thought I'd mention this since I don't think a lot of people know about it yet: I was really happy to learn the other day that you can use the same name for the unwrapped value as the optional when using if let. So you can do if let value = value instead of having to come up with some arbitrary name for one or the other like if let v = value.

Adbot
ADBOT LOVES YOU

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed
if x? { ... } would be a cute way to do explicit optional -> boolean conversions.

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

ljw1004 posted:

rjmcall Please remove this feature, and instead implement a method ".HasValue" on Optional<T>. That way we can write code that's easy to read:

This is actually already in the works, although I'm not sure how we're going to spell the property yet. (Other than definitely not using C# casing.)

Doctor w-rw-rw-
Jun 24, 2008

rjmccall posted:

This is actually already in the works, although I'm not sure how we're going to spell the property yet. (Other than definitely not using C# casing.)

I'm a fan of Guava's Optional's .isPresent(), or alternatively .present() for brevity if that is desired.

Axiem
Oct 19, 2005

I want to leave my mind blank, but I'm terrified of what will happen if I do

Ender.uNF posted:

rdar://17782662 filed to add the ability to export a module's internal members to another specified module. This makes unit tests on internal members much easier and allows two modules by the same developer to cooperate as if they were the same module, but without making that part of the public API.

See also: http://www.russbishop.net/swift-testing-privates

Yes, this is 100% the most annoying thing about access modifiers right now. Right after getting the beta 4 and fixing the fact that Double no longer auto-converts to CGFloat, I tried running tests and ended up with an awful lot of failures because I need to sprinkle "public" everywhere.

Grace Baiting
Jul 20, 2012

Audi famam illius;
Cucurrit quaeque
Tetigit destruens.



I HAVE A BUG TO REPORT!!

The blogposts from the Swift Blog's RSS feed are almost wholly unformatted, and in this most recent blog post about Access Control, it completely omitted the example code and instead had "Here is an example class that is part of a framework: [view code in blog] ". In the xml source I can see there are some extra spaces where bullet points should be, but there's not even a newline before them (and of course the rendered output has all contiguous whitespace condensed anyway). I want to read the entire blogpost in my RSS reader, not a seeming copy-paste job that excludes example code!

Here's the current RSS feed itself, for the curious: http://pastebin.com/KSjuf2jE

I know that the workings of the Swift Blog's RSS feed are of utmost importance to you rjmccall, so uh, you're welcome

I really enjoy reading about all this stuff and I haven't filed a bugreport in years and years -- should this actually be a radar?

dizzywhip
Dec 23, 2005

Axiem posted:

Yes, this is 100% the most annoying thing about access modifiers right now. Right after getting the beta 4 and fixing the fact that Double no longer auto-converts to CGFloat, I tried running tests and ended up with an awful lot of failures because I need to sprinkle "public" everywhere.

As an alternative to manually labeling everything as public, you can group your public API into an extension labeled as public, and everything inside that extension will default to public.

Axiem
Oct 19, 2005

I want to leave my mind blank, but I'm terrified of what will happen if I do

dizzywhip posted:

As an alternative to manually labeling everything as public, you can group your public API into an extension labeled as public, and everything inside that extension will default to public.

In quite a few cases, this will end up being an extension of everything in the class. At the current state of the project, most of my things are either value objects or don't have much internal logic; they're all API.

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

Abjad Soup posted:

I HAVE A BUG TO REPORT!!

The blogposts from the Swift Blog's RSS feed are almost wholly unformatted, and in this most recent blog post about Access Control, it completely omitted the example code and instead had "Here is an example class that is part of a framework: [view code in blog] ". In the xml source I can see there are some extra spaces where bullet points should be, but there's not even a newline before them (and of course the rendered output has all contiguous whitespace condensed anyway). I want to read the entire blogpost in my RSS reader, not a seeming copy-paste job that excludes example code!

Here's the current RSS feed itself, for the curious: http://pastebin.com/KSjuf2jE

I know that the workings of the Swift Blog's RSS feed are of utmost importance to you rjmccall, so uh, you're welcome

I really enjoy reading about all this stuff and I haven't filed a bugreport in years and years -- should this actually be a radar?

Oh man. I'm sure it would get routed to the right people if you filed it as a radar. I have no idea who those people would be, though.

Simulated
Sep 28, 2001
Lowtax giveth, and Lowtax taketh away.
College Slice
This is the second time I've started writing this horrible abomination... if I don't get some real reflection soon I won't be able to stop myself from unleashing it upon the world. I am going to get the type's members and their type names one way or another.

Also the unknowns are suspiciously in the executable region; brute forcing the function call didn't work but I found someone who figured out the function call trampoline and associated structs. When I get back from dinner that's my next horrible, horrible step.

I'm going to put functions inside a dictionary and invoke them later if it kills me.

code:
struct _MagicMirrorData {
    let owner: Int = 0
    let ptr: Int = 0
    let metadata:Any.Type = Any.self
    
    let unknown1: Int = 0
    let unknown2: Int = 0
}

struct _ClassMirror {
    let data:_MagicMirrorData = _MagicMirrorData()
}

let instance = Mine()
let mirror = reflect(instance)

let key = mirror[0].0
let value = mirror[0].1

//HORRIBLE
let horrible:_ClassMirror = reinterpretCast(value)
horrible.owner //actually swift NativeObject
horrible.ptr //RawPointer to ExistentialMetatype


//less horrible BUT STILL HORRIBLE
@asmname("swift_stdlib_getTypeName") func _stdlib_getTypeNameImpl<T>(value: T, result: UnsafePointer<String>)
let buf = UnsafePointer<String>.alloc(1)
_stdlib_getTypeNameImpl(key, buf) //mangled name
P.S. I'm moving to the SF/Bay area soon, joining a startup and moving to iOS full time. Can I be part of the cool kids club now?

Flobbster
Feb 17, 2005

"Cadet Kirk, after the way you cheated on the Kobayashi Maru test I oughta punch you in tha face!"

Ender.uNF posted:

P.S. I'm moving to the SF/Bay area soon, joining a startup and moving to iOS full time. Can I be part of the cool kids club now?

Enjoy the huge rent. :v: I thought I could escape it in San Jose (and did for a year!) but it eventually caught up down here too. Now there's no escaping it.

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

Flobbster posted:

Enjoy the huge rent. :v: I thought I could escape it in San Jose (and did for a year!) but it eventually caught up down here too. Now there's no escaping it.

Oh yes, it's crazy. I'm going to try to buy if possible. I just can't pay 3000-4000 a month for an apartment and end up with nothing for it.

That said, I have no idea how difficult it is to get a mortgage out there and I hear stories of people coming in with all cash offers, so that's scary too.

Simulated fucked around with this message at 01:51 on Jul 25, 2014

Carthag Tuek
Oct 15, 2005

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



Ender.uNF posted:

Oh yes, it's crazy. I'm going to try to buy if possible. I just can't pay 3000-4000 a month for an apartment.

Afaik you want to avoid buying because transfer of ownership means taxes get recalculated in California, so it might be more expensive in the end. Least that's what I heard, I've only rented there.

Axiem
Oct 19, 2005

I want to leave my mind blank, but I'm terrified of what will happen if I do
What protocol do I need to implement on an object so that XCTAssertEqual will output what I want in its default text, instead of the mangled name of the class? I've tried Streamable, Printable, and DebugPrintable to no avail. I'm getting really tired of my tests failing with the eternally unhelpful message `XCTAssertEqual failed: ("_TtC11SpaceTrains4Star") is not equal to ("_TtC11SpaceTrains4Star")`. It would be nice to be able to at least identify which is which in the output, so I could better figure out why a test is failing, and I'd rather not go and add my own custom format to each assertion when the default one is otherwise quite fine.

Simulated
Sep 28, 2001
Lowtax giveth, and Lowtax taketh away.
College Slice
edit: I see what is happening here; those things are @auto_closures and the compiler is breaking down the AST (presumably) and doing other fancy strangeness so you can't actually do anything about it.

Simulated fucked around with this message at 05:40 on Jul 29, 2014

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

quote:

operator infix !! { associativity left }

@infix public func !!<T>(left: T?, right: T?) -> T? {
return left ? left! : right
}

@infix public func !!<T>(left: T?, right: @auto_closure ()->T) -> T {
return left ? left! : right()
}

var x:String?
var y:String?
let z = "alt"

let a = x !! y !! z

Is there any reason this shouldn't work? The compiler complains that no overloads take the supplied arguments which is incorrect. In theory it should be using the first on x !! y, which produces an optional and so the second would be called with that result and z. If you move "alt" inline the compiler complains about converting a StaticString to String.

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe
It is probably failing to correctly order the auto-closure argument with the argument requiring an optional conversion.

Simulated
Sep 28, 2001
Lowtax giveth, and Lowtax taketh away.
College Slice
Ok, rdar://17842481 filed

Edit: autocorrect :mad:

Simulated fucked around with this message at 18:45 on Jul 29, 2014

Simulated
Sep 28, 2001
Lowtax giveth, and Lowtax taketh away.
College Slice
I did a horrible, horrible thing

code:
@asmname("swift_demangleSimpleClass") 
func demangleSimpleClass(mangledName: ConstUnsafePointer<Int8>, 
     moduleName: UnsafePointer<Int>, 
     className: UnsafePointer<Int>) -> Bool

let someInstance = ...
let namen = object_className(someInstance)

let p2 = UnsafePointer<Int>.alloc(1)
let p3 = UnsafePointer<Int>.alloc(1)

let ret = demangleSimpleClass(namen, p2, p3)

if ret {
    let p2_2 = UnsafePointer<Int8>(p2.memory)
    let p3_2 = UnsafePointer<Int8>(p3.memory)

    let moduleName = String.fromCString(p2_2)!
    let className = String.fromCString(p3_2)!

    println("Type Name: \(moduleName).\(className)")
} else {
    println("Failure")
}

p2.dealloc(1)
p3.dealloc(1)
:stare:

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

Ender.uNF posted:

code:
var p2 = 0
var p3 = 0
let ret = demangleSimpleClass(namen, &p2, &p3)

:science:

ultramiraculous
Nov 12, 2003

"No..."
Grimey Drawer

Is this abomination rjmccall approved now or something?

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


:eng99:

To be fair, I did try something like that but forgot I declared them "let" and the compiler just gave me another "cannot convert to @lvalue $T###" sort of message. I filed rdar://17857686 to ask that Swift output something like "Cannot pass 'let' constant reference to parameter expecting mutable 'inout'". I'm sure you already have a ton of bugs related to type conversion error messages since they're all pretty much like that in beta 4.

So what I'm saying is I take no responsibility for my own failures and blame it all on you :v:


P.S. Is there any way to get the type names/descriptions that the REPL will spit out? (i.e. find out that i's type is Swift.Int)

pre:
Welcome to Swift!  Type :help for assistance.
  1> let i = 5
i: Int = 5
  2> let mirror = reflect(i)
mirror: _LeafMirror<Int> = {
  _value = 5
  summaryFunction =
  quickLookFunction =
}
  3> mirror.valueType
$R0: Any.Type = Swift.Int 

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

ultramiraculous posted:

Is this abomination rjmccall approved now or something?

You can do what you want, Abe.

xilni
Feb 26, 2014




Have you done any benchmarking on Swift to see how it compares? Does it keep C/C++'s speed?

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe
Right now? Definitely not. And the overhead of automatic memory management will limit us from ever exactly matching C on all workloads. On the other hand, I think eventually we'll have a complete enough set of language/implementation tools to allow users to reliably optimize away overheads when it matters without massively distorting their program.

xilni
Feb 26, 2014




rjmccall posted:

Right now? Definitely not. And the overhead of automatic memory management will limit us from ever exactly matching C on all workloads. On the other hand, I think eventually we'll have a complete enough set of language/implementation tools to allow users to reliably optimize away overheads when it matters without massively distorting their program.

Is this one of the reasons they're not rushing to rewrite Mail.app etc... in Swift?

Toady
Jan 12, 2009

xilni posted:

Is this one of the reasons they're not rushing to rewrite Mail.app etc... in Swift?

Mail is a shipping app with time-tested code that already works.

DarkJC
Jul 6, 2010
Swift is also still in beta and thats a pretty large codebase to rewrite in a language that could still have syntax and behavioural changes.

feedmegin
Jul 30, 2008

rjmccall posted:

Right now? Definitely not. And the overhead of automatic memory management will limit us from ever exactly matching C on all workloads.

Makes sense; safety costs. Presumably as a statically compiled language with a decent optimisation backend you should end up coming pretty close, though?

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

feedmegin posted:

Makes sense; safety costs. Presumably as a statically compiled language with a decent optimisation backend you should end up coming pretty close, though?

Yes, we don't see anything that should prevent us from generating very high-performance code on the approximate order of C.

Kallikrates
Jul 7, 2002
Pro Lurker
Something that I sometimes did in Objective-C was return or accept specific types that conform to specific protocols.
Using Generics we can do this for function parameters (I think its analogous) but how do we enforce this on the return side?

code:

- (NSObject <Foo> *)returnsSomethingThatConforms;

code:

//compiler error
func returnsSomethingThatConforms() -> (T: NSObject where U: Foo) {}
//compiler error
func returnsSomethingThatConforms() -> (NSObject, Foo) {}

Most of my googling and reading of documentation only answers the parameter side of the question. Did I find my first swift radar?

Dessert Rose
May 17, 2004

awoken in control of a lucid deep dream...

Kallikrates posted:

Something that I sometimes did in Objective-C was return or accept specific types that conform to specific protocols.
Using Generics we can do this for function parameters (I think its analogous) but how do we enforce this on the return side?

code:

- (NSObject <Foo> *)returnsSomethingThatConforms;

code:

//compiler error
func returnsSomethingThatConforms() -> (T: NSObject where U: Foo) {}
//compiler error
func returnsSomethingThatConforms() -> (NSObject, Foo) {}

Most of my googling and reading of documentation only answers the parameter side of the question. Did I find my first swift radar?

Did you try func foo() -> (Foo) {}?

Axiem
Oct 19, 2005

I want to leave my mind blank, but I'm terrified of what will happen if I do

Kallikrates posted:

code:
//compiler error
func returnsSomethingThatConforms() -> (T: NSObject where U: Foo) {}
//compiler error
func returnsSomethingThatConforms() -> (NSObject, Foo) {}

Is there some reason you can't:

code:
func returnsSomethingThatConforms() -> (Foo) {}
fund returnsSomethingThatConforms() -> (protocol<NSObject, Foo>) {}
?

Also, is there a reason you're insisting it be an NSObject, instead of just an object that conforms to Foo?

Kallikrates
Jul 7, 2002
Pro Lurker
The example is derived..., the object implementing the protocol is not a plain NSObject. On the API consumer side, knowing its class X implements protocol Y is helpful when you want to call functions of class X and protocol Y without having to cast back and forth between "id<Foo>" and "X" when I get back to my computer with Xcode Beta ill try (protocol<NSObject, Foo>) but I don't think its a solution because the NSObject protocol only exists for base NSObject, not subclasses,

Another example:
code:
extension CustomNSOperationClass {
  func returnsSomethingThatConforms() -> (CustomNSOperationClass, FooProtocol) {

  }
}
//given the return type no as? should be needed
CustomNSOperationClass.returnsSomethingThatConforms().start().fooProtocolMethod()
The major use case is extensions/categories that add protocol conformance.

Another option would be to create a protocol of all the public methods/functions of a class and then use protocol composition, but that's not necessary in Obj-c so I don't think it should be needed in Swift.

Kallikrates fucked around with this message at 18:56 on Aug 3, 2014

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

Kallikrates posted:

The example is derived..., the object implementing the protocol is not a plain NSObject. On the API consumer side, knowing its class X implements protocol Y is helpful when you want to call functions of class X and protocol Y without having to cast back and forth between "id<Foo>" and "X" when I get back to my computer with Xcode Beta ill try (protocol<NSObject, Foo>) but I don't think its a solution because the NSObject protocol only exists for base NSObject, not subclasses,

Another example:
code:
extension CustomNSOperationClass {
  func returnsSomethingThatConforms() -> (CustomNSOperationClass, FooProtocol) {

  }
}
//given the return type no as? should be needed
CustomNSOperationClass.returnsSomethingThatConforms().start().fooProtocolMethod()
The major use case is extensions/categories that add protocol conformance.

Another option would be to create a protocol of all the public methods/functions of a class and then use protocol composition, but that's not necessary in Obj-c so I don't think it should be needed in Swift.

First you don't use NSObject, you use NSObjectProtocol because in Swift you can't name them the same thing since the type names would be ambiguous.

Second, this works just fine assuming Foo is a protocol:

code:
func returnConformable() -> (protocol<NSObjectProtocol, Foo>)
And indeed, Swift will infer that the return type supports all the properties and methods/selectors on both.

If you are talking about identifying the return type as being an instance of class X while also implementing protocol Y, then you can use a generic method:

code:
func returnConformable<T:MyClass where T: Foo>() -> T
But you'll have to declare MyClass' conformance to Foo or it won't compile, in which case you already have the definition of MyClass visible and it becomes pointless.

Glimm
Jul 27, 2005

Time is only gonna pass you by

Did += for Array just die with the new beta?

:cry:

Axiem
Oct 19, 2005

I want to leave my mind blank, but I'm terrified of what will happen if I do
Jeeze, Apple wasn't kidding when they talked about possibly changing the syntax.

It's at the moment quite annoying to have to do this in every single one of my classes that inherits from UIView:

code:
    required init(coder aDecoder: NSCoder!) {
        fatalError("NSCoding not supported")
    }
This would be a little better if when I got the message that my class doesn't implement all my superclass's required members, I had a way of seeing what I don't actually implement.

I'm just curious if this is overzealousness at the moment, or how this whole "required" business is going to shake out.

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe
I warned Doug this would happen. :argh:

That particular diagnostic enhancement is obvious and coming. We may also need to add a simple way to say that you don't implement something.

But yes, we are quite serious about breaking existing code if we think it makes the language better, and we plan to continue breaking things all the way to 2.0 and possibly beyond (although we hope to at least substantially automate updating from real released versions).

dizzywhip
Dec 23, 2005

Axiem posted:

This would be a little better if when I got the message that my class doesn't implement all my superclass's required members, I had a way of seeing what I don't actually implement.

You can, you just have to expand the error in the issue navigator.

Adbot
ADBOT LOVES YOU

Kallikrates
Jul 7, 2002
Pro Lurker
Interacting with API that require plist types is making my beautiful swift code look horrifying.

Edit: actually looks like it might have been due to using an enum.case as a key (even though it was an NSString type)

Kallikrates fucked around with this message at 15:37 on Aug 6, 2014

  • Locked thread