3 ms·
> Disambiguating SPM Naming Conflicts Perl 6 did some work on this. I'm not sure if it's fully supported yet, but the design docs[1] go into detail on how it'
by kbenson 11y ago
> Disambiguating SPM Naming Conflicts
Perl 6 did some work on this. I'm not sure if it's fully supported yet, but the design docs[1] go into detail on how it's supposed to work, so it may be worth looking at.
> Method Cascades
I suspect you might find disambiguating your method/attribute access easier in your "with" syntax if you make it match what swift appears to do for anonymous variables, use $0. (pardon my unfamiliarity with Swift, I may be mistaken on some obvious things).
E.g. Instead of
with let task = NSTask() {
launchPath = "/usr/bin/mdfind"
arguments = ["kMDItemDisplayName == *.playground"]
standardOutput = pipe
launch()
waitUntilExit()
}
do
with let task = NSTask() {
$0.launchPath = "/usr/bin/mdfind"
$0.arguments = ["kMDItemDisplayName == *.playground"]
$0.standardOutput = pipe
$0.launch()
$0.waitUntilExit()
}
That way if you want to use some non-related variabled within the block, or the output of an attribute or method as the input of some other attribute or method, it's not ambiguous.
1: http://design.perl6.org/S11.html#Versioning http://design.perl6.org/S11.html#Versioning
- mikeash 11y agoYou can nearly implement your version of `with` today: func with<T>(value: T, f: T -> Void) -> T { f(value) return value } Which you could then use like: let task = with(NSTask()) { $0.launchPath = "/usr/bin/mdfind" $0.arguments = ["kMDItemDisplayName == *.playground"] $0.standardOutput = pipe $0.launch() $0.waitUntilExit() } The `let task =` part would be optional, and you could leave it off if you don't need to use the variable later.
- kbenson 11y agoOkay, then I don't really see a reason for Erica's syntax,which is special and either ambiguous or limited. How do you know whether you are calling a method on the given object, or a plain function? How to you know whether you are using an attribute of the object or a variable? Just rely on scope? That seems far more problematic and error prone for the small convenience of not typing three more characters with object access. Your solution is actually simple enough that I'm not sure there needs to be a change to the language, unless there's some special behavior they can and should impart that we aren't thinking if. That said, I don't write swift, so feel free to take my opinion for whatever you think it's worth. :)
- chris_7 11y agoI'm not sure if the function call could be optimized out across module boundaries, that's really the only justification for this to be dedicated syntax I can see. It's somewhat obsoleted in Swift 3 regardless as SPM uses static linking, where it should be able to be optimized.
- mpweiher 11y agoIn Objective-Smalltalk: task ← NSTask new. task setLaunchPath:'/usr/bin/mdfind'; setArguments:#('kMDItemDisplayName == *.playground'); launch; waitUntilExit. Aside from the ';', no special syntax needed. And unlike other Smalltalks, there is a parallel with '|' for composition instead of cascade (so send the next message to the result instead of the receiver of the first expression), which helps disambiguating keyword syntax without requiring parentheses: 'a' stringByAppendingString:'b' | stringByAppendingString:'c'. This is especially useful when building expression incrementally, as it doesn't require going back to the start of the expression. Of course, stringByAppendingString: isn't such a good example because the ',' message makes this quite a bit more compact: 'a','b','c'