Tales from the jar side: Kotlin delegates, NEJUG, and Terrificon
Welcome to Tales from the jar side, the Kousen IT newsletter, for the week of August 4 - 11, 2019. This week I sent a new version of the Kotlin Cookbook to reviewers, gave a talk entitled Kotlin: Beyond the Basics at the New England Java Users Group, and attended Terrificon with my wife.
From that description, you can see that this was an intense week from a Kotlin perspective. I'm trying very hard to complete the book by September, and that means cranking through a lot of recipes. The fact that I knew I had to speak to the New England JUG Thursday evening kept me focused on short, straightforward examples I could present to an audience, which fit the book nicely. My plan was to send the latest version of the book to tech reviewers on Friday, even though it's still incomplete. I think I added as many as ten new recipes before I wrapped it up for the week.
Mostly I spent time on delegates. This is one part of Kotlin that less intuitive than I expected, so as usual something I thought would be easy led me down a rabbit hole of complexity. I have an example I use in Groovy that takes advantage of the @Delegate annotation, and eventually I was able to adapt it to Kotlin as a class delegate. That required creating interfaces for each of the contained objects and supplying instances in the constructor. Doable, but more work than I expected, and another indication that Kotlin is a very young language. I couldn't do that sort of thing easily in Groovy at this stage of its lifecycle, either.
Property delegates turned out to be a lot harder. Creating a property delegate requires adding a class with one or two methods of the right signature (depending on whether the property is read-only or not), but every time I tried to come up with an example I stumbled. I'm still not sure why I found such a simple idea so challenging, but over the years I've come to believe that if I'm struggling with a concept, so will a lot of other people, so I tried not to get too down about it.
In the end I dug into the four delegates that already are implemented in the standard library:
lazy, which makes it easy to delay the creation of an object until you need it
observable, which executes a block every time a property changes
vetoable, which lets you decide whether to accept the changes at all or not, and
notNull, which throws an exception if you access a property before it's been set
The lazy delegate followed a different implementation pattern from the others, but after seeing how the other three worked, I understood the process a lot better. I still wasn't able to come up with a good custom one of my own, but mostly because everything I thought of could be reduced to one of those existing ones.
The end result was that where I expected to do a single recipe (on lazy), I would up adding a chapter with five separate recipes in it. Given that my book is still on the short side (only about 175 pages at the moment) and each of those deserved a dedicated treatment, I think that all worked out pretty well.
I finally started adding recipes on sequences, too. As far as I can tell, they behave exactly the same way Java streams do, and I wrote most of a book about that. I ran out of time before adding everything I wanted there, but it's a start. I also added some good examples of when and how to use the the let and also functions, and in doing so I noticed that my programming style has evolved again. Code I wrote for the book that's even a few months old now looks out of date, so I'll be doing some work bringing that back into the standard idioms. That's all a good sign, but again it means I have more work to do.
Once again I'm tempted to lament the drive to meet rather arbitrary book-related deadlines, but the truth is I need this done, too. I'm caught, as usual, between several competing effects: I want the book to be finished, I don't like being told I have to finish, I hate feeling like the included parts are insufficient or not good enough, and the harsh knowledge that left to my own devices I might never complete it. In the end the deadline will probably win, as it usually does. The key is to believe the result is decent enough when I cross the finish line.
(Btw, for marketing purposes associated with this newsletter, I'll say that the book is fantastic and you should buy it immediately. Pay no attention to the man behind the curtain.)
One of the most important lessons I've learned from several writers I respect is to finish your projects. That always goes against my grain, but it's critical. There's so much fear in finishing, because until then the book is only a hypothetical, and no one can find fault with it -- or if they do, it's okay because it's not done. Even great artists say that the work you're creating never looks like the image you have in your head. At some point you have to accept that it is what it is, and finish. Joe Straczynski (more about him below) says that a writer starts off with only a few tools in his or her toolkit, and every time you finish something you add a new tool.
I'm sure that's an oversimplification, but it's true enough for me. I'll get this done, but this home stretch will take a lot of effort. Then I can get back to my Managing Your Manager book (remember that?) which has been sadly neglected for far too long and presents its own unique set of challenges.
I remember reading a story told by Neal Gaimon after finishing one of his major novels. He apparently told a good friend (might have been Terry Pratchett; I'm not sure) that he now felt he knew how to write a book. Pratchett replied, no, now you know how to write this book. Each one will be significantly different.
When my Kotlin Cookbook is done, I will have written three recipe-based books in the same system for the same publisher, and they've all been dramatically different. Go figure.
My presentation at NEJUG (New England Java Users Group) went really well. I got to show them all the delegate examples, and "let" and "also" and others, and several things besides. The attendees were interested, enthusiastic, and asked lots of good questions.
The only non-presentation tidbit I want to mention is that the meeting was held in Burlington, MA, which is about 120 miles from me. That meant a round-trip of about 240 miles. I charged up my Tesla the night before to full, which supposedly is just over 300 miles. I managed to make it there and back without stopping to charge the battery, but when I got home I only had about 15 miles left. The estimates in the car are off, therefore, by as much as 40 or 50 miles. I'll have to take that into account in the future.
Also regarding the Tesla, it downloaded a software update early last week, which now means I can play chess against my car. The included AI has four levels. I beat it on levels 1 and 2, but I've lost to it every time on level 3, which is rather frustrating. That mostly reminds me that I don't really enjoy playing the game nearly as much as watching grandmasters play each other. I love watching Magnus Carlsen (the reigning World Champion) play blitz or even bullet chess (1 minute for the entire game). I'm good enough to just barely follow what he's doing, but not good enough to avoid getting crushed by my car. And yeah, that statement means something very different from what it sound like.
On Friday afternoon, my wife and I traveled down to Terrificon at the Mohegan Sun casino. That's about a 45 minute drive for us. The comic book convention was held over three days and featured several people we wanted to see, like John Wesley Shipp, Billy Dee Williams, Tony Todd, Val Kilmer, Robert Wuhl, and the one person I wanted to see, J. Michael Straczynski, who goes by Joe but is often referred to as JMS. My wife's birthday is Monday, so this was an excuse to get away for the weekend doing something she would enjoy, too.
I've already talked about how much I wanted to see JMS and how I read his entire autobiography Becoming Superman beforehand, and how my wife and I were huge Babylon 5 and Sense8 fans, and more. I won't rehash all that here. I'll just say that I was worried, because they say you should never meet your heroes, and if he wasn't what I thought he was I didn't want to know about it, and if he was then I wanted more than 30 seconds of time with him.
I'll say this much:
He is incredibly kind and friendly in person.
Despite his self-described lack of social skills (documented in his book), he makes eye contact and really listens when you talk to him. It was awesome.
He started his panel on time because of course he did.
He answered every question asked clearly and intelligently, including those who waited until afterward to see him.
He gave me way more time than I expected or arguably deserved, and let me take the pictures below on two separate days.
One of his best characteristics wasn't obvious right away. We of course went to see him first. We were told to pay $10 for an autograph (I brought my book with me), and he kindly let us take a picture with him. We then went and met a few of the other guests, who were charging about $30 to $40 for a signature and another $20 or so for a picture. After that I noticed JMS had nobody in front of him, so I dropped by and said, "you're really undercharging." He smiled and said, "I know", which is not only consistent with everything I've ever read about him but shows you that he still thinks of himself as a fan and is happy be available for them at minimal cost. That one act put me is a good mood the rest of the day. :)
(My apologies for the sideways pictures. I have no idea why TinyLetter does that, and have no idea how to fix it.)
I mentioned to Joe that he doesn't come across in pictures nearly as well as he does in person. He replied that "there are some people the camera loves, there are some people it hates, and some people cause the camera to go hide in the bathroom crying until they go away."
Yeah, he's a writer. He says things like that. :)
I could say more, but I'm afraid I seem to have picked up a bit of a stomach bug, or perhaps some other convention ailment and this is about all I have energy for tonight. Suffice it to say this was a fun week.
This coming week is the last "open" week on my schedule for quite a while, so if I did catch a cold I guess it's good timing. I definitely have to get a lot of writing done, however.
Last week:
Turned in a draft of Kotlin Cookbook to reviewers
Kotlin: Beyond the Basics talk at the New England Java Users Group
Terrificon in Uncasville, CT
Next week:
More work on Kotlin Cookbook
Get back to Managing Your Manager