Skip to main content

K ?

Whisper the terrible words type erasure to any moderately experienced Java programmer and you're likely to see:
  1. fear
  2. loathing
  3. soul-crushing sense of defeat
  4. all of the above
Now if you are a moderately experienced Java programmer, but you're thinking to yourself "what's type erasure ?" my advice is this:

Stop reading now !

Sometimes ignorance really is bliss.

My own rule of thumb, when confronted by the need to query the identity of K, or V, or T or any of their evil friends, in the innards of a class with generic type parameters, is to run away. Sadly, there are circumstances when that's just not an option. Although I deeply empathise with those who equate Java reflection with incest and folk dancing there are times when it's black magic or nothing. But where to find the magic ?

Enter Richard Gomes and his article: Using TypeTokens to retrieve generic parameters. This demonstrates the seemingly impossible feat of pulling erased rabbits out of Java's hat.

I'm off to try it now... "Nothing up my sleeve..."


Comments

Popular posts from this blog

Circle packing in R (again)

Back in 2010 I posted some R code for circle packing . Now, just five years later, I've ported the code to Rcpp and created a little package which you can find at GitHub . The main function is circleLayout which takes a set of overlapping circles and tries to find a non-overlapping arrangement for them. Here's an example: And here's the code: # Create some random circles, positioned within the central portion # of a bounding square, with smaller circles being more common than # larger ones. ncircles

Fitting an ellipse to point data

Some time ago I wrote an R function to fit an ellipse to point data, using an algorithm developed by Radim Halíř and Jan Flusser 1 in Matlab, and posted it to the r-help list . The implementation was a bit hacky, returning odd results for some data. A couple of days ago, an email arrived from John Minter asking for a pointer to the original code. I replied with a link and mentioned that I'd be interested to know if John made any improvements to the code. About ten minutes later, John emailed again with a much improved version ! Not only is it more reliable, but also more efficient. So with many thanks to John, here is the improved code: fit.ellipse 0)] f Next here is a utility function which takes a fitted ellipse and returns a matrix of vertices for plotting: get.ellipse And finally, some demo code from John: create.test.ellipse 1 Halíř R., Flusser J.: Numerically stable direct least squares fitting of ellipses. In: Proceedings of the 6th International Confere...

Circle packing with R

To visualize the results of a simulation model of woodland trees within R, I needed an algorithm that could arrange a large number of circles within a rectangle such that no two circles overlapped by more than a specified amount. A colleague had approached this problem earlier by sorting the circles in order of descending size, then randomly dropping each one into the rectangle repeatedly until it landed in a position with acceptable overlap. I suspected a faster and more robust algorithm could be constructed using some kind of "jiggling the circles" approach. Luckily for me, I discovered that Sean McCullough had written a really nice example of circles packing into a cluster using the Processing language. Sean's program is based on an iterative pair-repulsion algorithm in which overlapping circles move away from each other. Based on this, and modifying the algorithm a little, I came up with an R function to produce constrained random layouts of a given set of circles. ...