Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KT-20357 - Added samples for property delegates. #1426

Closed
wants to merge 5 commits into from

Conversation

@AdamMc331
Copy link
Contributor

AdamMc331 commented Dec 12, 2017

Adding some standard lib samples to go with https://youtrack.jetbrains.com/issue/KT-20357.

@AdamMc331 AdamMc331 changed the title Added samples for property delegates. KT-20357 - Added samples for property delegates. Dec 12, 2017
@Sample
fun vetoableDelegate() {
var max: Int by Delegates.vetoable(0) { property, oldValue, newValue ->
newValue > oldValue

This comment has been minimized.

Copy link
@ilya-g

ilya-g Dec 28, 2017

Contributor

Another common use case of vetoable is to throw an exception before the new value is assigned if it is invalid. It could be illustrated in a separate sample.

This comment has been minimized.

Copy link
@AdamMc331

AdamMc331 Dec 30, 2017

Author Contributor

Added this in throwVetoableDelegate() sample.

var max: Int by Delegates.notNull()

assertFailsWith<IllegalStateException> { println(max) }
}

This comment has been minimized.

Copy link
@ilya-g

ilya-g Dec 28, 2017

Contributor

nonNullDelegate and notNullAccessError can be merged in one sample.

@Sample
fun throwVetoableDelegate() {
var max: Int by Delegates.vetoable(0) { property, oldValue, newValue ->
if (newValue > oldValue) true else throw(Throwable("New value must be larger than old value."))

This comment has been minimized.

Copy link
@ilya-g

ilya-g Jan 9, 2018

Contributor

Usually IllegalArgumentException is used in this case. Also there are redundant parentheses here.

@ilya-g

This comment has been minimized.

Copy link
Contributor

ilya-g commented Jan 11, 2018

Thanks, I've squashed and merged it with minor edits (assertTrue/assertFalse instead of assertPrints for booleans)

@ilya-g ilya-g closed this Jan 11, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

2 participants
You can’t perform that action at this time.