This file contains 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 person = Person().apply { | |
name = "Tony Stark" | |
age = 52 | |
// More such stuff | |
} |
This file contains 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
FileReader("input.txt").use { | |
// Read the file | |
} | |
// Automatically closed |
This file contains 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
try (FileReader reader = new FileReader("input.txt")) { | |
// Read the file | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
// Automatically closed |
This file contains 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 person = Person().also { | |
it.name = "Tony Stark" | |
it.age = 52 | |
// More such stuff | |
} |
This file contains 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 person = Person().also { p -> | |
p.name = "Tony Stark" | |
p.age = 52 | |
// More such stuff | |
} |
This file contains 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 index = "Kotlin".indexOf('K').takeIf { it > 0 } ?: 0 |
This file contains 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 index = "Kotlin".indexOf('K').takeUnless { it < 0 } ?: 0 |
This file contains 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 person = with(Person()) { | |
name = "Tony Stark" | |
age = 52 | |
// More such stuff | |
this | |
} |
This file contains 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
class UserViewModel() : ViewModel() { | |
/** | |
* The user | |
*/ | |
private var users: LiveData<List<User>> | |
init { | |
// Load the user over here | |
// users = .... |
This file contains 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
class MainActivity : LifecycleActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
// Get the ViewModel instance | |
val userViewModel = ViewModelProviders.of(this).get(UserViewModel::class.java) | |
userViewModel.getUsers().observe(this, Observer { | |
// Update the UI |