Skip to content

[parser4k] Add by delegates for .with caching #6

@duangsuse

Description

@duangsuse
object MinimalCalculator {
    val cache = OutputCache<BigDecimal>()
    fun binaryExpr(s: String) = inOrder(ref { expr }, token(s), ref { expr })

    val number = oneOrMore(oneOf('0'..'9')).map { it.joinToString("").toBigDecimal() }.with(cache)
}

why not add getValue(k, p, self) for OutputCache:

class OutputCacheBy<out T>(private val p: Parser<T>) {
  fun getValue(k: KClass<*>, p: KProperty<*>, self: OutputCache) = p.with(self)
}

and then:

object MinimalCalculator {
    val cache = OutputCache<BigDecimal>()
    fun binaryExpr(s: String) = inOrder(ref { expr }, token(s), ref { expr })

    val number = by cache { oneOrMore(oneOf('0'..'9')).map { it.joinToString("").toBigDecimal() } }
}

Eh... looks ugly, but in real projects, I think p.with(cache) should have a shorter expression (maybe with a CachedParsers baseclass ext-fun?).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions