Is there an annotation like `@Deprecated` but mean...
# announcements
r
Is there an annotation like
@Deprecated
but meaning specifically that a method call is pointless, because it returns
this
and has the same return type as the object it is called on? For instance,
String.toString() = this
would be a candidate for it.
You would do this when the method is defined as an abstract one on a supertype - so it’s totally valid to call it on the supertype, and the subtype’s
= this
implementation is totally correct, but it would never make sense to call it on an instance types as the subtype.
c
maybe method contracts can do it?
b
I usually put something like
throw IllegalAccessError()
inside a method like that, but I don't know if there is an annotation.
r
I definitely don’t want that: 1) I want a compile time warning/error, not a runtime one 2) It would be very surprising & undesirable if calling the method on a val typed as the supertype blew up just because it happened to be an instance of a particular subtype The goal is just to gently point out to developers that they are making an unnecessary call, like calling
"a string".toString()
. You wouldn’t want the implementation of
toString
on
String
to throw an exception rather than returning itself.
c
I think idea could just do that without any annotation
s
Isnt this just:
Copy code
@Deprecated("Useless method call", replaceWith = ReplaceWith("this"), level = DeprecationLevel.ERROR)
r
Yes, that’s what I’m doing; it’s just that
@Deprecated
feels slightly semantically odd for a method that exists for good reason and that I have no intention of ever removing, and that it’s quite valid to call on the supertype. So I was wondering if there was something more like
@PointlessToCall
that had the same effect..
z
You can define your own annotations annotated with
RequiresOptIn
that kind of act like Deprecated.
👍 1