This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package strings | |
fun String.lastChar(): Char = get(length - 1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> println("Kotlin".lastChar()) | |
n |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
newClass object = new newClass(null,null); | |
object.setfirstVal(10.8); | |
object.setsecondVal("example"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data class newClass (var firstVal:Any? ,var secondVal:Any?) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun test() { | |
var mood = "I am sad" | |
run { | |
val mood = "I am happy" | |
println(mood) // I am happy | |
} | |
println(mood) // I am sad | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// روش معمول | |
fun createIntent(intentData: String, intentAction: String): Intent { | |
val intent = Intent() | |
intent.action = intentAction | |
intent.data=Uri.parse(intentData) | |
return intent | |
} | |
// روش پیشرفته و زنجیری | |
fun createIntent(intentData: String, intentAction: String) = |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// روش معمول | |
fun createInstance(args: Bundle) : MyFragment { | |
val fragment = MyFragment() | |
fragment.arguments = args | |
return fragment | |
} | |
// روش پیشرفته | |
fun createInstance(args: Bundle) | |
= MyFragment().apply { arguments = args } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// روش معمول | |
fun makeDir(path: String): File { | |
val result = File(path) | |
result.mkdirs() | |
return result | |
} | |
// روش بهبودیافته | |
fun makeDir(path: String) = path.let{ File(it) }.also{ it.mkdirs() } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// روش معمول | |
fun makeDir(path: String): File { | |
val result = File(path) | |
result.mkdirs() | |
return result | |
} | |
// روش بهبودیافته | |
fun makeDir(path: String) = path.let{ File(it) }.also{ it.mkdirs() } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val original = "abc" | |
// مقدار دچار تغییر میشه و به قسمت بعدی زنجیره منتقل میشه | |
original.let { | |
println("The original String is $it") // "abc" | |
it.reversed() // دچار تغییر میشه و به عنوان پارامتر به قسمت بعدی زنجیره فرستاده میشه | |
}.let { | |
println("The reverse String is $it") // "cba" | |
it.length // همونطور که میبینید میتونه به نوع دیگری تبدیل بشه | |
}.let { |