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
jawbroken
Aug 13, 2007

messmate king

fleshweasel posted:

Is it because declaring a variable of a strict type using "let" will prevent mutation of the "var" members of the struct anyway, thus making "let" properties on a struct pointlessly strict?

Partially, but mainly because the semantics of value types make mutating a property and replacing the whole struct with a new one equivalent. You can even reassign self explicitly in a mutating function on a struct. For a reference type you might decide to have immutable properties to make sharing/concurrency less perilous, etc, but for a value type they should be very rare.

Axiem posted:

The thing is, this breaks encapsulation; now whoever wants to create a struct with one variable different has to know how that variable is stored internal to the struct.

Not really, because you can replace a stored property with a computed property without having to change the call sites, just as you can change your internal storage while keeping the same initialisers in your example.

Axiem posted:

Doing it with the withX functions also gives me the ability to pause anywhere in the chain and capture the struct, do some more work and capture that, and possibly run comparisons on the two, especially around computed values. The Star example is admittedly a little weak in that arena, because they're effectively static in my game.

If I understand this correctly, you can do that anyway. Make a copy of the struct, directly perform whatever mutations you like, then compare the two. Everything will work correctly even if you are mutating structs that are properties of other structs. Nothing about your withX functions is special in that regard.

Adbot
ADBOT LOVES YOU

Axiem
Oct 19, 2005

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

jawbroken posted:

If I understand this correctly, you can do that anyway. Make a copy of the struct, directly perform whatever mutations you like, then compare the two. Everything will work correctly even if you are mutating structs that are properties of other structs. Nothing about your withX functions is special in that regard.

True; I just prefer to implicitly copy the struct rather than explicitly.

I understand the point you're making, especially since as value types structs already do the copies as necessary. It's mostly that something bothers me about letting an external object manipulate an instance of the struct by directly modifying the properties, instead of going through a function interface. That and my default is to always write "let" unless there's a compelling reason to write "var", which is probably an overreaction to seeing people make things mutable way too often.

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe
It's not at all ridiculous to have properties on a value type that don't expose setters. It might not make sense to independently modify that property, and you might want to push people towards some higher-level interface. For example, if you're going to store points using polar coordinates, it's not obvious that you should allow the Cartesian coordinate projections to be individually set; among other things, that's a really expensive operation compared to setting them both simultaneously. (My specific advice here is that this kind of representation difference is probably too central to pretend that you can reasonably abstract over it. An algorithm written to expect one representation is going to behave like poo poo given the other. You should probably just have two different point types and provide easy conversions back and forth.)

