Let’s get to know interfaces for functions - yes, that’s a thing.
We have seen interfaces used as types and interfaces as array types. Since function is an object and interfaces are supported on other objects, they should be supported on functions also. Right?
You betcha.
What is this blog if we can’t write about all the stuff that no one care’s about? Well, to be clear - I am not starting that trend today.
Let’s see an example of a simple function -
|
|
Imagine we have to support similar functions for all other million mathematical expressions. What are we going to do? Write a million types - never! We are no beasts.
We do something graceful using them interfaces and apply them to the function arguments.
|
|
Quite neat - won’t you say? But, it can be much neater.
We will wrap the entire darn function with an interface so that it covers all the things required to support the million math operations.
|
|
The added advantage - you don’t go about changing function signatures for object oriented programming. That is so 2000s. Now, you just mask (interface) the non-OOP beauty.
Let’s say you have another function with a similar signature,
|
|
.. all you have to do is to apply signature..
|
|
And, you are off to races (or wherever you go after you code a million math op’s).