mktany2k / funcito

Automatically exported from code.google.com/p/funcito
0 stars 0 forks source link

Support wrapping constructors #56

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
(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.

Original issue reported on code.google.com by palotai....@gmail.com on 15 Jun 2012 at 12:42

GoogleCodeExporter commented 9 years ago

Original comment by kandpwel...@gmail.com on 15 Jun 2012 at 3:48

GoogleCodeExporter commented 9 years ago
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.

Original comment by kandpwel...@gmail.com on 18 Jun 2012 at 10:59

GoogleCodeExporter commented 9 years ago
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.

Original comment by palotai....@gmail.com on 19 Jun 2012 at 1:32

Attachments:

GoogleCodeExporter commented 9 years ago
Interesting.  Thanks, I will consider.

Original comment by kandpwel...@gmail.com on 19 Jun 2012 at 2:56

GoogleCodeExporter commented 9 years ago
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.

Original comment by kandpwel...@gmail.com on 19 Jul 2012 at 8:34

GoogleCodeExporter commented 9 years ago
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.

Original comment by kandpwel...@gmail.com on 24 Aug 2012 at 3:52