超越Android:探索Kotlin的应用领域

by Adam Arold

亚当·阿罗德(Adam Arold)

超越Android:探索Kotlin的应用领域 (Going beyond Android: exploring Kotlin’s areas of application)

If you have written something in Kotlin, chances are that you wrote it for Android. Kotlin, however, has other areas where it can be useful. In the following series, we’ll explore in what other fields Kotlin can shine and discuss how you can take advantage of them.

如果您使用Kotlin编写过某些内容,则很可能是您为Android编写的。 但是,Kotlin在其他领域可能会有用。 在以下系列中,我们将探讨Kotlin在其他领域可以发挥的作用,并讨论如何利用它们。

Even though Kotlin started out as a language for the JVM, its creators have started to give us options for using the language on other platforms as well. The Kotlin Frontend Plugin is nothing new, but now we can go native or create multiplatform projects.

即使Kotlin最初是作为JVM的一种语言,它的创建者也开始为我们提供在其他平台上使用该语言的选择。 Kotlin前端插件并不是什么新鲜事物,但是现在我们可以使用本机或创建多平台项目了 。

What is also interesting is that on the JVM you can also use Kotlin on the backend with great effect. Using Spring with Kotlin is becoming easier with the advent of Spring 5 which has built-in support for Kotlin, but you can choose from a variety of technologies, like vert.x, RxKotlin or even tools written in Kotlin like Hexagon.

同样有趣的是,在JVM上,您也可以在后端使用Kotlin,效果非常好。 随着Spring 5的内置支持 ,Spring与Kotlin结合使用变得越来越容易,但是您可以选择多种技术,例如vert.x , RxKotlin或什至是用Kotlin编写的工具(例如Hexagon) 。

后端的Kotlin (Kotlin on the backend)

As I have written before, I think that the interop between Java and Kotlin is quite seamless. This also means that using Kotlin in place of Java on the backend is rather easy. Apart from a few nuisances, you can pretty much start writing your new features in Kotlin within your Java project. Or if you just want to try it out, you can start by writing your tests with it.

正如我之前写的 ,我认为Java和Kotlin之间的互操作是相当无缝的。 这也意味着在后端使用Kotlin代替Java很容易。 除了一些麻烦之外,您几乎可以开始在Java项目中的Kotlin中编写新功能。 或者,如果您只想尝试一下,则可以开始编写测试。

If you look around, it seems that companies with a big slice of the backend pie also have the same thought: the new version of Spring has some features dedicated to Kotlin, and you can even use Kotlin to write your Gradle scripts using the kotlin-dsl.

如果环顾四周,似乎后端派中有很大一部分的公司也有相同的想法:Spring的新版本具有一些Kotlin专用的功能,您甚至可以使用Kotlin使用kotlin-编写Gradle脚本- dsl 。

What is interesting to note here is that you don’t need Kotlin support for any of these libraries, because the Java interop features of Kotlin are so good. In the next article in this series, we’ll explore how you can write backend code with and without libraries written in Kotlin. We’ll also look into how you can tinker with your existing programs written in Java.

这里有趣的是您不需要这些库中的任何一个Kotlin支持 ,因为Kotlin的Java互操作性非常好。 在本系列的下一篇文章中,我们将探讨如何使用和不使用Kotlin编写的库来编写后端代码。 我们还将研究如何修改Java编写的现有程序。

将Kotlin编译为Javascript (Compiling Kotlin to Javascript)

When trying to compile Kotlin to Javascript, you have two options: the kotlin2js plugin and the kotlin-frontend-plugin.

尝试将Kotlin编译为Javascript时,您有两个选择: kotlin2js插件和kotlin-frontend-plugin

The former is a simple way to turn your code to JS without the hassle of managing external dependencies. It just works out of the box and results in a .js file which you can copy to your static assets folder.

前者是将代码转换为JS的简单方法,而无需管理外部依赖项。 它开箱即用,并生成一个.js文件,您可以将其复制到静态资产文件夹中。

The latter is a little more involved, but it lets you use both Maven and npm dependencies.

后者要复杂一些,但是它可以让您同时使用Maven和npm依赖项。

With the use of these, you can easily go full stack — but they do not let you share code between your backend and your frontend project. Check out this TodoMVC implementation which I have written if you are interested in how this works.

使用这些功能,您可以轻松地进行完整堆栈处理-但是它们不允许您在后端项目和前端项目之间共享代码。 如果您对此工作原理感兴趣,请查看我编写的此 TodoMVC实现。

走向本土 (Going native)

Have you ever tried running Kotlin in an embedded environment or compiled it into a single binary? Enter Kotlin Native which lets you do just that:

您是否曾经尝试过在嵌入式环境中运行Kotlin或将其编译为单个二进制文件? 输入Kotlin Native,您可以执行以下操作:

Kotlin/Native is an LLVM backend for the Kotlin compiler, runtime implementation, and native code generation facility using the LLVM toolchain.

Kotlin / Native是LLVM后端,用于LLVM工具链的Kotlin编译器,运行时实现和本机代码生成工具。

While this is still in a pre-release version (0.4 at the time of writing) Kotlin Native is a promising development. It tries to fill in the holes which are currently present, and lets you use Kotlin in some areas where it was not feasible to do so, like:

尽管它仍处于预发行版本中(在撰写本文时为0.4 ),但Kotlin Native是一个有前途的开发。 它尝试填补当前存在的漏洞,并让您在不可行的某些区域使用Kotlin,例如:

  • iOS applications (reusing code with Android)

    iOS应用程序(在Android中重用代码)
  • Embedded systems/IoT (e.g., Arduino and beyond)

    嵌入式系统/物联网(例如,Arduino和更高版本)
  • Data analysis and Scientific Computing

    数据分析与科学计算
  • Server-side and Microservices (low-footprint executables, utilizing the power of coroutines)

    服务器端和微服务(占用空间较小的可执行文件,利用协程的强大功能)
  • Game Development

    游戏开发

多平台项目 (Multiplatform projects)

While it is all well and good that you can now use Kotlin on a multitude of platforms, what good does it do if you can’t wire your different codebases together? With the release of Kotlin 1.2, you can now share code between platforms reliably.

虽然现在可以在多种平台上使用Kotlin很好,但是如果您不能将不同的代码库组合在一起又有什么用呢? 随着Kotlin 1.2的发布 ,您现在可以在平台之间可靠地共享代码。

This works by dividing your codebase into common and platform modules and by using the expect + actual API. This lets you define classes and functions which will be implemented on each platform. You can take a look at this video by Dmitry Jemerov, who is the Kotlin IDE Team Lead at JetBrains, to get a better understanding of this topic.

通过将您的代码库划分为通用模块和平台模块,并使用Expect +实际 API,可以实现此目的。 这使您可以定义将在每个平台上实现的类和函数。 您可以观看JetBrains的Kotlin IDE团队负责人Dmitry Jemerov的这段视频 ,以更好地理解这一主题。

结论 (Conclusion)

We’ve explored some areas where Kotlin can shine as well as how you can glue together your multiplatform projects. In the next articles, we’ll look into each of these options in a little more detail.

我们探索了Kotlin可以发挥作用的一些区域,以及如何将多平台项目粘合在一起。 在下一篇文章中,我们将更详细地研究每个选项。

Thanks for reading! You can read more of my articles on my blog.

谢谢阅读! 您可以在我的博客上我的文章。

翻译自: https://www.freecodecamp.org/news/beyond-android-exploring-kotlin-areas-of-application-5d28ff66a446/

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/394891.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

3.SFB标准版前端安装

SFB服务器准备部分:1.修改服务器名称,sfb加入域,用域管理员账户登录2.配置服务器IP地址,DNS3.安装Windows组件Add-WindowsFeature NET-Framework-Core, RSAT-ADDS, Windows-Identity-Foundation, Web-Server, Web-Static-Content,…

向spark standalone集群提交任务

文档链接 #切换到spark安装目录,执行下面一条命令,192.168.0.10是master的ip, examples/src/main/python/pi.py 是python 文件的路径 ./bin/spark-submit --master spark://192.168.0.106:7077 examples/src/main/python/pi.py任务已经执行完毕,耗时10秒 转载于:https://www.c…

SQLite学习手册

一、聚合函数: SQLite中支持的聚合函数在很多其他的关系型数据库中也同样支持,因此我们这里将只是给出每个聚集函数的简要说明,而不在给出更多的示例了。这里还需要进一步说明的是,对于所有聚合函数而言,distinct关键字…

oracle全局索引 效率,关于插入,全局索引和局部索引的情况,那种效率高

分区表上的索引表可以按range,hash,list分区,表分区后,其上的索引和普通表上的索引有所不同,oracle对于分区表上的索引分为2类,即局部索引和全局索引,下面分别对这2种索引的特点和局限性做个总结…

python excelwriter保存路径_Python和Excel 终于可以互通了!!

点击“开发者技术前线”,选择“星标🔝”在看|星标|留言, 真爱作者:小天真_5eed 链接:https://www.jianshu.com/p/6ecf414f3372今天为大家分享一篇使用python将大量数据导出到Excel中的技巧心得,可以让Python和Excel…

nodejs 调用微服务器_无服务器NodeJS:构建下一个微服务的快速,廉价方法

nodejs 调用微服务器by Filipe Tavares由Filipe Tavares 无服务器NodeJS:构建下一个微服务的快速,廉价方法 (Serverless NodeJS: the fast, inexpensive way to build your next microservice) I love Node.js. I’ve re-discovered Javascript through…

