KTLN-153 | Examples for article + Upgrade Kotlin version to 1.4.0 (#10108)
* KTLN-153 | Examples for article + Upgrade Kotlin version to 1.4.0 * KTLN-153 | UNDO Upgrade to Kotlin 1.4.0 * KTLN-153 | Update examples to match the article edits * KTLN-153 | Update examples to match the article edits - 2 Co-authored-by: tpurdeva <ramprasad.devarakonda@waitrose.co.uk>
This commit is contained in:
parent
b63aad5048
commit
2d128104d4
@ -0,0 +1,37 @@
|
||||
package com.baeldung.arguments
|
||||
|
||||
fun main() {
|
||||
|
||||
// Skip both the connectTimeout and enableRetry arguments
|
||||
connect("http://www.baeldung.com")
|
||||
|
||||
// Skip only the enableRetry argument:
|
||||
connect("http://www.baeldung.com", 5000)
|
||||
|
||||
// Skip only the middle argument connectTimeout
|
||||
// connect("http://www.baeldung.com", false) // This results in a compiler error
|
||||
|
||||
// Because we skipped the connectTimeout argument, we must pass the enableRetry as a named argument
|
||||
connect("http://www.baeldung.com", enableRetry = false)
|
||||
|
||||
// Overriding Functions and Default Arguments
|
||||
val realConnector = RealConnector()
|
||||
realConnector.connect("www.baeldung.com")
|
||||
realConnector.connect()
|
||||
}
|
||||
|
||||
fun connect(url: String, connectTimeout: Int = 1000, enableRetry: Boolean = true) {
|
||||
println("The parameters are url = $url, connectTimeout = $connectTimeout, enableRetry = $enableRetry")
|
||||
}
|
||||
|
||||
open class AbstractConnector {
|
||||
open fun connect(url: String = "localhost") {
|
||||
// function implementation
|
||||
}
|
||||
}
|
||||
|
||||
class RealConnector : AbstractConnector() {
|
||||
override fun connect(url: String) {
|
||||
println("The parameter is url = $url")
|
||||
}
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
package com.baeldung.arguments
|
||||
|
||||
fun main() {
|
||||
resizePane(newSize = 10, forceResize = true, noAnimation = false)
|
||||
|
||||
// Swap the order of last two named arguments
|
||||
resizePane(newSize = 11, noAnimation = false, forceResize = true)
|
||||
|
||||
// Named arguments can be passed in any order
|
||||
resizePane(forceResize = true, newSize = 12, noAnimation = false)
|
||||
|
||||
// Mixing Named and Positional Arguments
|
||||
// Kotlin 1.3 would allow us to name only the arguments after the positional ones
|
||||
resizePane(20, true, noAnimation = false)
|
||||
|
||||
// Using a positional argument in the middle of named arguments (supported from Kotlin 1.4.0)
|
||||
// resizePane(newSize = 20, true, noAnimation = false)
|
||||
|
||||
// Only the last argument as a positional argument (supported from Kotlin 1.4.0)
|
||||
// resizePane(newSize = 30, forceResize = true, false)
|
||||
|
||||
// Use a named argument in the middle of positional arguments (supported from Kotlin 1.4.0)
|
||||
// resizePane(40, forceResize = true, false)
|
||||
}
|
||||
|
||||
fun resizePane(newSize: Int, forceResize: Boolean, noAnimation: Boolean) {
|
||||
println("The parameters are newSize = $newSize, forceResize = $forceResize, noAnimation = $noAnimation")
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user