function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Melvin_DeveloperMelvin_Developer 

Parameterized classes

Does anyone know how I can create a Parameterized class?

For Example:
I want to be able to do the following, customClass<String> cc = new customClass<String>();
So all the methods in the customClass work with String in this case, but should allow any type to be passed in.

Simular to the way a Map or List works!

Any help on this would be great,

Thank you,
                  Melvin!

kiranmutturukiranmutturu

I think in any of the languages there is no concept of parameterized class at all.. but there is a concept of parameterized constructors.....

bob_buzzardbob_buzzard
Melvin_DeveloperMelvin_Developer

Thank you for your help with this, I'm not sure that the referenced document details the ability to what I want.
Below is an example of what I need to do:

customList<String> cl = new customList<String>();
customList<Integer> cl = new customList<Integer>();

The customList should take a string, integer, class or anyother type for that matter and store those in a list and allow all functions belonging to the customList to return the correct type etc... Just like in a normal list or Map.

The issue with the information provided in the documentation is that there must be a concrete instance of the customList that defines the type, that is not what I want, I require the flexability to pass in any type. I think the solution will use much of the information provided in this document but I don't know how you would get around the need for a concrete instance.

I hope the above makes sence.

Any Ideas how this could be done would be great!

 

This is how far I have got so far:

public virtual interface CustomListBase<T> {
	List<T> getItems();
	void setItems(List<T> val);
	List<T> reverse();
}
	
public interface CustomList<T> extends CustomListBase<T> {}

public void runTestCustomList(){
	CustomList<String> cl = new CustomList<String>();
	cl.setItems(new List<String>{'1','2','3'});
}

 But I get an error at this line: CustomList<String> cl = new CustomList<String>();
Error = Type cannot be constructed



Thank you,
                  Gary J Burgin

bob_buzzardbob_buzzard

Hmm.  I can't think of a way to do this without a concrete implementation per type, aside from a clunky class that maintains a list of objects and each method has a large number of if statements to figure out the actual type and behave appropriately.  Wouldn't be future-proofed though, as if you used a custom class that would require new code.