Mark Elston (111) [Avatar] Offline
#1
I thought I was following the discussion here until I came to this section. But I am now completely confused.

After the second listing in this section you mention "In this example you have to pass the Java class explicitly. You know already how to fix this". I'm not sure what you mean by 'fix this' as the 'fix' in the next listing also makes use of the Java type so I don't really see what was fixed.

Next, in the third listing you use the 'reified' keyword in the type when without explaining why it is used. In particular, why is it used here but wasn't required in the previous listing? I don't really see the difference in the two listings as to why it is needed in one and not in the other.

I suppose I may have missed something fundamental in the previous discussion but I am completely confused by this section. I'm completely at a loss as to how you decide when to use this keyword and when you don't need to.
Svetlana Isakova (9) [Avatar] Offline
#2
Hi Mark,

Thanks for your feedback!

Let's try to rewrite the confusing paragraph:

"In this example you have to pass the Java class explicitly when you want to create a new animal, a verbose Cat::class.java is the first argument. In the section "Replacing Class References with Reified Type Parameters" we discussed how to simplify that and specify only the type argument instead: newAnimal<Cat> {...}. You declare a new function that takes the necessary subclass as a type parameter and just delegates to the function above: ..."

By default you don't put reified keyword on a type parameter. You use it only when you need this feature, for example when you want to check an expression for having the exact type, or to access class references. But you have to inline the function then.

In the third listing you have to make a type parameter T reified to be able to access T::class.java in the function body.
Mark Elston (111) [Avatar] Offline
#3
Thanks for clearing this up. I think I will have to reread this chapter a few times before it really sinks in.

One remaining question I have is that if you can only use the reified keyword in an inlined function, why is it needed at all? If a function is inlined isn't the type available to the compiler without the reified keyword?

My background here is C++ and the model I am thinking of is something along the lines of a preprocessor directive where the code being inlined is actually inserted into the place it is used before being compiled (and I know that isn't how inlining works in C++ but it is an effective model anyway). Why doesn't the compiler have access to the type in an inlined function without the reified keyword?
Dmitry Jemerov (39) [Avatar] Offline
#4
The compiler does have access to the type, but we believe that it's better to use an explicit keyword to mark the fact that the type parameter is being used in a way that's different from the way normal Java generic type parameters are used.
Mark Elston (111) [Avatar] Offline
#5
Thanks, again.