Generics and templating: Difference between revisions

From Helpful
Jump to navigation Jump to search
mNo edit summary
mNo edit summary
 
(One intermediate revision by the same user not shown)
Line 55: Line 55:
you might be able to do generics only by forgetting type information in the process - which you might call '''type erased generics'''.
you might be able to do generics only by forgetting type information in the process - which you might call '''type erased generics'''.


For example, Java has allowed a ''kind'' of generics for a long time, in that you can see every object as Object type before handing it around{{verify}}, and hoping you only hand it to something that doesn't confuse what more specific type it actually is, or for you to hand that information along some indirect way.
For example, Java has allowed a ''kind'' of generics for a long time, in that you can see every object as an Object before handing it around, and hoping you only hand it to something that doesn't confuse what more specific type it actually is, or for you to hand that information along some indirect way.


This type-erasing generics seems to have been done at the time to have backwards compatibility - older runtimes would still understand this newer style of generic classes, because they weren't really special{{verify}}.
This type-erasing generics seems to have been done at the time to have backwards compatibility - older runtimes would still understand this newer style of generic classes, because they weren't really special{{verify}}.




In such contexts, '''reified generics''' refer to the situation where the compiler{{verify}} knows and preserves the type, for syntax to use.  
In such contexts, '''reified generics''' refer to the situation where the compiler / editor / runtime knows and preserves the type, for syntax to use.
 
 
You can explicitly add the type information in the implementation, as you have above, but that requires additional code every time the list is used, and is pretty messy in my opinion. Also, in this case, you still don't have runtime type checking for all of the list methods unless you add the checks yourself, however reified generics will ensure the runtime types.
 





Latest revision as of 13:58, 9 January 2024