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

Feature request: lazy labels #969

Open
noresttherein opened this issue Jun 6, 2023 · 1 comment
Open

Feature request: lazy labels #969

noresttherein opened this issue Jun 6, 2023 · 1 comment

Comments

@noresttherein
Copy link

Evaluating labels, if they contain formatted input data, can be somewhat expensive. I'd love to see sibling methods, which take a => String argument. I solved it with a very ugly - but working - hack, but it would be great to see it integrated propely.

	private class LazySet[T](lzy : => Set[T]) extends AbstractSet[T] {
		private lazy val evaluated = lzy
		override def contains(elem :T) :Boolean = evaluated.contains(elem)
		override def incl(elem :T) :Set[T] = new LazySet(evaluated + elem)
		override def excl(elem :T) :Set[T] = new LazySet(evaluated - elem)
		override def iterator :Iterator[T] = evaluated.iterator
		override def empty = new LazySet[T](Set.empty[T])
	}

	implicit class PropExtension(private val self :Prop) extends AnyVal {
		def lbl(l : => String) :Prop = self.map { res =>
			res.copy(labels = new LazySet(res.labels + l))
		}
		@inline def lbl_:(l: => String) :Prop = lbl(l)


		def orElse(p : => Prop) = 
			self.combine(Prop.secure(p)) { (first, second) =>
			if (first.failure && second.status.isInstanceOf[Prop.Exception])
				err.println(first.toString + " orElse " + second)
			if (first.success) first
			else second
		}
	}

	implicit class BooleanExtension(private val self :Boolean) extends AnyVal {
		private def prop = Prop(self)

		def lbl(l : => String) :Prop = prop.map { res =>
			res.copy(labels = new LazySet(res.labels + l))
		}
		@inline def lbl_:(l : => String) :Prop = lbl(l)

		def orElse(p : => Prop) = 
			prop.combine(Prop.secure(p)) { (first, second) =>
				if (first.failure && second.status.isInstanceOf[Prop.Exception])
					err.println(first.toString + " orElse " + second)
				if (first.success) first
				else second
			}
	}
@mrdziuban
Copy link

@noresttherein I had the same thought! Opened a PR at #979

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants