Explanation of Kotlin Syntax for Ktor in Application.kt - kotlin

I'm new to Kotlin coming from the Python world, and wanted to get into Web Development with Kotlin with Ktor.
Now I've started the tutorial https://ktor.io/docs/creating-interactive-website.html#running-our-application-for-the-first-time and already I don't understand quite a few things.
Looking at the code for Application.kt, which is
package com.jetbrains.handson.website
import freemarker.cache.*
import freemarker.core.HTMLOutputFormat
import io.ktor.application.*
import io.ktor.freemarker.*
import io.ktor.html.respondHtml
import io.ktor.http.HttpStatusCode
import io.ktor.http.content.*
import io.ktor.request.receiveParameters
import io.ktor.response.*
import io.ktor.routing.*
import kotlinx.html.*
fun main(args: Array<String>): Unit = io.ktor.server.netty.EngineMain.main(args)
fun Application.module() {
routing {
static("/static") {
resources("files")
}
}
}
I don't understand what the syntax means. I'm assuming module is an extension function for the class Application? But what does routing and static mean. Can someone explain the concepts happening there, or maybe just name them so I can google them.
Thanks in advance :-)
Edit: For clarification. I know where to look for when it comes to routing concepts and such for ktor. What I am asking is what routing is. Is it a function? But if so what parameters does it take, it does not look like a normal function definition and it is declared inside another function. When I look at the types it seems it takes a lambda function with type Routing.() -> Unit. What does Routing.() mean...
That's what I don't understand.

The routing is an extension function of the Application that installs the Routing plugin and configures it using a provided lambda. The Route.() -> Unit is a function type with the Route receiver (this has type Route inside this function) that has no parameters and that returns Unit (nothing). For more information please read the official Kotlin documentation. So it's the build pattern in action, that allows writing DSLs like Ktor's routing.

Related

Random.nextInt() unresolved reference

I get unresolved reference on either
Random
or .nextInt()
when using any of those imports import java.util.* import kotlin.random.*
on Android Studio with Kotlin.
I've used both import java.util.* and
import kotlin.random.* and the unresolved reference error appears on either the Class or the method. How do I import Kotlin.random properly?
kotlin.random and its associated classes are actually included in the Kotlin standard library, so your existing reference should be working:
import kotlin.random.Random;
fun main() {
println(Random.nextInt())
}
What it sounds like is that the appropriate references/libraries for Kotlin and/or Java aren't being properly loaded for your project. Depending on your environment (e.g. Gradle, Maven, etc.), you may want to force a compile or install step to ensure the dependencies are available.

Can I use or import the functions in CharJVM.kt file which is inline function collection defined by the Kotlin platform?

I want to make my custom inline function in .kt file using checkRadix function already implemented by Kotlin.
But I cannot import it. How can I import and use it?
I tried
import kotlin.jvm.JvmMultifileClass.*
kotlin.jvm.JvmMultifileClass.checkRadix(radix)
But I can't compile and there is no recommended resolution by IDE.
That function is marked as internal, which means it's only available within that module — i.e. within the Kotlin stdlib, not to your code.
I don't know why it's marked like that; maybe JetBrains consider it an implementation detail.  But they clearly don't want it being used by any other code.
(Of course, it wouldn't be hard to reimplement yourself.)

No kotlin.Math class in kotlin 1.2 as it is said in the documentation

I have been dealing with kotlin multiplatform alot recently, and I totaly understand the nature of the development. Initially, I had my own expected Math class (in a common module) and I had actual classes in the JS and JVM environment.
Since I like to read the documentations, I found that the Math liblary has been added to the standard liblary since kotlin 1.2. this trouble me as I am using kotlin 1.2.51 and I get an error trying to access the class from kotlin.Math in my common module and any of my platform specific module.
What am I not geting? How do I get access to the kotlin.Math class in my common module?
The Math-class is deprecated and the deprecated-message contains:
Use top-level functions from kotlin.math package instead.
(see also https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.js/-math/index.html)
So somehow the answer of #mTak is right, even though it was not mentioned, that you should use the kotlin.math.*-import instead of your Math-class.
Alternatively, you can also import kotlin.math.max, etc. depending on which function you actually require.
The more I think of it: I don't know whether there ever was a Math-class in the jvm-variant of Kotlin (couldn't find anything regarding it) and so in a multiplatform project the Math-class-access probably should always have failed.
Import it like this: import kotlin.math.*
In the Kotlin standard library math functions are provided as top-level functions in the kotlin.math package.
Therefore you need to import that package and then you'll be able to use functions from it, like sin, sqrt and so on.
import kotlin.math.*
val sqrt2 = sqrt(2.0)
You can also import functions one by one, e.g. import kotlin.math.sqrt or even call them fully qualified val result = kotlin.math.sqrt(2.0)
After a while (I even feel stupid). I found that the kotlin.math library in kotlin common modules was already added. The only difference was, it had no the 'Math.' predecessor as I am normally used to.
So, Math.round(x: Float) was just round(x: Float)
Math.sin(x: Float) was just sin(x: Float)

