A little bit of http4k syntactic sugar … If you d...
# http4k
n
A little bit of http4k syntactic sugar … If you define path lenses as extensions of Path like this…
Copy code
val Path.bob by lazyOf(Path.value(Bob).of("bob"))
Then using them in routes can get quite verbose…
Copy code
"/${Path.alice}/${Path.bob}/${Path.carol}" bind ...
But the following inline function lets you refer to them without the Path. prefix and therefore without curly braces:
Copy code
inline operator fun Path.invoke(mkPath: Path.() -> String) = mkPath()
Now I can write bindings like…
Copy code
Path { "/$alice/$bob/$carol" } bind ...
K 3
d
Further to this, we have just released v6.24.0.0 in which you can do the following, and thus allows you to omit the repetition of the name entirely (at the cost of allowing reflection):
Copy code
val Path.foo by <http://Path.int|Path.int>()
assertThat(Path { "bar/${foo}" }, equalTo("bar/{foo}"))
a
I imagine that Nat's
lazy
delegate is far more efficient than what I've previously done to avoid polluting the class's companion object or global namespace. I know the JVM is very good at collecting recently created immutable garbage, but the instantiations must add up at scale.
Copy code
val DeviceId.Companion.pathLens get() = Path.value(DeviceId).of("device_id")
n
TBH I’ve never measured it or compared the generated bytecode