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

Proposal: calling a reactive value could be a combined getter/setter #1423

Open
gadenbuie opened this issue May 24, 2024 · 0 comments
Open

Comments

@gadenbuie
Copy link
Collaborator

A reactive.value instance is a callable object, but the .__call__() method is an alias for the reactive value's .get() method.

def __call__(self) -> T:
return self.get()

The __call__ method could take an optional value that's used to set the reactive value, which would align Shiny for Python's reactive.value with R's reactiveVal(). This would reduce code switching costs for those of us who are used to R's pattern, without interfering with anyone who prefers the .get() and .set() methods approach.

The biggest challenge is likely typing, in particular because the .set() method returns True or False if it has triggered a reactive invalidation or resulted in a no-op. I propose we resolve this by having the __call__ method return either the new value (it'd be nice if we could do this invisibly, by which I mean in a way that doesn't print in Shiny Express) or nothing at all.

# return nothing if setting ----
    def __call__(self, value: MISSING_TYPE = MISSING) -> T | None: 
		if isinstance(value, MISSING_TYPE):
			return self.get()
		else:
			self.set(value)

# return value
    def __call__(self, value: MISSING_TYPE = MISSING) -> T: 
		if isinstance(value, MISSING_TYPE):
			return self.get()
		else:
			self.set(value)
			return value
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

1 participant