Scala and Friends


Scala is an interesting language. I needed to pick it up for a project at work and was enthralled. I love some of the elegance of it, while some of the challenges are downright insane.

One point of view that I’ve come to embrace from working with PHP, is that types are fungible. In Web land, everything technically comes across the wire as a bunch of bytes that are most easily represented as a string. We create data formats like xml and json to “hint” what kind of data the string represents. PHP recognizes this fact, such that you can do "1number" + 42 and end up with 43.

I chose Finangle + Finch + Circe for a lightweight API. I tried many scala libraries, and something I had a hard time with on all of them was accepting a request that has a json body with multiple types. Here’s some examples:

https://gist.github.com/withinboredom/9b4ecdfd7f35f0a7ffbd8d3d026c269e

I ended up with the following structure, which I’m not entirely sure it’s right, but it worked for me:

https://gist.github.com/withinboredom/1168bf77711c328733c421870dd92c68

This allows me to pass around KeyValue[Value] objects at will. Circe will use the decoders to ensure things get casted to the right types. The major downside is that it cannot really handle complex types as a value. However, the spec that I’ve been working with doesn’t allow for complex types anyway.

Overall …

Scala has been pretty cool. I like how you can switch between imperative and functional style programming, depending on the problem being solved. You’re not strictly limited to one or the other, or using half-baked ideas sewn in in the last few years.

However, I’ve run into several language language/compiler bugs that have been more than annoying. It’s very frustrating when you have comments like this in the code:

import blah.blah.blah // this line must be the first line in the PROGRAM, otherwise, the code cannot be compiled from a clean state (ie, CI) but will compile just fine incrementally (ie, on your machine)

I’m not even joking, that comment is in the code base! There’s a bug filed for it somewhere, it took me awhile to find it because hell, how do you search for that? “scala clean compile fails?” nope, while that has a similar workaround, it isn’t the actual issue I was facing. I can’t even find it again.

It’s been fun, but honestly, I’m kinda looking forward to getting back to some php 🙂


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.