[swift-evolution] [Proposal] Improving operator requirements in protocols
Xiaodi Wu
xiaodi.wu at gmail.com
Mon May 2 16:01:49 CDT 2016
Maybe one minimalist approach could be to have these take two arguments as
though it's an infix operator where one of lhs or rhs is Void:
T.++(&value, ()) //postfix
T.++((), &value) // prefix
On Mon, May 2, 2016 at 15:56 Dave Abrahams via swift-evolution <
swift-evolution at swift.org> wrote:
>
> on Mon May 02 2016, Tony Allevato <allevato-AT-google.com> wrote:
>
> > On Mon, May 2, 2016 at 1:20 PM Dave Abrahams via swift-evolution
> > <swift-evolution at swift.org> wrote:
> >
> > Tony, thanks for writing this up!
> >
> > on Mon May 02 2016, Tony Allevato <swift-evolution at swift.org> wrote:
> >
> > > Other kinds of operators (prefix, postfix, assignment)
> > >
> > > Static operator methods have the same signatures as their global
> > counterparts.
> > > So, for example, prefix and postfix operators as well as assignment
> > operators
> > > would be defined the way one would expect:
> > >
> > > protocol SomeProtocol {
> > > static func +=(lhs: inout Self, rhs: Self)
> > > static prefix func ~(value: Self) -> Self
> > >
> > > // This one is deprecated, of course, but used here just to serve
> as an
> > > // example.
> > > static postfix func ++(value: inout Self) -> Self
> > > }
> > >
> > > // Trampolines
> > > func += <T: SomeProtocol>(lhs: inout T, rhs T) {
> > > T.+=(&lhs, rhs)
> > > }
> > > prefix func ~ <T: SomeProtocol>(value: T) -> T {
> > > return T.~(value)
> > > }
> > > postfix func ++ <T: SomeProtocol>(value: inout T) -> T {
> > > return T.++(&value)
> > > }
> >
> > How does one distinguish between calls to a static prefix operator
> and a
> > static postfix operator with the same name?
> >
> > Ah, that's a tricky one that I don't have an immediate answer to, so I'm
> > definitely open to creative thoughts here.
>
> One possibility: just use “qualified operator” notation.
>
> lhs T.+= rhs
>
> T.++x
> x T.++
>
> > The first stab I would take at is, what if we included the token
> "prefix" or
> > "suffix" before the operator name in the expression, like this?
> >
> > return T.prefix ++(&value)
> > return T.postfix ++(&value)
> >
> > But that could start to look like an invocation of "++" on a static
> property
> > "T.prefix". I haven't dug into the parser to determine if that would
> even be
> > feasible or not.
>
> These are not unreasonable either:
>
> return prefix T.++(&value)
> return postfix T.++(&value)
>
> --
> Dave
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160502/3b022e16/attachment.html>
More information about the swift-evolution
mailing list