1

Edit: I have rewritten the question to hopefully make it more understandable.

I do not want to overload!

If you have the following code:

ImmutableObject mutableReference = new ImuttableObject();

mutableReference = mutableReference.doStuff(args);

Can a compile time or pre-compile time process replace defined text formats? For example:

DEFINE X.=Y AS X = X.Y

could replace
mutableReference .= doStuff(args) with mutableReference = mutableReference.doStuff(args);

So some process knows that the code before ".=" is X and after is Y. Similar to syntactic sugar, before compiling or during, just replace X.=Y with X = X.Y.

Below is the old version of the question.

I have the following "form" of code for lack of a better word.

turnStates = turnStates.add(currentState); // log end of turn state.

            //turnStates.=add(currentState);
            //turnStates=.add(currentState);

Where turnStates can be a reference to any immutable object. I would like it to look like the code commented out or similar. Much like integers that have ++ and += I'd like a way to write my own for my immutables.

I think I recall some pre-processor stuff from C++ that I think could replace predefined text for code snippets. I was wondering if there was a way in java to define a process for replacing my desired code for the working code at compile time.

I'm sure you could make the IDE do it, but then you can't share the code with others not running a pre-configured IDE.

Edit:

turnStates is immutable and returns a different object on a call to add. It is test code and I have my reasons why a list, or as it is at the moment acting more like a stack, is immutable. Irrelevant for the question as I could simply replace it with player = player.doSomething(args) where doSomething(args) returns a Player instance. Player is just a small part of the model and is costless to be immutable.

I know Overloads and syntax can't be changed in Java. As I tried to portray originally, sorry if it didn't come across this way is:

I was hoping that I wasn't aware of a syntax to do with maybe the @ sign that could replace text before compiling. So for example:

DEFINE X.=Y AS X = X.Y where X = turnStates and Y = add() in my example.

But as the answer I upvoted said. I'll check out Scala as the answer seems to be no.

Fat_Llama
  • 65
  • 10
  • 4
    Changing the syntax is not possible. Other languages allow you to overload operators, but not Java! – Jacob G. Apr 12 '17 at 00:17
  • No. In any case, unless `turnStates.add()` returns a new object, which would itself be pretty pointless, the syntax you suggest is completely pointless, as is the assignment in the original statement. – user207421 Apr 12 '17 at 00:19
  • http://stackoverflow.com/questions/10483423/java-code-transform-at-compile-time Same Question. It has no accepted answer so didn't want to flag. – Fat_Llama Apr 12 '17 at 01:24

2 Answers2

9

No. Java explicitly does not support operator overloading for user defined data types. However, scala is a JVM hosted language and does.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
1

Unlike C++,Java doesn't support operator overloading.But Scala or Groovy does. Scala can be integrated into Java but the operator overloading integration part is still not directly supported by Java as you will not be able to use the operator itself but something like @eq(...) for the "=" operator. Check this link out for a little more detail if you want to know about Scala integration into java

Bottom line: operator overloading​ is not supported by Java

And if your project requires a lot of vector addition, substraction,etc. i.e. lot of custom operators then a good suggestion would be using C# as your choice of language which is a Java like language

Shankha057
  • 1,296
  • 1
  • 20
  • 38