by default-kramer · 8y ago · 0 comments
I'm definitely in favor of making dependencies explicit, but I wish there was a typed language that allowed me to avoid mentioning "less important" dependencies when I want to. Logging is a perfect example:
function log(msg: string) =
ImplicitDependencies<ILogInfo>().Write(msg)
Let's imagine that "ImplicitDependencies" is some special keyword. The compiler would give the "log" function a type of (string -> (implicit ILogInfo) -> unit). But my code is allowed to say that its type is (string -> unit) if it wants. Now if I use this "log" function in UpdateCustomerProfile, then UpdateCustomerProfile gets an (implicit ILogInfo) parameter too.The compiler would also give me some way to designate a function as "root level" meaning that all implicit dependencies must be resolved. I'd imagine something like Racket's parameterize for this. (https://docs.racket-lang.org/guide/parameterize.html)
The idea is, most of the time, I don't want to keep mentioning logging in my type signatures. I just want to silently kick it up the call stack.
It's also unprincipled as heck, from both an object-oriented and a functional perspective. I think my alignment in the logging department might be chaotic good.
It seems similar to traditional OO dependency injection, except without all the ceremony of adding a constructor parameter and corresponding class member to hold it.
It's a case of multiple implementation inheritance, so I could see a lot of people balking at it.
If you have a language that supports thread-locals and lambdas, you can get pretty far with something like:
public static S parameterize<T,S>(ThreadLocal<T> param, T value, Callable<S> body) {
T old = param.get();
param.set(value);
try {
S result = body.call();
} finally {
param.set(old);
}
return result;
}
You can then do something like: public static final ThreadLocal<Logger> LOG = ThreadLocal.withInitial(() -> getRootLogger());
And when you need a different logger, you can do: String result = parameterize(LOG, myFancyLogger, () -> {
return methodThatLogsThings();
}); [ImplicitDependency(LOG)]
void methodThatLogsThings()
Which would cascade up the call hierarchy until it gets satisfied.It does not, however, automatically infer the implicit parameters based on usage of implicit arguments in the function body, though.
https://dotty.epfl.ch/docs/reference/implicit-function-types...
https://www.youtube.com/watch?v=YXDm3WHZT5g&feature=youtu.be
https://blog.carbonfive.com/2018/03/19/lightweight-dependenc...
For context, Elixir (which includes Erlang) is a compiled language, but it doesn't/can't catch a lot of compile time type errors that you'd only uncover at runtime. For example, one process can send a message of any type to another process. There just isn't a great way (for the Elixir compiler) to catch that at compile time. It gives you more flexibility, but you can't have confidence that it'll work just because it compiled. Dependency injection with tests is a really helpful tool in Elixir.
The idea is that the type system knows everything, but allows you ignore these implicit parameters if you want.
function square3( x : int ) : div int
My code should be allowed say function square3( x : int ) : int
Basically, the problem is that in most typed languages, if you add a logger dependency to a function, then you have to rewrite the signatures of all the functions that depend on that function. That sucks.