(蓝桥杯)2018JAVA B组 日志分析

日志统计 小明维护着一个程序员论坛。现在他收集了一份"点赞"日志,日志共有N行。其中每一行的格式是: ts id 表示在ts时刻编号id的帖子收到一个"赞"。 现在小明想统计有哪些帖子曾经是"热帖"。如果一个帖子曾在任意一个长…

MySQL 导出数据

2019独角兽企业重金招聘Python工程师标准>>> 1、导出整个数据库 mysqldump -u 用户名 -p 数据库名 > 存放位置比如: mysqldump -u root -p project > c:/a.sql 2.导出一个表的结构,并且带表中的数据 mysqldump -u 用户名 -p 数据库名 …

哎 心好累

雨天后的周六还要上班,避开了所有上班的交通方式,没有比这更需要车的时候,哎,感觉心好累 好好努力买车吧,觉得再这样只能是徒劳了。 困得和傻逼一样 单片机又要换型号,后面一堆事儿,哎 再见-dsp…

Abbey加入了FreeCodeCamp团队,担任编辑

by Quincy Larson昆西拉尔森(Quincy Larson) Abbey加入了FreeCodeCamp团队,担任编辑 (Abbey is joining the freeCodeCamp team as an editor) Every article you’ve read here on the freeCodeCamp community Medium publication has been edited with care by a…

单片机STM8S测量电压电路_单片机电路设计中的10个难点

单片机是嵌入式系统的核心元件,使用单片机的电路要复杂得多,但在更改和添加新功能时,带有单片机的电路更加容易实现,这也正是电器设备使用单片机的原因。那么在单片机电路的设计中需要注意的难点有哪些?嵌入式ARM开发 …

oracle ebs 数据源,Oracle EBS环境下查找数据源(Form篇)

关于在Oracle EBS环境下如何查找数据源的文章几年前就已经开始整理,但是其中关于OAF方面的一直没有整理,导致这份文档一直残缺不全,有很多次同事都向我索要相关文档都未能如愿以偿,新的一届培训工作再次启动,为了自己也…

net-speeder

有的同学反映自己的***速度慢,丢包率高。这其实和你的网络服务提供商有关。据我所知一部分上海电信的同学就有这种问题。那么碰到了坑爹的网络服务商,我们应该怎么办呢? duangduang~~~~~~有请今天的主角:Net-Speeder登场&#xff…

linux 实用指令

通过init 来制定/切换不同的运行指令 查看linux 系统下,电脑的运行级别 vim /etc/inittab 如何找回丢失的root密码? 进入到单用户模式,然后修改root密码 进入到单用户模式,root不需要密码也可以登录 如果开机就是init 0 办法&…

Atitit.异步的实现模式attilax大总结

Atitit.异步的实现模式attilax大总结 1.1. 函数回调(包括的future模式)1 1.2. 事件机制( 包括定时器 listeners 1 1.3. 中断机制1 1.4. 订阅机制 发布/订阅 又称"观察者模式"(observer pattern)。1 1.5. Promises对象1 1.6. 轮询1 2. 实现级别…

区块链开发指南_区块链开发权威指南

区块链开发指南by Haseeb Qureshi由Haseeb Qureshi 区块链开发权威指南 (The authoritative guide to blockchain development) Cryptocurrencies, ICOs, magic internet money — it’s all so damn exciting, and you, the eager developer, want to get in on the madness.…

【BZOJ1831】[AHOI2008]逆序对(动态规划)

【BZOJ1831】[AHOI2008]逆序对(动态规划) 题面 BZOJ洛谷 题解 显然填入的数拎出来是不降的。 那么就可以直接大力\(dp\)。 设\(f[i][j]\)表示当前填到了\(i\),上一个填的数是\(j\)的最小逆序对数。 随便拿什么维护一下转移就好了。 #include&…

chrome控制台如何把vw显示成px_【CSS】rem,em,px的区别和使用场景

前端潮咖点击上面蓝字,关注我们!关注关注前端潮咖,每日精选好文作者:大前端小菜鸟来源:cnblogs.com/hyns/p/12380944.html作rem布局原理深度理解(以及em/vw/vh)一、前言我们h5项目终端适配采用的是淘宝那套《Flexible实…

php7对象转换成数组,php 如何把对象转换成数组对象

php把对象转换成数组对象的方法:首先打开相应的PHP代码文件;然后通过“function array_to_object($arr){...}”方法把对象转换成数组即可。本文操作环境:windows7系统、PHP7.1版,DELL G3电脑php-对象(object) 与 数组(array) 的转…

python中的线程之semaphore信号量

semaphore是一个内置的计数器 每当调用acquire()时,内置计数器-1 每当调用release()时,内置计数器1 计数器不能小于0,当计数器为0时,acquire()将阻塞线程直到其他线程调用release()。 来看下面的代码: import time imp…