#Specify generics?

15 messages · Page 1 of 1 (latest)

hybrid badge
#

Can I do something like class ArrayList<String> extends ArrayList<E> {? I have a class with a generic and I want to override a method if the type is string, so I can use .matches() instead of ==. Being able to check the type of a generic would also work.

pearl cosmosBOT
#

This post has been reserved for your question.

Hey @hybrid badge! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

alpine thistle
#

what exactly do you want to do?

#

Can you give a more concrete example?

#

you cannot check generics at runtime

hybrid badge
#

I have a method that takes the type of my list, and uses == to compare it to some elements. I’d like to be able to use .matches() instead if the generic happens to be a string

alpine thistle
#

you cannot check the generic type at runtime

#

but you can check the element

#

and in general, maybe consider using equals() instead of ==

hybrid badge
queen jungle
#

String.class, but yes

#

Or check if (input instanceof String s)

#

Then you can operate on s

hybrid badge
#

Cool, I’ll try that