Hello, I have multiple kotlin gradle modules, let’...
# getting-started
k
Hello, I have multiple kotlin gradle modules, let’s say library: A and B (and B depends on A). In module A I want to make some data/inline class with field, but only my libraries can access to that field. What is the best way to achieve that? 🤔
j
Not entirely sure what refer to as field, but most probably sounds like you want to use private or internal visibility on the fields? 🙂 In some cases if nested deps, can also decouple with interfaces as well in combination 🙂
k
I can give an example: In module A:
Copy code
data class X(val type: Int)
I want to have an access to
type
field in module B, but not for other consumers. In this example
internal
simply not working. Maybe there are some other annotations or something else that I can specify that this field is for internal use of my libraries.
r
do you want to hide everything in module A or only specific classes? What you want seems to somewhat match the
api
vs
implementation
dependencies in the Gradle
java-library
plugin (assuming you're creating a Java library and not multiplatform). See: https://gradlehero.com/how-to-use-gradle-api-vs-implementation-dependencies-with-the-java-library-plugin/
m
A common option is to use an OptIn annotation that says it is internal. It won't stop people from accessing it, but it will tell them that are not supposed to do it, and they have to actually opt in to doing it.
👍 1
👍🏻 1
c
Another option is to have an internals module that both modules depend on. The second module can depend on it with
implementation
to avoid exposing it as part of its own API. However, I wouldn't really recommend it, it makes everything more complicated and you increase your risks of binary incompatibilities if you don't enforce platforms. What is your problem exactly? In general, you shouldn't need this: each module should be completely independent and only expose what other modules need. It seems you want to have a module to secretly expose a part of its internals…
k
I’m writing game engine. One module is my engine api module in which I made wrapper around handle to the windowing system. I separated the renderer module to be easily swapped to another one, but I need that handle to initialise renderer context using this windowing API. So my wrapper should be in the public API, but it’s internal state I mean field should be used only internally. And that’s my whole story.
c
If you were to create a new renderer, wouldn't it need the handle too? If so, the handle should probably be a part of your public API