Issue 56: Support wrapping constructors
Status:  New
Owner: ----
Reported by palotai....@gmail.com, Jun 15, 2012
(I remember reading about this on the list, but can't find any related issue)

The ability to wrap a constructor of Clazz with args A, B, ... to a FN<A, B, ..., Clazz> would be great.

Maybe this depends on #55 (wrap multi-arg methods into FN) for functionaljava, not sure about other frameworks. Is using multi-arg or curried function objects in other frameworks common/possible at all? If not, then maybe these FJ-specific enhancements should go into a separate extension package sooner or later.
Jun 15, 2012
Project Member #1 kandpwel...@gmail.com
(No comment was entered for this change.)
Labels: -Type-Defect Type-Enhancement
Jun 18, 2012
Project Member #2 kandpwel...@gmail.com
I'm not sure I see the usefulness in this.  You may have to flesh this one out some more before I consider it: I would want usefulness, and sample API on what it would look like, and whether this is truly something that would get used a lot by FJ users, or is it rather just something that might get used once in a blue moon.

I will be honest that I am not actually an FJ user, and so I don't understand how the higher-arity F's (F2, F3) get used in practice, and why in the world you would use one for a specific constructor of a specific type with specific arg types when you could just use the constructor itself... maybe when several classes in a hierarchy have the same constructor signature?  I am sure I have just not been initiated into the mysteries of the much more advanced FJ library :-)  Please enlighten me.
Jun 19, 2012
#3 palotai....@gmail.com
A constructor is a function from its arguments to the constructed type. Let's assume, that I can easily use a constructor instead of that constructing function, then by extension, I can as well easily use methods instead of their wrapped functions, so Funcito is not useful as a result. But since it is useful, there should be something useful about wrapped ctors :).

Attached is a Main.java (add fj 3.0 as dependency), highlighting 3 different use-cases of FNs (here F2, for simplicity). These are not exclusively useful for constructors, but I find the outlined pattern (Validation especially) common (YMMV).

If interested in these patterns more generally, you may find more info about Monads and Applicative functors (here, Option was used as a Monad, and Validation as an Applicative). My main sources were learnyouahaskell and realworldhaskell (both free online) and scalaz.
Main.java
3.4 KB   View   Download
Jun 19, 2012
Project Member #4 kandpwel...@gmail.com
Interesting.  Thanks, I will consider.
Jul 19, 2012
Project Member #5 kandpwel...@gmail.com
This may also be linked to the same/similar challenge as I just added to Issue 33: there is no constructor polymorphism, so normal bytecode provider (cglib/javassist) proxy factories cannot intercept constructor calls.  Once again, I might need to look at PowerMock code, or directly use Objenesis.
Aug 24, 2012
Project Member #6 kandpwel...@gmail.com
Looking into Powermock led me into JMockit, which is using Java instrumentation API.  This would force the use of an agent, which can be automatic in Java 6+, or force a command line option in Java 5, and also require some classpath ordering.

I'm concerned that such a solution might affect runtime speed.  Not sure if I could limit the scope of *when* method interception happened.  If not, this may not be feasible.  Same thing for wrapping static methods(see Issue 31).

Good news is, that if this is do-able, it may open up the door to wrapping methods/chains that are final or final classes.