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
rjmccall
Sep 7, 2007

no worries friend
Fun Shoe
There's a huge amount of bad taste and cargo-culting in the functional community. It's a "well, we're functional programmers so we're obviously a cut above the rest, now here's an unnecessary monstrosity of syntax that's worse than anything in perl" sort of thing. There is a reason that "advanced" Haskell and Scala look the way they do, and it is not because that is somehow core to the mathematical purity of the model.

Always remember that as a programmer your goal is to write good code that solves problems, not to make your job more interesting by finding clever ways to eliminate keystrokes. It's something we're all susceptible to, but it's a bad instinct.

Adbot
ADBOT LOVES YOU

ultramiraculous
Nov 12, 2003

"No..."
Grimey Drawer

rjmccall posted:

There's a huge amount of bad taste and cargo-culting in the functional community. It's a "well, we're functional programmers so we're obviously a cut above the rest, now here's an unnecessary monstrosity of syntax that's worse than anything in perl" sort of thing. There is a reason that "advanced" Haskell and Scala look the way they do, and it is not because that is somehow core to the mathematical purity of the model.

Always remember that as a programmer your goal is to write good code that solves problems, not to make your job more interesting by finding clever ways to eliminate keystrokes. It's something we're all susceptible to, but it's a bad instinct.

What you mean scalaz's ★ operator doesn't mean "monad transformer" or whatever to most people?

Look Around You
Jan 19, 2009

ultramiraculous posted:

What you mean scalaz's ★ operator doesn't mean "monad transformer" or whatever to most people?

What about the ☆ one?

e: seriously having a star as an operator is dumb enough, but then having a filled one and a hollow one as operators is even loving worse. IIRC they're related too?

Lumpy
Apr 26, 2002

La! La! La! Laaaa!



College Slice

Look Around You posted:

What about the ☆ one?

e: seriously having a star as an operator is dumb enough, but then having a filled one and a hollow one as operators is even loving worse. IIRC they're related too?

The empty star operator means you haven't favorited the left side of the expression yet.

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

How do you even type that?

Kallikrates
Jul 7, 2002
Pro Lurker
⌃⌘Space and then you select it with the mouse.

ultramiraculous
Nov 12, 2003

"No..."
Grimey Drawer

Look Around You posted:

What about the ☆ one?

e: seriously having a star as an operator is dumb enough, but then having a filled one and a hollow one as operators is even loving worse. IIRC they're related too?

Yeah they were both related to the same operation with modads. Phone posting, but I have a horrible feeling that they represented opposite prefix/postfix operator affinity (or at least there was another horrible operator that was like that).

Just because you can, doesn't mean you should.

ultramiraculous fucked around with this message at 07:09 on Feb 17, 2015

brap
Aug 23, 2004

Grimey Drawer
They implemented monads with the stupid operators because they thought it was too easy to tell what was going on.

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?

fleshweasel posted:

They implemented monads with the stupid operators because they thought it was too easy to tell what was going on.

They should've added a native concept of combining diacritics to the language to represent the functional concept of composition.

e: good luck distinguishing ± from + combined with an underbar diacritic!

Simulated
Sep 28, 2001
Lowtax giveth, and Lowtax taketh away.
College Slice
code:
infix operator !! { associativity left }

//everything is ok when this comes first
func !!<T>(left: T?, @autoclosure right:  ()->T) -> T {
    return (left != nil) ? left! : right()
}

//and this comes second; swap them around and errors abound.
func !!<T>(left: T?, right: T?) -> T? {
    return (left != nil) ? left! : right
}

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

let a = x !! y !! z
:what: Operator overloading with overloaded definitions appears to be strangely sensitive to the order of definition. Swap the two functions around and it will fail to compile. Strangely, resolution still picks the correct definition in the working case (a chain of optionals should infer an optional result type, while a chain terminating in a non-optional should infer a non-optional result type). Found this when verifying bugs against 1.2; this was filed as rdar://17842481 during the original beta.

On another note, is there a story forthcoming for handling visibility for unit testing? Now that we can't make extensions visible for types defined in other modules, it further complicates the unit testing story. The whole rationale for how public/private work is completely smashed because I have to consider public visibility for everything I do, otherwise none of it is visible to the unit test project.

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe
That sounds like a bug. (Also, that's just an example, right? Because ?? already does that; it didn't always, so I wanted to make sure you knew.)

I agree that ideally you shouldn't have to spuriously mark stuff public just to get unit tests to work. I know we've talked about it, but I don't know what the current state of the fix is. Are your unit tests really in a completely different project, not just a different target?

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

rjmccall posted:

