Bug 403777 - Suggestion: additional operator overloading
Summary: Suggestion: additional operator overloading
Status: NEW
Alias: None
Product: Xtend
Classification: Tools
Component: Core (show other bugs)
Version: unspecified   Edit
Hardware: PC Mac OS X
: P3 enhancement with 2 votes (vote)
Target Milestone: ---   Edit
Assignee: Project Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
: 405637 (view as bug list)
Depends on:
Blocks:
 
Reported: 2013-03-19 10:02 EDT by Christian Vogel CLA
Modified: 2016-02-16 01:16 EST (History)
7 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Christian Vogel CLA 2013-03-19 10:02:48 EDT
Some suggested additional operator overloadings:

a:b   // would allow JSON like DSL more easily, amongst others
a|b   // allows for piping syntax, would be really nice
a?    // would be very nice for option type programming for example
a?b   // nice in combination with a?
a!    // just nice, no real case yet :-)
a%    // 50% :-)
a@b   // for addressing DSL
a::b  // addressing
@a    // addressing
~a    // addressing
a~b   // addressing
a\b   // addressing
a[b]  // now that would be tough.. but powerful. array like objects possible

However not all fit the Xtend syntax, so needs checking what is feasible.
Comment 1 Sven Efftinge CLA 2013-04-15 02:15:16 EDT
*** Bug 405637 has been marked as a duplicate of this bug. ***
Comment 2 Sven Efftinge CLA 2014-02-03 10:55:03 EST
(In reply to Christian Vogel from comment #0)
> a:b   // would allow JSON like DSL more easily, amongst others

conflicts with several expressions (e.g switch)

> a|b   // allows for piping syntax, would be really nice

conflicts with lambdas

> a?    // would be very nice for option type programming for example
> a?b   // nice in combination with a?
> a!    // just nice, no real case yet :-)

conflicts with !a

> a%    // 50% :-)
> a@b   // for addressing DSL
> a::b  // addressing

conflicts with old-style static access

> @a    // addressing

conflicts with annotations, especially when we are going to add type annotations (JSR 308)

> ~a    // addressing
> a~b   // addressing
> a\b   // addressing
> a[b]  // now that would be tough.. but powerful. array like objects possible

conflicts with lambdas.
Comment 3 Floris van der Grinten CLA 2015-09-25 06:13:44 EDT
More suggestions:

Elvis operator variations (with a question mark), in order to be able provide more functionality, while not having to overload the original elvis operator. Like:

?%

and 

?*

And more assigning kind of operators, like:

<~

and 

~>
Comment 4 Floris van der Grinten CLA 2015-09-28 11:50:49 EDT
And, I know it wouldn't be possible right now, but maybe in the future: turning ?. into an operator.
Comment 5 Sebastian Zarnekow CLA 2015-09-28 12:01:13 EDT
(In reply to Floris van der Grinten from comment #4)
> And, I know it wouldn't be possible right now, but maybe in the future:
> turning ?. into an operator.

Could you pleas elaborate on the use case? Usually it's a bad idea to implement another semantics for an operator that is well understood and associated with a defined behavior.
Comment 6 Floris van der Grinten CLA 2015-09-29 04:41:42 EDT
(In reply to Sebastian Zarnekow from comment #5)

> Could you pleas elaborate on the use case? Usually it's a bad idea to
> implement another semantics for an operator that is well understood and
> associated with a defined behavior.

And that is the exact reason I would like to have elvis variations like ?* and ?%.

But about the null safe feature (?.), that could be used really nicely with optionals. So same semantics, but working with an implementation of optionals.
I would like to see something like this:
val Opt<String> someString = someOtherOptString?.toLowerCase // returns an optional either way

But like I said, for now having ?* and/or ?% would be really good already.
Comment 7 Mark Jeronimus CLA 2016-02-13 10:11:01 EST
I'd like to use more symbolic operators to overload and define, both binary and unary (infix, prefix)


For example, I'd like to:

e1 | e2
e1 |= e2
e1 & e2
e1 &= e2
e1 ^ e2
e1 ^= e2
e1 ^^ e2
e1 ^= e2
e1 %= e2
e1 **= e2
e1 ?:= e2
e1 !: e2
e1 !:= e2
e1 <<= e2
e1 >>= e2
e1 <<<= e2
e1 >>>= e2

Unary:

%e1
~e1
@e1

Postfix derived from prefix:

e1~~ (does: e1 = ~e1)
e1!! (does: e1 = !e1)
etc.


In general, it would be ideal if there wasn't a restriction on which symbols you can use, and for binary, how many there can be in a row. This would allow operators like %!!$~#, ∩, ≅ or ∊.

Related: #476778 #413818