Instinctively making everything immutable is definitely a sign of importing philosophy from other languages, though. The most important thing about code is your ability to understand what it's doing and why. Local mutable state doesn't create significant new challenges as long as you can still easily find everything that modifies or depends on a variable. (There are common objections to this argument like "but mutable variables can be modified by some arbitrary, obscure branch of the function!". The counter-argument is that that modification is happening for a reason: there is code that needs to use the modified value, not the original. Removing mutation doesn't eliminate that need, it just forces the value to be propagated manually out of that obscure branch and bound to a new name, achieving nothing other than making it harder to recognize the link between the old and new values.) Nor is it troublesome to pass a variable down to be mutated when it's obvious that that's what's happening. The problem with mutation comes when something can be modified outside of the immediate context, e.g. because it's global or otherwise shared state. But value types don't inherently present you with that problem, and so there's no reason to reflexively limit mutation in them.

toiletbrush
May 17, 2010

rjmccall posted:

Instinctively making everything immutable is definitely a sign of importing philosophy from other languages, though.
That's exactly why I'm doing it here...I got inspired by Eric Lippert's current Ocaml/ZMachine blog to try and write a simple cpu emulation using a similar functional style. I'm just interested in seeing what sort of code and design I end up with if I'm really hardcore/overly explicit about immutability. So far its led to some nice ideas I wouldn't have thought of if I let myself use var, but some of it is awkward as hell...

On a similar note, are there any plans to have a 'strict' version of typealias? Or is it just a bad idea?

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe
You mean something like Haskell newtype?

toiletbrush
May 17, 2010
I don't really know Haskell at all but it looks like it, yeah...so having...
code:
typealias Foo = Int
...and passing an Int to a method expecting a Foo would cause a compiler error. I could wrap Int, but I like the cheapness of typealias.

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe
You can make an enum with one element pretty easily.

The main (very large) usability gap of that vs. newtype is the ability to derive protocol conformances easily.

Simulated
Sep 28, 2001
Lowtax giveth, and Lowtax taketh away.
College Slice
I just wanted to say kudos to the whole Swift team regarding swift-evolution and the development process. It's awesome watching the discussions and the progress of the entire project.

Kallikrates
Jul 7, 2002
Pro Lurker
Anyone come across a good guide on getting parts of swift(oss) building locally? Seems like there is no consolidated getting started guide.

Fresh clones off master and my build environment all pointing to lated Dev snapshots everything seems to be a weird mix of swift 2 and swift 3 style API that doesn't build.

Nothing really mentioned If I had to build swift 3 off master and point my toolchain at that product before I could build XCTest or SwiftPM. Everything just mentioned using the Dev snapshots (I think since they are dated 3/1 they predate the swift3 api?)

Posted to some mailing lists. So I'll see whats up eventually I hope.

Kallikrates
Jul 7, 2002
Pro Lurker
Looks like I picked a poor weekend to try this.
Foundation master was migrated to swift 3 api, but looks like the documentation/script to build the v3 toolchain is locked away in a PR. Once a snapshot is released there shouldn't be an issue I think.

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe
Yeah, a lot of library things are a mess right now. Trust me, it's worse internally. No good way to do a massive library source break.

Doctor w-rw-rw-
Jun 24, 2008
https://twitter.com/ashfurrow/status/715941778621206529

ultramiraculous
Nov 12, 2003

"No..."
Grimey Drawer

:drat:

Doh004
Apr 22, 2007

Mmmmm Donuts...

Yeah this owns.

n4
Jul 26, 2001

Poor Chu-Chu : (
I'm a somewhat new developer (bootcamp grad) and decided to start learning Swift recently. Uhm it's awesome. Coming from Ruby and JavaScript it feels really nice and very easy to learn. Gonna make that mobile money.

pokeyman
Nov 26, 2006

That elephant ate my entire platoon.
I only noticed today that the Swift Package Manager will generate you an Xcode project (as of a month and a half ago, I'm slow ok). You do that, flip your toolchain in Xcode preferences, and now you've got a real nice autocompleting environment for your Swift development needs.

Definitely curious to see how Xcode 8 interacts with SPM. I'm tentatively excited to never use CocoaPods again.

lord funk
Feb 16, 2004

pokeyman posted:

I only noticed today that the Swift Package Manager will generate you an Xcode project (as of a month and a half ago, I'm slow ok). You do that, flip your toolchain in Xcode preferences, and now you've got a real nice autocompleting environment for your Swift development needs.

Definitely curious to see how Xcode 8 interacts with SPM. I'm tentatively excited to never use CocoaPods again.

Can you elaborate? My tired brain is reading this like it solves the choppy / slow / missing autocomplete, is that right?

pokeyman
Nov 26, 2006

That elephant ate my entire platoon.
Doesn't solve that directly (though I'm seeing much snappier autocomplete with the trunk toolset). What it solves for me is good editor support for making things using the Swift Package Manager.

edit: previously it was just editing like a single file in Xcode outside of a project, you don't get any compiler-assisted autocomplete or inline errors.

dc3k
Feb 18, 2003

what.
So I just got this neat new job that I start in a couple of weeks. The company just recently (late last year-ish?) started doing everything Swift. That's great, but I don't know Swift at all. I tried watching those Stanford lectures but they were the most boring loving thing ever. I've been a full time iOS developer for a bunch of years, so I don't need to learn what Autolayout is or how to use a tableview. I just need to learn some Swifty stuff. What's the hot poo poo as far as books go? The OP is pretty empty.

Glimm
Jul 27, 2005

Time is only gonna pass you by

status posted:

So I just got this neat new job that I start in a couple of weeks. The company just recently (late last year-ish?) started doing everything Swift. That's great, but I don't know Swift at all. I tried watching those Stanford lectures but they were the most boring loving thing ever. I've been a full time iOS developer for a bunch of years, so I don't need to learn what Autolayout is or how to use a tableview. I just need to learn some Swifty stuff. What's the hot poo poo as far as books go? The OP is pretty empty.

Have you looked at the books Apple provides on iBooks?

https://itunes.apple.com/us/book/swift-programming-language/id881256329?mt=11
https://itunes.apple.com/us/book/using-swift-cocoa-objective/id888894773?mt=11

They're very informative. They aren't super jazzed up or anything though, I don't know how boring you might find them.

pokeyman
Nov 26, 2006

That elephant ate my entire platoon.
The Advanced Swift book from the-decreasingly-well-named objc.io sounds like it might be a good fit. Spends most of its time talking about things Swift does (well) that objc doesn't (do as well). It's up-to-date for Swift 2.2, which is nice because there's a lot of excited material out there talking about code that doesn't compile anymore.

Then it spends the rest of its time talking about things that are trivially not a problem in objc. You can probably skip those parts if you're not interested.

dc3k
Feb 18, 2003

what.
Those three links are exactly what I need. Thanks!

lord funk
Feb 16, 2004

Yeah the Swift guide from Apple is a hoot.

TheReverend
Jun 21, 2005

I'm just like forums poster 'status' above except I bought a big nerd ranch guide to swift.

It's pretty good but I have a swift observation I'd like to make.

Swift looks like it's a very versatile language and is extremely flexible and there exists opportunities to write really neat innovative code.

However....

Would you agree the potential to write godawful loving spaghetti code is now also greater? Seems like a lot of the "benefits" of swift could easily be abused and make everything worse for everybody.

Doctor w-rw-rw-
Jun 24, 2008

TheReverend posted:

Would you agree the potential to write godawful loving spaghetti code is now also greater? Seems like a lot of the "benefits" of swift could easily be abused and make everything worse for everybody.
You haven't written much Objective C++ have you

Asymmetrikon
Oct 30, 2009

I believe you're a big dork!

TheReverend posted:

Would you agree the potential to write godawful loving spaghetti code is now also greater? Seems like a lot of the "benefits" of swift could easily be abused and make everything worse for everybody.

What language features of Swift would you consider to enable spaghetti code?

lord funk
Feb 16, 2004

Doctor w-rw-rw- posted:

You haven't written much Objective C++ have you

TheReverend
Jun 21, 2005

I guess just the idea of a function, that takes another function as a parameter and returns multiple items,(one of which could be another function) blows my fragile little mind. Any one of these functions could have a nested function.

I'm trying to keep an open mind. I'm sure I'll come around to it.

lord funk
Feb 16, 2004

TheReverend posted:

I guess just the idea of a function, that takes another function as a parameter and returns multiple items,(one of which could be another function) blows my fragile little mind. Any one of these functions could have a nested function.

I'm trying to keep an open mind. I'm sure I'll come around to it.

I just recently wrote an function to ramp a value, and it takes an 'interpolationFunction' argument. That way I can slip in whichever interpolation equation I want on the fly just by feeding it the function name. It's actually pretty cool - if I had to do it in Obj-C I'd probably need to make an enum entry for each function, then switch through an exhaustive list in the method body.

Anyway it all comes down to what Spiderman's uncle said about power and responsibility, I guess.

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

TheReverend posted:

Would you agree the potential to write godawful loving spaghetti code is now also greater? Seems like a lot of the "benefits" of swift could easily be abused and make everything worse for everybody.

There are more ways that you can go out of your way to be perverse in Swift. I wouldn't say it's "easier", or at least not problematically so, because it's not like you're just throwing together a view controller and then, whoops, you accidentally slip up and define a bunch of confusing infix operators or structure all of your code into global getters. I think we've done a pretty good job of communicating how these things should be used — sparingly, and with solid justification — and so far I've been pretty satisfied with how the community has taken that to heart.

I've never found the argument very convincing that languages should limit things like this just because they could be abused. Bored programmers will always find some way to act out; if anything, letting it be more clearly "abusive" makes it harder for them to actually justify checking it in when they're done, at least in contrast to standard bored-programmer practices like writing an amazing new ten-thousand-line framework to read configuration files.

Edit: oh, I mean, higher-order programming can definitely be abused to make terrible spaghetti code, but in practice it's usually just "pass a function to another which will call it during its execution".

Echo Video
Jan 17, 2004

after working on an app where everything was based around dynamic dispatch by frankensteining selectors together with stringWithFormat, swift holds no nightmares for me

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed
Swizzling methods is far worse than anything in swift.

lord funk
Feb 16, 2004

I work exclusively in Notification-Oriented Programming.

Doh004
Apr 22, 2007

Mmmmm Donuts...
All of my code is poo poo, regardless of the language.

emoji
Jun 4, 2004
Is it possible to demangle/C-ify certain symbol names in swift? In this case I want to make a Lua-autoloadable dylib. Lua transliterates module names to symbol names to search for.

Objective-C code:
int luaopen_xx_yy_internal(lua_State* L __unused) {
  //stuff
  return 1;
}
code:
$ nm -gU ./myObjcLib.dylib
00000000000022f0 T _luaopen_xx_yy_internal
Nice.

For swift:
code:
func luaopen_xx_yy_internal(L: UnsafeMutablePointer<lua_State>) -> Int32 {
  //stuff
  return 1;
}
code:
$ nm -gU ./mySwiftLib.dylib
0000000000003e80 T __TF3sss23luaopen_xx_yy_internalFGSpVSC9lua_State_Vs5Int32
...
0000000000001220 T _main  //implicit main wrapping contents?
I mean, manually calling package.loadlib('thing.dylib','_TF3sss23luaopen_xx_yy_internalFGSpVSC9lua_State_Vs5Int3') after inspecting the dylib works, but that breaks the automagical lazy loading in the context, and it'd be nice to designate a symbol name.

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe
Swift uses, or will use, a different calling convention from C, so things like that are not reliable. That said, while we want to have a feature like "expose this as a C function", currently we don't, so that might still be your best bet. When we add the feature, you can move to it and your life will just get better. (The feature will, of course, promise that the exported symbol matches the C conventions for its type.)

emoji
Jun 4, 2004
I guess that would explain why 'force-loading' the raw symbol name randomly hard crashes (Xcode, not the actual process, heh) from one minute to the next (but works most times)? I'd been looking elsewhere.

(Xcode seems mostly unharmed).
code:
Crashed Thread:        24

Exception Type:        EXC_BAD_ACCESS (Code Signature Invalid)
Exception Codes:       0x0000000000000032, 0x000000011c25b000
Exception Note:        EXC_CORPSE_NOTIFY

kernel messages:
-1 sec           	CODE SIGNING: cs_invalid_page(0x100789000): p=16461[llvm-nm] final status 0x2000000, allowing (remove VALID) page
-1 sec           	CODE SIGNING: cs_invalid_page(0x11c25b000): p=15729[Xcode] final status 0x3004200, denying page sending SIGKILL
-1 sec           	CODE SIGNING: process 15729[Xcode]: rejecting invalid page at address 0x11c25b000 from offset 0x0 in file "/../file.dylib" (cs_mtime:1462979447.0 != mtime:1462980149.0) (signed:1 validated:1 tainted:1 wpmapped:0 slid:0)

VM Regions Near 0x11c25b000:
    CoreAnimation          000000011c25a000-000000011c25b000 [    4K] r--/r-- SM=PRV  
--> mapped file            000000011c25b000-000000011c261000 [   24K] r--/rwx SM=COW  
    CoreAnimation          000000011c261000-000000011c264000 [   12K] rw-/rwx SM=PRV  

Thread 24 Crashed:
0   com.apple.LLDB.framework      	0x00000001119f1b38 lldb_private::DataExtractor::GetU32(unsigned long long*) const + 44
1   com.apple.LLDB.framework      	0x0000000111b1e0d9 ObjectFileMachO::ParseHeader() + 93
2   com.apple.LLDB.framework      	0x0000000111b1c3a0 ObjectFileMachO::CreateInstance(std::__1::shared_ptr<lldb_private::Module> const&, std::__1::shared_ptr<lldb_private::DataBuffer>&, unsigned long long, lldb_private::FileSpec const*, unsigned long long, unsigned long long) + 276
3   com.apple.LLDB.framework      	0x0000000111bed070 lldb_private::ObjectFile::FindPlugin(std::__1::shared_ptr<lldb_private::Module> const&, lldb_private::FileSpec const*, unsigned long long, unsigned long long, std::__1::shared_ptr<lldb_private::DataBuffer>&, unsigned long long&) + 1536
4   com.apple.LLDB.framework      	0x0000000111a1c046 lldb_private::Module::GetObjectFile() + 240
5   com.apple.LLDB.framework      	0x0000000111a1ffd6 lldb_private::ModuleList::GetSharedModule(lldb_private::ModuleSpec const&, std::__1::shared_ptr<lldb_private::Module>&, lldb_private::FileSpecList const*, std::__1::shared_ptr<lldb_private::Module>*, bool*, bool) + 876
6   com.apple.LLDB.framework      	0x0000000111c3f9fb lldb_private::Target::GetSharedModule(lldb_private::ModuleSpec const&, lldb_private::Error*) + 577
7   com.apple.LLDB.framework      	0x0000000111b06c64 DynamicLoaderMacOSXDYLD::FindTargetModuleForDYLDImageInfo(DynamicLoaderMacOSXDYLD::DYLDImageInfo&, bool, bool*) + 308
8   com.apple.LLDB.framework      	0x0000000111b09d37 DynamicLoaderMacOSXDYLD::AddModulesUsingImageInfos(std::__1::vector<DynamicLoaderMacOSXDYLD::DYLDImageInfo, std::__1::allocator<DynamicLoaderMacOSXDYLD::DYLDImageInfo> >&) + 483
9   com.apple.LLDB.framework      	0x0000000111b08452 DynamicLoaderMacOSXDYLD::AddModulesUsingImageInfosAddress(unsigned long long, unsigned int) + 488
10  com.apple.LLDB.framework      	0x0000000111b08138 DynamicLoaderMacOSXDYLD::NotifyBreakpointHit(void*, lldb_private::StoppointCallbackContext*, unsigned long long, unsigned long long) + 666
11  com.apple.LLDB.framework      	0x000000011197ab24 lldb_private::BreakpointOptions::InvokeCallback(lldb_private::StoppointCallbackContext*, unsigned long long, unsigned long long) + 48
12  com.apple.LLDB.framework      	0x000000011197803c lldb_private::BreakpointLocation::InvokeCallback(lldb_private::StoppointCallbackContext*) + 82
13  com.apple.LLDB.framework      	0x000000011197884c lldb_private::BreakpointLocation::ShouldStop(lldb_private::StoppointCallbackContext*) + 116
14  com.apple.LLDB.framework      	0x00000001119794d3 lldb_private::BreakpointLocationCollection::ShouldStop(lldb_private::StoppointCallbackContext*) + 77
15  com.apple.LLDB.framework      	0x00000001119826b8 lldb_private::BreakpointSite::ShouldStop(lldb_private::StoppointCallbackContext*) + 48
16  com.apple.LLDB.framework      	0x0000000111c373f6 lldb_private::StopInfoBreakpoint::ShouldStopSynchronous(lldb_private::Event*) + 294
17  com.apple.LLDB.framework      	0x0000000111c4ef79 lldb_private::Thread::ShouldStop(lldb_private::Event*) + 709
18  com.apple.LLDB.framework      	0x0000000111c54993 lldb_private::ThreadList::ShouldStop(lldb_private::Event*) + 687
19  com.apple.LLDB.framework      	0x0000000111c220f1 lldb_private::Process::ShouldBroadcastEvent(lldb_private::Event*) + 431
20  com.apple.LLDB.framework      	0x0000000111c1f1ae lldb_private::Process::HandlePrivateEvent(std::__1::shared_ptr<lldb_private::Event>&) + 204
21  com.apple.LLDB.framework      	0x0000000111c22aee lldb_private::Process::RunPrivateStateThread(bool) + 1004
22  com.apple.LLDB.framework      	0x0000000111c22317 lldb_private::Process::PrivateStateThread(void*) + 19
23  libsystem_pthread.dylib       	0x00007fff93bc999d _pthread_body + 131
24  libsystem_pthread.dylib       	0x00007fff93bc991a _pthread_start + 168
25  libsystem_pthread.dylib       	0x00007fff93bc7351 thread_start + 13

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe
No, that actually doesn't seem like a likely culprit for that, actually. I have never seen that crash before.

emoji
Jun 4, 2004
Whatever it is, Xcode crashes very hard at the moment I attempt to package.loadlib using the raw symbol, tho not reliably. Somehow it doesn't kill the process itself, which does 'successfully' return a function in the lua interpreter (tho invoking that function will finally crash the app). If it doesn't kill Xcode, the received function is fine to call as well.

Any timeline at all on the C interface?

Along these lines I've been wondering, is swift really killing ObjC slowly? Swift seems to be able to utilize most/all of ObjC, but ObjC can only use a very limited subset of swift, and every day more swift libraries are made with seemingly nothing but more swift able to use them.

Adbot
ADBOT LOVES YOU

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

emoji posted:

Along these lines I've been wondering, is swift really killing ObjC slowly?

I sure hope so.

  • Locked thread