http://stackoverflow.com/questions/4551457/python-like-decorators-in-java
A Spring Aspect Oriented Programming example.
https://github.com/keobox/dojokaffe/tree/master/fp/spring-elapsed
Code walkthrough in Eclipse.
In [3]:
"Elapsed decorator."
import datetime
def elapsed(func):
"Elapsed decorator"
def _wrapper(*args, **kwargs):
"Decoration function"
start = datetime.datetime.now()
ret = func(*args, **kwargs)
print("Elapsed time", datetime.datetime.now() - start)
return ret
return _wrapper
The former was both a Closure and a High Order Function. Did you heard about Functional Programming?
In [6]:
# Usage:
import time
@elapsed
def task():
"A difficult task."
print("Processing...")
time.sleep(2)
task()
The previous was the built-in "Decorator" syntax.
Duck Typing it's a sort of Generic Programming where object's suitability is determined by the presence of certain methods and properties, rather than the actual type of the object.
In [1]:
class Duck:
def quack(self):
print("Just a crazy, darn fool duck!")
class Man:
def quack(self):
print("Are you crazy?!")
def porky_pig_shoots_a(quacker):
quacker.quack()
In [2]:
duffy = Duck()
cesare = Man()
porky_pig_shoots_a(duffy)
porky_pig_shoots_a(cesare)
Although is perfectly possible to use Duck Typing in Java with the Reflection API, Java is not done for this. Since Java 1.5 we have Generics, so let's take a look to them.
Look at Generics issues pointed out by "Effective Java" Item 25.
Generics was a great addition, but the choice to be backward compatible with previous Java code has produced some implementation problems that surfaced into the language design.
Other examples of Pitfalls:
"...This was my first clue that, in Python, I was actually dealing with an exceptionally good design. Most languages have so much friction and awkwardness built into their design that you learn most of their feature set long before your misstep rate drops anywhere near zero...". "Why Python?" Eric Raymond, 2000, http://www.linuxjournal.com/article/3882
"I always admired the neatness of Python syntax...". Martin Odersky, 2017, https://github.com/lampepfl/dotty/issues/2491