[Haskell-cafe] On the parallel between java annotations and monads

Keith Sheppard keithshep at gmail.com
Fri Jul 3 13:27:41 EDT 2009


I see some parallels between Inversion of Control/Dependency Injection
frameworks and monads. I would say annotations are tools that are
sometimes used for those frameworks, but they are also used for other
unrelated things (warning suppression, overriding functions ...) so it
may be more clear to focus on IoC and DI instead of annotations.

-Keith

2009/7/3 Rafael Almeida <almeidaraf at gmail.com>:
> Hello,
>
> After an extensive search (5 minutes googling) I could not find any comparison
> between Java annotations (or Python annotations for that matter) and Monads. I
> think they are similar in various aspects and I want to discuss them here.
>
> I'm sure several of you have experience with Java programming and its annotation
> system. At first they seem like an extension to the typing system, by using
> annotations a single variable or method may have more than one type: the real
> type and all the annotations types. The way you declare them is not important
> for this discussion, but assume that you've created the annotations
> GreaterThanFive and LessThanTwenty, you could annotate a method like
> this (let's
> forget about all the class verbosity for a while):
>
>        @GreaterThanFive @LessThanTwenty int doYourThing();
>
> You could than use reflection to read those annotations and you could check if
> the returned integer is in the correct range. You could even make the compiler
> generate code that will do that checking automatically for you.
>
> Even though that was my first impression of them, that's not really
> how they are
> used in practice (at least the practice that I have experimented). People use
> them to implement Monad-like features to the language. Actually, you can forget
> monad and think of computation containers.
>
> On an EJB environment there's something called a container and it will do all
> sorts of computations when you call an annotated method or when you
> create a new
> annotated class. For instance, if, inside of an entity bean, you annotate a
> variable like this:
>
>        @Column(name = "NAME") private String name;
>
> then, when you create a method like so:
>
>        public String getName()
>        {
>                return name;
>        }
>
> that name is actually something retrieved from the database, even though, it
> looks just like something in memory. As far as I know the interaction with
> monads for handling databases is not very different. There are other annotations
> which allows the container to "inject" some object on a variable, such as:
>
>        @PersistenceContext private EntityManager entityManager;
>
> now you won't even create an EntityManager instance, but will use the object
> living inside the container. I compare that to getting a value from an State
> Monad. Like there, you'll do some computations using entityManager, but it will
> ultimately be returned to the container.
>
> While each computational container in Haskell is very rigid and well
> defined, in
> java there's nothing rigorous about the annotation. However, quite often the
> annotations in Java are used in a manner similar to the implementation of a
> Monad in Java.
>
> []'s
> Rafael
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>



-- 
keithsheppard.name


More information about the Haskell-Cafe mailing list