benleggiero
12/10/2017, 2:23 AMumar
12/10/2017, 3:19 AMbenleggiero
12/11/2017, 1:37 AM// My idea in Kotlin:
context.drawImage(method = ImageDrawMethod.sourceAtop)
context.drawImage(method = .sourceAtop)
context.fillPath(color = Color.red) // Also note that this reads weird in English: "Context, fill path with color color red"
context.fillPath(color = .red) // Versus "Context, fill path with color red"
listOfDoubles.reduce(operation = Double::plus)
listOfDoubles.reduce(operation = .plus) // Ideally, this might even become `listOfDoubles.reduce(operation = +)`, but that's unrelated to this proposal :)
GrandparentClass.takeGrandchild(GrandparentClass.ParentClass.MyClass.predefined)
GrandparentClass.takeGrandchild(.predefined)
path.lineTo(Point.zero)
path.lineTo(.zero) // here's it's obvious what's required and what's going to happen. When constructing a path, move to and create lines to points. No other type makes sense to go here, so the type should be omitable
Note that in all of these examples, the intelligent omission of the class name does not make it more ambiguous. The type is easily implied, both by the compiler and by the human.
This is especially useful when you want to make a bunch of predefined instances of a class. Swift does this in many places, most notably with Notification.Name
, to great effect, wrapping what used to be string literals inside a semantic class:
// Swift code:
// Swift 2 did it in one of these ways, where `post` took a `String`:
NSNotificationCenter.defaultCenter().post("SomeMagicalStringYouMustKnow")
NSNotificationCenter.defaultCenter().post(SomeGlobalConstantInASeaOfGlobalConstants)
NSNotificationCenter.defaultCenter().post(MyUtilities.MyNotificationNameConstant)
// Swift 3 did it like this, taking advantage of extensions, strong typing, and implicit member expressions. `post` takes a `Notification.Name`:
<http://NotificationCenter.default.post|NotificationCenter.default.post>(.constantFromFoundation)
<http://NotificationCenter.default.post|NotificationCenter.default.post>(.myNotificationNameConstant)
<http://NotificationCenter.default.post|NotificationCenter.default.post>(.init("SomeNotificationForWhichIDontWantAConstant"))
// The constants are declared like this:
public extension Notification.Name {
public static let myNotificationNameConstant = Notification.Name("myNotification")
}
If Kotlin allowed this, then we could combine it with the IDE's autocomplete give a list of all valid values once the dot is typed. This eliminates a lot of useless/redundant code (like the name of a class that you already typed on the same line), makes it much more clear what goes where (turns magic numbers/strings/etc. and global variables into semantic values in predictable places), and reduces coding/reading effort (99.9% of the time I either intuitively know what type is going there, or I don't care; either way I don't need to see or write the type. In the remaining 00.1%, the type doesn't have to be omitted).gildor
12/11/2017, 3:29 AMcombine it with the IDE’s autocomplete give a list of all valid values once the dot is typedLike IDE-only autocompletion feature it looks not bad actually. btw we already have smart autocomplete that offers you only valid values, but maybe
dot
autocomplete would be helpful, especially with such context, because now if you start dot
you don't get any helpful resultsgildor
12/11/2017, 3:33 AMgildor
12/11/2017, 3:34 AMlistOfDoubles.reduce(operation = .plus) // Ideally, this might even becomeDon’t like this example with method reference, too ambiguous for me
benleggiero
12/11/2017, 11:11 PMplus
operator that took 2 `Double`s and returned a Double
, it wouldn't've compiled in the first place.