That sounds like a bug. (Also, that's just an example, right? Because ?? already does that; it didn't always, so I wanted to make sure you knew.)

I agree that ideally you shouldn't have to spuriously mark stuff public just to get unit tests to work. I know we've talked about it, but I don't know what the current state of the fix is. Are your unit tests really in a completely different project, not just a different target?

Yeah, I just had that handy from the original beta bug report (before ?? worked like it does now).

Maybe I'm missing something but for frameworks and mixed ObjC/Swift projects the unit tests pull in the project code as a module so anything not public isn't visible.

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

Ender.uNF posted:

Maybe I'm missing something but for frameworks and mixed ObjC/Swift projects the unit tests pull in the project code as a module so anything not public isn't visible.

Yeah, that's what we still need to fix. But when we fix it, I wouldn't be surprised if the tests need to be in the same project. Maybe I shouldn't speculate; I haven't been following that discussion very closely.

Simulated
Sep 28, 2001
Lowtax giveth, and Lowtax taketh away.
College Slice
Anyone have a cleaner method for initializing values related to a property where you need to respond in both didSet and init? Either I end up just saying gently caress it and making the related fields implicitly unwrapped optional (which feels gross) or I have to do this stupid dance:

code:
    private var _settings:EffectMode
    var effect:EffectInstance {
        didSet { didSetEffect(oldValue) }
    }
    //pointless trampoline function
    private func didSetEffect(oldValue:EffectInstance?) {
        //do some related setup crap here to change _settings among other things
    }
    
    init(effect:EffectInstance) {
        self.effect = effect
        _settings = EffectViewModel.NormalSettings //pointless and possibly totally wrong!
        super.init()
        didSetEffect(nil) //can't call this before super.init(), which is actually the opposite of safe initialization!
    }
