gpt4 book ai didi

android - Koin中的DSL有什么用?

转载 作者:行者123 更新时间:2023-11-29 18:25:45 25 4
gpt4 key购买 nike

在 dagger 2 中,我们在编译时生成了代码。根据Koin library website , Koin 不做任何代码生成,是一个“DSL,一个轻量级容器和一个实用的 API”。

After reading Martin Fowlers Blog regarding DSL ,在我看来,DSL 可以生成代码,或在运行时解释。

来自马丁斯的文章:

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.

但是如果 Koin 不生成任何代码,Koin 是在运行时解释的吗?这是否意味着 Koin 带有某种解析器?正如我所看到的,没有解析器,那么是否意味着 Kotlin 本身就是解析器?

谢谢

最佳答案

内部外部 DSL。引用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.

外部 DSL(有时,并非总是)涉及代码生成; Koin 是一种内部 DSL,但它不是。

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.

这是外部 DSL 的两个选项。

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

是的。

written differently, interpreted at runtime.

没有。它只是 Kotlin,使用 Kotlin 编译器编译。内部 DSL 和库之间没有主要区别;如果它使您的代码具有足够的可读性,则可以将其视为 DSL。

关于android - Koin中的DSL有什么用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59141999/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com