#Java Custom Annotations for Implicit Constructors

1 messages · Page 1 of 1 (latest)

untold geyser
#

Hello! Wanting to ask for information on this topic as I think it might be interesting to build custom implicit constructors for Java using annotations, so then I can build a class, let's say TimeStamp and have its constructor syntax be: 12:00PM instead of something like: new TimeStamp(12, 0, "PM");
So that it behaves basically like a primitive type such as the String class, which has custom syntax to define itself: "" instead of new String(...);.
Same goes for int, double, boolean, etc.
They all have unique, implicit constructors and I'd like to build my own.
I think it would be interesting to define a unique implicit constructor via annotations with a @ConstructorRule annotation
For example, TimeStamp's implicit constructor could be:@ConstructorRule(int:int (PM|AM))And a Data implicit constructor could look like: @ConstructorRule(int/int/int)(Possibly with some more rules to limit to 2 digits/2 digits/4 digits?)
Whitespace would be ignored.
So you could define primitive types as "parameters" to the implicit constructor, then characters/strings (such as :/PM/AM in TimeStamp or f in C#'s float)

Could be a cool feature for Java, but it could also make Java fairly unreadable if one is not aware of the added syntax rules made by the @ConstructorRule annotation
It's not really super useful per-say and doesn't add anything that can't be done with vanilla constructors, but it would look cool!

Is this possible with the current state of annotations? How would I go about starting to structure this annotation? What should I look out for when starting this project?

restive cliffBOT
#

<@&987246841693360200> please have a look, thanks.

vital saffron
#

Unless you want to hack into the compiler (requiring separate support for ecj), no. Annotation processors can only create new files, so they can't even modify existing classes, let alone add new syntax.

#

And if you hack into the compiler, then you aren't really using java anymore, but a custom extension of it

untold geyser
#

So how does the @FunctionalInterface annotation work? Is it not what powers the lambda syntax?

#

Or is it a way to define types as a lambda?

timber oak
#

An interface has no logic behind them

vital saffron
#

@FunctionalInterface doesn't do anything besides adding a compile time check that the interface only has one abstract method. You can create a lambda for any such interface, the annotation just exists as a hint to programmer that it's intended to be used with a lambda .

vital saffron
untold geyser
#

Interesting

#

Out of curiosity, do y'all know of any such language that has that functionality?

#

I'm curious to see how a working idea of such a feature would behave and what possible utility it would have

#

It definitely seems like it could cause problems at a compiler level though as it puts an ambiguity onto the syntax and could mess with every step in the compilation process

#

Especially if you have a constructor syntax that has a conflict with other syntax

#

It could also be powerful though for building languages, interestingly enough

#

Almost like boilerplate or a framework for a language

timber oak
#

I mean you have the existing annotations such as @NotNull and @Size

#
public User(@NotNull @Size(min = 1) String name, @NotNull @Size(min = 5) String email) {
        this.name = name;
        this.email = email;
    }
untold geyser
#

:O

timber oak
#

Doing it on the constructor itself is pointless

untold geyser
#

Why?

timber oak
#

because the signature is defined strictly and you can add your own validation inside it

untold geyser
#

Didn't think about that

#

So how would you go about enforcing something like max/min amount of digits in a number?

#

The only way I can think is int -> String -> length > max? || length < min? -> throw exception

timber oak
#

Noo

#

Let's say you have a number:

int number;

#

To restrict its size

#

E.g. you only want 3 digits?

number >= 100 && number <= 999
#

Only want 2 digit numbers?

number >= 10 && number <= 99

untold geyser