In this case, Swift's attempt to make initialization safe actually fights me and make things less safe. If this class had a superclass then some fields may not be nil, but they sure don't have legitimate values. Or I can just duplicate the code by copy-paste :v: (can't call functions on self until after super init :v: :v: )

I understand why initialization works this way but I do wonder if we could have a way to trigger willSet/didSet prior to calling super? Perhaps if they were declared with explicit optional parameters or something?

code:
    var effect:EffectInstance {
        @initable didSet(oldValue) {
            //oldValue is optional when declared @initiable and will be nil during init
        }
    }

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe
It's an interesting idea. We could have an attribute that makes initialization call didSet and then impose initializer-like restrictions on didSet, i.e. disallowing the oldValue parameter and only allowing general use of the current property. A more convenient alternative to extracting that logic into a global or static helper function and calling it in two places. Worth a radar.

In the meantime, you should be able to put your helper logic in a global function and call it at the right time.

MrSaturn
Sep 8, 2004

Go ahead, laugh. They all laugh at first...
Is there an equivalent to Objective-C's "class" data type in swift? I'm having trouble finding anything that mirrors the type used in this function declaration:

code:
- (void)registerNSManagedObjectClassToSync:(Class)aClass;
context: here


e: or if any of you wizards know a better way to accomplish something along the lines of what they're doing in that post (in swift, with Parse.com in my case), please chime in. This is exhausting to figure out ;)

MrSaturn fucked around with this message at 23:42 on Feb 24, 2015

MrSaturn
Sep 8, 2004

Go ahead, laugh. They all laugh at first...
Replying to myself. Based on documentation, I think I'm just looking for AnyClass, which seems strange.

https://developer.apple.com/library...object_getClass

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

rjmccall posted:

It's an interesting idea. We could have an attribute that makes initialization call didSet and then impose initializer-like restrictions on didSet, i.e. disallowing the oldValue parameter and only allowing general use of the current property. A more convenient alternative to extracting that logic into a global or static helper function and calling it in two places. Worth a radar.

In the meantime, you should be able to put your helper logic in a global function and call it at the right time.

Filed, radar://19948325 . Please excuse the spelling errors, I'm on muni to NSCoder night and this iPad has developed some "interesting" autocorrect habits.

alanthecat
Dec 19, 2005

Posting on the off chance there's anyone from Dublin reading -- there's a Swift meet today and the next couple of Wednesdays in Citi Bank on the quays.

https://twitter.com/SwiftlyDublin

The Eventbrite page doesn't seem to be working this week so just show up for 7pm and ask for Aman.

And the Xcake group covered Swift a bit earlier in the month and undoubtedly will again.

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

Found three new issues in the beta. I'd file radars but bugreport.apple.com just redirects me back to the login page.

1. If a function taking a closure contains only a call to another function taking a closure that has an error (e.g. UInt changed to Int in the closure signature), Swift will mis-attribute the error to the outer closure. If you stick a let statement or something in there then it correctly identifies the inner call as being passed an incorrect closure.

2. The Xcode Fix-it thing has a lag time for updating; if you apply a fix-it (e.g. converting "as" to "as!"), the following fixits will be off by... you guessed it! one character! They're obviously using cached text positions that don't adjust for the inserted character, so it helpfully offers to replace " a" with " a!".

3. The Swift syntax upgrade tool does not upgrade the unit tests


P.S. The Xcode release notes link on the official Swift blog give an unauthorized access page because it links to the old version of the release notes.

Simulated fucked around with this message at 18:48 on Feb 26, 2015

sarehu
Apr 20, 2007

(call/cc call/cc)
Sheesh, talk about software quality at Crapple. They should've implemented Swift in a memory-safe language like Rust, or PHP.

pokeyman
Nov 26, 2006

That elephant ate my entire platoon.

sarehu posted:

Sheesh, talk about software quality at Crapple. They should've implemented Swift in a memory-safe language like Swift.

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed
Shortly after Swift was first announced I had a very dumb argument with a person who was convinced that the Swift compiler would be both faster and more reliable if they'd written just enough of it in C++ to bootstrap and then wrote the real thing in Swift.

ultramiraculous
Nov 12, 2003

"No..."
Grimey Drawer
Rewrite the whole thing in WebObjects.

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

Self-hosting is righteous. Or are you saying that if I want more reliable code I should choose C++ over Swift?

Axiem
Oct 19, 2005

I want to leave my mind blank, but I'm terrified of what will happen if I do
As I understand it, the sorts of things a language needs to be able to do in order to self-host don't have much in common with the sorts of things a language needs to do in order to be generally usable in developing GUI-having applications. It therefore becomes a matter of time and priorities: do you spend the time to self-host because...reasons; or do you spend the time building a language that people can use to create apps?

For content: How is Swift/Obj-C interoperability for reals? I have someone on my team that wants to be aggressive about writing new stuff in Swift and converting over, but I'm not sold that it's going to be smooth sailing. Was wondering what sorts of real-life experience people have had.

brap
Aug 23, 2004

Grimey Drawer
It can be bumpy. Certain things can't show up in the signatures of functions you want to expose to objective C, like tuples. You need to configure your project settings correctly if you're including a swift dylib in an objective C app project. It was painful sometimes, especially when I just couldn't tell what I was doing wrong, but in the end the expressiveness of swift made it worth it to me. And unless you have the money for a ground up rewrite, transitions have to be gradual.

I don't have experience with how swift 1.2 improves this. I would love to hear from you folks who do.

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

Axiem posted:

As I understand it, the sorts of things a language needs to be able to do in order to self-host don't have much in common with the sorts of things a language needs to do in order to be generally usable in developing GUI-having applications. It therefore becomes a matter of time and priorities: do you spend the time to self-host because...reasons; or do you spend the time building a language that people can use to create apps?

Another argument is that it can kindof misleading. The needs and priorities of someone writing a compiler can be pretty different from the needs of other programmers. I like ADTs and pattern-matching, and we have a vision for where we're going with them in Swift, and I'm not saying that they're useless for other programmers... but if we were self-hosting they'd be far more complete, by necessity, and that focus means that there would be other features that would have gotten less attention, and there would always be a tension between optimizing for our own convenience and considering the needs of the users who actually matter. There are already enough languages that excel at writing a compiler.

sarehu
Apr 20, 2007

(call/cc call/cc)
Development time would also be longer because you wouldn't have all the tools available outside the language, like rock solid editor, debugger support.

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

I had no reason to expect this to work, but taking the address of an instance property that has didSet/willSet still manages to fire the willSet/didSet on the Swift side. I don't know what trampoline magic makes that work but I like it.

Dr. McCheese
Sep 19, 2007
I ♥ Katamari.
Is there any way to specify @autoclosure for an enum payload in Swift 1.2? I assumed that the following would work, but it's treated as a syntax error:
code:
enum Foo {
    case Bar(@autoclosure _: () -> ())
}
I don't know if there are any real world reasons for wanting to do that, but I was using auto closures in generic enum payloads as a temporary workaround prior to Swift 1.2 to get around the fact that the following doesn't yet compile:
code:
enum Foo<TA, TB> {
    case A(TA)
    case B(TB)
}

Ender.uNF posted:

I'd file radars but bugreport.apple.com just redirects me back to the login page.

I had the same thing yesterday. Purging cookies for apple.com fixed it.

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

Dr. McCheese posted:

Is there any way to specify @autoclosure for an enum payload in Swift 1.2?

No. Someone was working on locking down on the number of places we accepted it — it's only supposed to be allowed in parameters, but we weren't representing it in the right way to get that behavior — and broke this pattern. We realized what we'd done, thought about it, and decided we didn't care; sorry. Just make a standalone constructor function that takes the values you want (by value!) and embeds them in some way into your enum; that's better practice anyway, since you'll actually capture the values instead of capturing the expression that computes them. While you're at it, a class is a better way to embed the values than a function.

We'll have recursive enums eventually, I promise.

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

Ender.uNF posted:

rjmccall:

I had no reason to expect this to work, but taking the address of an instance property that has didSet/willSet still manages to fire the willSet/didSet on the Swift side. I don't know what trampoline magic makes that work but I like it.

That's a general thing with Swift inout parameters: you can pass a reference to a computed property inout, and it's implemented by reading the property into a temporary, passing the address of that, and writing the value back after the call. The assumption, codified in the language rules, is that the callee won't try to keep the reference alive longer than the call, e.g. by capturing the inout parameter in a closure. Swift is still memory-safe, so the compiler isn't supposed to generate code that crashes if you violate that; but it's against the rules, and the compiler is allowed to do things that lead to unexpected behavior, like writes to the captured inout parameter "disappearing".

If the property is stored, we should be passing the actual memory backing it; doing that safely even for class properties (which can be made computed in an override) has been fun.

The C interop bit here is that we allow you to pass variables inout as a C pointer argument. Of course, the C function can do things that break memory-safety with this feature, but it's C, of course it can do that.

Dr. McCheese
Sep 19, 2007
I ♥ Katamari.

rjmccall posted:

While you're at it, a class is a better way to embed the values than a function.

Oh, of course, pointers occupy a fixed amount of space, so either wrapping values in Box<T> or using enum Foo<TA: AnyObject, TB: AnyObject> will work.
Thanks for running this thread and for providing your insights.

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

Dr. McCheese posted:

Oh, of course, pointers occupy a fixed amount of space, so either wrapping values in Box<T> or using enum Foo<TA: AnyObject, TB: AnyObject> will work.

Right. In Swift, class references are one pointer and functions are two; this might make the direct storage size of your enum slightly smaller, depending on the other cases. In both cases, a heap allocation is required.

The other advantage is that class storage is transparent. Absent heroic optimization, with a function the compiler always has to call something to get the value; with a class, the compiler can pull the storage directly out of memory, which both is faster and has a lot of nice second-order effects. (To this end, you should also make your class private or final. This shouldn't really be required — the compiler should be able to figure this out reliably even in debug builds — but for now it is.)

Doh004
Apr 22, 2007

Mmmmm Donuts...
Hey rjmccall,

How do I println the size of an object in memory? I want to print the size of my NSCache but it yells at me when I try to do a malloc_size(UnsafePointer<NSCache>(self.cache)).

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

Doh004 posted:

How do I println the size of an object in memory? I want to print the size of my NSCache but it yells at me when I try to do a malloc_size(UnsafePointer<NSCache>(self.cache)).

The direct answer is that you can make an Unmanaged reference and then get that as an opaque pointer.

The actual answer is that malloc_size doesn't do what you're hoping. It'll at best print the instance size of an NSCache object, i.e. the amount of storage needed for the ivars of NSCache and its superclasses, not the number of entries in the cache or its current capacity, and certainly not the amount of memory currently being kept alive by a reference from the cache.

Doh004
Apr 22, 2007

Mmmmm Donuts...

rjmccall posted:

The direct answer is that you can make an Unmanaged reference and then get that as an opaque pointer.

The actual answer is that malloc_size doesn't do what you're hoping. It'll at best print the instance size of an NSCache object, i.e. the amount of storage needed for the ivars of NSCache and its superclasses, not the number of entries in the cache or its current capacity, and certainly not the amount of memory currently being kept alive by a reference from the cache.

Hmm, okay. Would instruments be the true way to get the actual size of those objects than?

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe
Instruments might help. It doesn't look like NSCache has much in the way of API for it; I think you're just supposed to trust that it's doing the right thing.

Adbot
ADBOT LOVES YOU

Doh004
Apr 22, 2007

Mmmmm Donuts...

rjmccall posted:

Instruments might help. It doesn't look like NSCache has much in the way of API for it; I think you're just supposed to trust that it's doing the right thing.

Yeah, that's what I'm worried about!

But thank you for the help! I've been learning quite a bit as I reinvent the wheel and create my very own super performant (read: non-crashing) image cache.

  • Locked thread