Search code examples
androidkotlindependency-injectiondagger-2koin

What is the use of DSL in Koin?


In dagger 2, we had code generation at compile time. According to the Koin library website, Koin doesn't do any code generation and is a "DSL, a lightweight container and a pragmatic API".

After reading Martin Fowlers Blog regarding DSL, It appears to me that DSL can be generated into code, or interpreted at run-time.

From Martins article:

DSLs can be implemented either by interpretation or code generation. Interpretation (reading in the DSL script and executing it at run time) is usually easiest, but code-generation is sometimes essential. Usually the generated code is itself a high level language, such as Java or C.

But if Koin doesn't generate any code, Is Koin interpreted at runtime? Does it mean Koin comes with some kind of parser? As I have seen, there is no parser, so does it mean that Kotlin itself is the parser?

Thank you


Solution

  • There are internal and external DSLs. To quote Fowler:

    DSLs come in two main forms: external and internal. An external DSL is a language that's parsed independently of the host general purpose language: good examples include regular expressions and CSS. External DSLs have a strong tradition in the Unix community. Internal DSLs are a particular form of API in a host general purpose language, often referred to as a fluent interface. The way mocking libraries, such as JMock, define expectations for tests are good examples of this, as are many of the mechanisms used by Ruby on Rails. Internal DSLs also have a long tradition of usage, particularly in the Lisp community.

    It is external DSLs which (sometimes, not always) involve code generation; Koin is an internal DSL, which doesn't.

    Following Martin Fowler's blog https://martinfowler.com/bliki/DomainSpecificLanguage.html He says that DSL's can either be converted to code(code generation). Or it can be interpreted at run time.

    These are two options for external DSLs.

    If I understand you right, it means Koin DSL is simply Kotlin

    Yes.

    written differently, interpreted at runtime.

    No. It's just Kotlin, compiled using the Kotlin compiler. There's no major distinction between internal DSLs and libraries; if it makes your code readable enough, it can be considered a DSL.