Archive for the ‘Programming’ Category
Kotlin: it’s more useful than half a unicycle.
I’m on one of my departures from Scala, where I spend a week writing something in Scala, look at it at the end of the week, and realize I’ve written a big pile of unreadable crap. I mean, even more unreadable than usual; while at first I chalked it up to me being kind of dumb on my best day I’m starting to think some of the blame should be laid on the language. Scala seems bound and determined to make me write nasty* code that I feel bad about later.
I’m an IntelliJ user, and I am sort of honor-bound to love JetBrains because they make my job happier. So Kotlin’s been on my radar for a while. I was curious about it around the time of their first milestone release, but never followed up on it, and because I keep going “I miss Scala feature X” when working in Java** I decided that I’d risk hilariously-alpha software to see what I could do with it.
First impressions: This is the first non-Java JVM language that I haven’t started hating before writing a single line of code! The Maven integration is decent, which surprised me; right now they expect you to be building projects in IntelliJ and using the built-in project system, and I’m not because I’m a big uncooperative jerk who wants to build code where I don’t have IntelliJ installed–but, for the most part, they’ve still got my back on this. The documentation is sparse, but that’s also to be expected with a project at version 0.1.you’rejokingwhyareyouusingthis. None of that really bugs me, and so once I found the magic incantations, I was set to go.
(One minor beef that probably should have been in the documentation but isn’t: it appears to be conventional to put your entry point as a ‘main’ function instead of in a class. This is OK by me. What’s infuriating is that you are given approximately zero clues as to how to invoke this from the command line. Turns out that free functions in a namespace are collated in the ‘namespace’ class file and you can invoke them that way. Once I found out the magic words and could give them to maven-exec-plugin, it worked great!)
Now I should, like, go write some code. See if it holds up after all of fifteen minutes.
* – “nasty” can be replaced by “pythonic” or “functional”; you can pick your own pejorative!
** – None of these “feature X”es have anything whatsoever to do with the Functional Mafia, but rather stuff like object { }, val/var and type inference, and simple lambdas instead of the “new Runnable()” boilerplate. Hey Oracle, are you watching***?
*** – This is rhetorical. Oracle gives not one red fuck.
New project: Velvetrope
So with the release of Play 2.0 (Now With Working, Not Broken Scala Support)!, I’m playing around with Scala in a web context again. I dig the workflow in Play 2.0, even if SBT makes me a little bit queasy, and I’m enjoying getting to build web stuff in Scala.
The first thing I noticed was a lack of plugins. I’m not complaining: Play 2.0 is so new it still has the new-car smell on it and so this is completely to be expected. So I started noodling around on the important bits and pieces for myself. First up was building a user concept: user objects, ACLs, that sort of thing. If you’re using Java with Play, there’s Deadbolt 2, which looks to be very well thought-out — I say “looks to be” because I haven’t tried it, I’m doing my damndest to stay away from Java in my Play environment — but seems a lot more ambitious than what I need. I need to know “is this a user?” and “should this user be here?” and that’s about it.
Enter Velvetrope, the extracted role/permissions bit of my ACL stuff. (The actual user stuff, termed Guestlist, will come along when I — err — finish writing it.) It’s designed to be as painfully simple as possible: you define Roles as Scala objects and test against them with reasonably nicely loose-coupled logic points for your own crunchy bits. This is not super-complex code and may, in the end, be useful only for me, but if anyone else finds it useful, awesome.
Personally, I’m digging the way that access ends up being controlled. If you want to restrict a controller action, it ends up being something like this:
val readSomethingSecret = AccessControlled(SomeSecretPermission) {
(user, request) => {
Ok("here it is!")
}
}
Brain-hurtingly simple and just the way I like it. You attach a trait to your GlobalSettings-derived object and that trait allows you to define a function that pulls your User object — decorated with RoleOwner — from a Request, as well as specifying a Result to hand back (a redirect or whatever else) in the case that the User object doesn’t have the right Role or doesn’t exist at all. Like I said — not super-complex, but it’s the first building block for the rest of the stuff I’m doing and it was an excuse to start pushing stuff public.
You can get Velvetrope on Github, or you can use a dependency from my maven repo at maven.edropple.com (which I recommend, as it’s what I’m going to be using myself). The SBT magic words are:
val appDependencies = Seq(
"com.edropple" % "velvetrope" % "1.0-SNAPSHOT"
)
val main = PlayProject(appName, appVersion, appDependencies, mainLang = SCALA).settings(
).settings(
resolvers += "Ed Ropple's repository" at "http://maven.edropple.com"
)
Questions, comments, hate mail, or whatever else – @edropple on twitter.