[swift-evolution] [Proposal] Scoped import (of weirdly overladed logical operators)
Marcel Jackwerth
marceljackwerth at gmail.com
Fri Jan 8 04:41:53 CST 2016
Sorry in advance for that slightly esoteric case.
>From a quick survey of peers an overloaded == which returns some other
type than Bool isn't desirable (same goes for >=, <=, !=, &&, ||) and
should be forbidden (this would be the counter-proposal to this).
However it allows interesting DSLs like creating layout constraints
with https://github.com/indragiek/SwiftAutoLayout
let layoutConstraint = view1.al_width == view2.al_width * 0.5 + 10.0
Allowing to import these 'conflicting' operators closely to where the
DSL is actually used could hint other developers to that DSL and
wouldn't pollute the global scope of the ViewController for example.
I thought about a syntactical solution quite a bit but I failed to
find something that looked right. So here's a syntax draft which is
exceptionally wrong:
import SwiftAutoLayout without fancy operators
func compareSomething() -> Bool {
return view1.al_width == view2.al_width // will fail since not
Equatable, but al_width is known
}
func createConstraints() -> [NSLayoutConstraints] {
import fancy operators from SwiftAutoLayout
return [view1.al_width == view2.al_width] // succeeds
}
OR (probably an implementation nightmare)
func createConstraints() -> [NSLayoutConstraints] {
import SwiftAutoLayout
return [view1.al_width == view2.al_width]
}
Opinions against the proposal:
- Module devs shouldn't overload those operators and modify the return
type but use ====, !===, <==, ==>, &&&, ||| instead - Swift should
prevent that devs from doing this then.
- Devs will learn over time that (==, <=, >=, !=) are just the same as
+ and - and will have a return type depending on the arguments.
- Module devs should keep operators in submodules. (But then get
cyclic dependencies if they want to use these operators in the core
themselves.)
- Users of DSL modules should move the DSL dependent code into a
different file where it isn't mixed with code that doesn't need the
DSL.
Releated are the explicit includes and excludes when importing Modules
in the "Beef Up Import" thread
https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20151221/004553.html
More information about the swift-evolution
mailing list