I've been using spare cycles over the last six weeks to try to put together a proof of concept implementation of the type macros I described in my Taming the Cake Pattern blog post from three months ago. I'm happy to say I have a working implementation of the first six macros! It really needs at least one more to be at all useful, and there is plenty of stuff that needs to be fleshed out. But I was very happy to learn that it was actually possible to make this happen. I'm currently compiling three examples and 27 test cases.
scabl
Scalable Blog.
2013-05-07
2013-04-23
Scaladoc Macros
I've seen these used a lot in the Scala language source code, but I haven't gotten around to trying it until now. I have two public methods that have the same constraint on, and initially was maintaining two copies of the documentation describing the constraint:
This generated Scaladoc that looked like so:
Using Scaladoc's @macro syntax, (described in here somewhere), I can remove the repeated code as follows:
The generated Scaladoc is identical.
This generated Scaladoc that looked like so:
Using Scaladoc's @macro syntax, (described in here somewhere), I can remove the repeated code as follows:
The generated Scaladoc is identical.
2013-03-26
Monads in Scala Part Two: More Maybes
In the first installment of this series, we translated the examples from the first page of the chapter on monads in the Haskell wikibook from Haskell to Scala. In this blog post, we will continue on with the second page. This page continues with more Maybe examples, which will help us to generalize our monadic laws test for Maybes a bit. We'll also learn about kleisli composition, which composes functions suitable for passing to flatMap.
All the source code presented here is available here on GitHub. I'm compiling against Scala 2.10.1. Some of this code will not compile under Scala 2.9.x.
2013-03-08
Taming the Cake Pattern with Type Macros
In a previous post we discussed various techniques that could be employed with the component based dependency injection (CBDI; typically referred to as the cake pattern). In particular, we discussed two CBDI features that are not available in DI frameworks such as the Spring framework or Google guice: expressing design constraints between hierarchical components, and encapsulating the details of a composite component. Alongside these advantages, however, the cake pattern has some disadvantages that limit its appeal. The three major problems that I see are:
Lately I've been pretty excited about type macros, an experimental Scala language feature, and I've been considering ways in which type macros might relieve some of these problems. In this blog post, I want to describe some hypothetical type macros that might alleviate the first two problems: verbosity and opacity. I haven't had a chance to use Scala macros yet, and while the macros presented here should be possible, I can't say for sure until I've tried it. So think of what is presented here as a spec. I'm going to try to implement it, and I'll let you know how that goes.
- Verbose: Lots of boilerplate.
- Opaque: It's hard to figure out and understand what is going on by looking at the code because the language structures used do not signify the user's intent. These language structures are somewhat advanced, and may seem a bit foreign to programmers that are relatively new to Scala.
- Aloof: Compiler error messages are potentially confusing and misleading, because they address the language structures used, and not the user's intent.
Lately I've been pretty excited about type macros, an experimental Scala language feature, and I've been considering ways in which type macros might relieve some of these problems. In this blog post, I want to describe some hypothetical type macros that might alleviate the first two problems: verbosity and opacity. I haven't had a chance to use Scala macros yet, and while the macros presented here should be possible, I can't say for sure until I've tried it. So think of what is presented here as a spec. I'm going to try to implement it, and I'll let you know how that goes.
2013-02-24
Monads in Scala Part One: Maybe[Person]
Intro
I've split the introduction to this post into two parts. The first part is long and sentimental, describing my personal journey with monads up to now. The second part is short and to the point. Feel free to skip over the next section if you're not interested in the sappy stuff - you won't be missing anything important.Sappy Intro
I've been programming Scala for a little over two years now, and I love it. I was an old-time Java hack, firmly embedded in the OO programming paradigm. Scala to me was largely like a souped up Java, with all the clunky, broken things about Java fixed (aside from type erasure). I had taken courses Scheme and the lambda calculus in college, and I thought I had a pretty good understanding of functional programming. However, while programming Scala, and getting exposure to the language, libraries, and community, I began to discover that there is a lot more to functional programming than I originally thought. By far the most prominent example of this was the the importance of the concept of the monad in the Scala community.I didn't know what a monad was, and over the past couple of years, I've made various efforts to try to understand it. For the most part, the videos I watched, and the articles I read, left me with one of two impressions: Either it didn't make any sense, or I just couldn't see what the big deal was. As I continued to explore, I began to notice that most of the explanatory examples are in Haskell, and not understanding the Haskell language or syntax was a serious impediment for me. At that point, I set out specifically searching for monad examples in Scala, and found this excellent series of blog posts by James Iry: Monads are Elephants (see also parts two, three, and four). Finally, monads were beginning to make sense! But after reading these articles, I realized I was going to have to implement some monads myself to really grok them. I also knew that I would have to follow by example to do this. Which meant that I really needed to bite the bullet and look at some monad examples in Haskell.
No Nonsense Intro
I found an excellent exposition on monads in this Haskell Wikibook, and set out to translate the examples into Scala. In this blog post, we will present a translation of the Maybe/Person example presented in the first page the chapter on monads. In future blog posts, I hope we can do the same for the rest of the pages of this chapter.2013-02-19
Please Comment and Follow
I'm very happy to see so many people reading my previous blog post on Component Based Dependency Injection in Scala. Thanks to Morgan for commenting! I also received some good feedback from other sources, including LinkedIn, and the scala-user Google group. I want to reach out to encourage you to comment on the posts. Also, please follow my blog if you like what you see. See the link at the top of the right sidebar. Thanks!
2013-02-12
Component Based Dependency Injection in Scala
I recently wrote up my experiences using the "cake pattern" for dependency injection in Scala. It grew into quite a lengthy write-up, so I wanted to share it as a PDF in case reading in this blog format is inconvenient:
The same content appears below, but please be warned that the internal links are broken, and the code formatting is not to my liking:
Subscribe to:
Posts (Atom)