Plugin programming on golang

I am coding my project with golang. I want to design my project like plugin programming but I have confused with golang. My project have data analysis task, I purpose make a folder that contain modules analysis, if I have new modules after, I just need copy it to folder and main application will pass data to the new modules without modified.
Can you help me? Thanks for watching!
Go with it's interfaces is a good choice to build something pluggable.
Interfaces
Interface in Go is a way of types abstraction.
Interfaces in Go provide a way to specify the behavior of an object: if something can do this, then it can be used here.
It means a simple but very powerful thing - you can use different composite literals as an interface type if they implement the interface. This is already a pluggable system and if are looking for something simple it is possible to build with minimal effort.
Simplest implementation
Let's say you have such a primitive architecture:
▾ pluggable/
▾ app/
pusher.go
▾ plugins/
green_button.go
plugin_interface.go
red_button.go
main.go
plugins/plugin_interface.go
An interface which will be used as a type abstraction for plugins.
package plugins
type Button interface {
Push()
}
plugins/green_button.go
Now it is possible to extend your application with plugins implement the interface.
package plugins
import (
"fmt"
)
type GreenButton struct {
Msg string
}
func (b GreenButton) Push() {
fmt.Println(b.Msg)
}
plugins/red_button.go
One more plugin...
package plugins
import (
"fmt"
)
type RedButton struct {
Err error
}
func (b RedButton) Push() {
fmt.Println(b.Err)
}
app/pusher.go
An interface type embedded in the composite literal. Any cl implementing the interface could be used in the Pusher instance. Pusher doesn't care about particular plugin implementation it just pushes. It is well abstracted and encapsulated.
package app
import (
"github.com/I159/pluggable/plugins"
)
type Pusher struct {
plugins.Button
}
main.go
Usage of all the stuff.
package main
import (
"errors"
"github.com/I159/pluggable/app"
"github.com/I159/pluggable/plugins"
)
func main() {
alert_pusher := app.Pusher{plugins.RedButton{Err: errors.New("Alert!")}}
success_pusher := app.Pusher{plugins.GreenButton{Msg: "Well done!"}}
alert_pusher.Push()
success_pusher.Push()
}
You can add more sugar, for example one more level of isolation to use a single button configured to be a one or another particular implementation and so on.
Plugins as libraries
Same trick with plugin-libraries. A composite literals declared in a plugin-libraries must implement a plugin interface of the main application. But in this case you will need a register a function and a file with libs imports, so it already looks like a nano plugin framework.
Go 1.8 has support for plugins: https://beta.golang.org/pkg/plugin/. If you can wait a couple months you could use that (or just use the beta 1.8).

If Aurelia understands "import", why use dependency injection?

I don't understand.. if I can use import in Aurelia, why do I have to wire up the constructor with #autoinject() and all that? I'm sure I'm missing something, but, from what I can tell, I can just use my imported module whenever I want.
import something from "whatever"
export class SomeViewModel {
activate() {
// use something
}
}
Typically, in an Aurelia application, the thing you are importing isn't an instance of Something it's the class Something. To actually use whatever has been imported, you need an instance of it.
import Something from 'whatever';
let something = new Something();
When you use Aurelia's Dependency Injection system, you are utilizing a design pattern called "Inversion of Control." Instead of your class (or you) being in charge of instantiating its dependencies, it lists what dependencies it has and then has instances of the dependencies injected in to its constructor function.
This helps with testability, as now you can pass mocked instances of the dependencies to a class in your test fixtures (note that in your tests, your tests will pass the mocks to the constructor, and not rely on Aurelia's DI container).This also allows you to tap in to the Dependency Injection container's ability to be configured to create dependencies using different object lifestyles such as singletons and transient.
--- Edits to answer OP's questions from comments ---
If I import a module defined as export default class Something into an
aurelia view model using constructor injection, it does not need to be
instantiated. It is an instance of the class Something.
This is because Aurelia's Dependency Injection container is instantiating an instance for you. This is why your code looks like this:
import {inject} from 'aurelia-framework';
import Something from 'somewhere';
#inject(Something)
export class Foo {
constructor(something) {
this.something = something;
}
//...
}
and not
import Something from 'somewhere';
export class Foo {
constructor(Something) {
this.something = something;
}
//...
}
You are telling Aurelia "I need one of these, please give it to me," and Aurelia says "Sure thing, I've created one or I already had one lying around, here it is."
In other words, it appears that aurelia's constructor DI only works
with class exports, and it does instantiate the class. It looks like
if I want to import something like moment js into my aurelia view
model, I should just continue doing things the way I've always done
them (not using aurelia's DI). Does that sound correct?
This is correct. Libraries like moment give you a function to use, and not a class that can be instantiated by Aurelia. For these you would continue to use them as in the past.
Well technically you can use the imported modules without Aurelia's DI, but in most situations that would be a bad thing. The Dependency Injection layer gives you so much versatility and flexibility. It handles caching, it supports singleton and transient dependencies, handles lifetime and makes thing neater from an architectural perspective.