超越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.pswp.cn/news/394891.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/394891.shtml
英文地址,請注明出處:http://en.pswp.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…