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

calling default lambda only once per delegate #69

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/main/kotlin/com/xenomachina/argparser/Default.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ fun <T> ArgParser.DelegateProvider<T>.default(newDefault: () -> T): ArgParser.De
/**
* Returns a new `Delegate` with the specified default value.
*
* @param newDefault the default value for the resulting [ArgParser.Delegate]
* @param defaultValue the default value for the resulting [ArgParser.Delegate]
*/
fun <T> ArgParser.Delegate<T>.default(defaultValue: T): ArgParser.Delegate<T> = default { defaultValue }

/**
* Returns a new `Delegate` with the specified default value as a lambda.
*
* @param newDefault the default value for the resulting [ArgParser.Delegate]
* @param defaultValue the default value for the resulting [ArgParser.Delegate]
*/
fun <T> ArgParser.Delegate<T>.default(defaultValue: () -> T): ArgParser.Delegate<T> {
if (hasValidators) {
Expand All @@ -56,6 +56,8 @@ fun <T> ArgParser.Delegate<T>.default(defaultValue: () -> T): ArgParser.Delegate

return object : ArgParser.Delegate<T>() {

private val computedDefault by lazy(defaultValue)

override val hasValidators: Boolean
get() = inner.hasValidators

Expand All @@ -71,7 +73,7 @@ fun <T> ArgParser.Delegate<T>.default(defaultValue: () -> T): ArgParser.Delegate
override val value: T
get() {
inner.parser.force()
return if (inner.hasValue) inner.value else defaultValue()
return if (inner.hasValue) inner.value else computedDefault
}

override val hasValue: Boolean
Expand Down
38 changes: 38 additions & 0 deletions src/test/kotlin/com/xenomachina/argparser/DefaultTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright © 2018 Tobias Berger
//
// This file is part of kotlin-argparser, a library which can be found at
// http://github.com/xenomachina/kotlin-argparser
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published by the
// Free Software Foundation; either version 2.1 of the License, or (at your
// option) any later version.
//
// This library is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
// for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this library; if not, see http://www.gnu.org/licenses/

package com.xenomachina.argparser

import io.kotlintest.shouldBe
import io.kotlintest.specs.FunSpec

class DefaultTest : FunSpec({
test("Single call on default lambda") {
val parser = parserOf()

var callCount = 0
val testDelegate by parser.storing("-t") { toInt() }
.default { ++callCount }

callCount shouldBe 0
testDelegate shouldBe 1
callCount shouldBe 1
testDelegate shouldBe 1
callCount shouldBe 1
}
})