supercollider icon indicating copy to clipboard operation
supercollider copied to clipboard

PopUpMenu.item throws errors if item list is empty

Open daveriedstra opened this issue 1 year ago • 2 comments

Attempting to access an item from PopUpMenu will fail with an error if the item list is either non-instantiated or empty. The error message is the same as attempting to access an element of an non-instantiated or empty collection, which makes debugging difficult and time-consuming. This can come up when the menu item list is automatically populated and there is a chance it could be empty (in my case, selecting a hardware peripheral).

Environment

  • SuperCollider version: 3.11.2
  • Operating system: Linux (Pop!_OS 22.04)

Steps to reproduce

m = PopUpMenu();
m.item.postln; // fails with "Message 'at' not understood"
m.items = [];
m.item.postln; // fails with "Primitive '_BasicAt' failed."

Expected vs. actual behavior

Expected: return nil with no error or fail with a more informative error Actual: misleading error

daveriedstra avatar Oct 07 '24 15:10 daveriedstra

Could be fixed by overwriting the item getter in the PopUpMenu class, so something along

	item {
		^if(this.value.isNil, {
			nil
		}, {
			items.at( this.value )
		});
	}

Do you want to make a PR, @daveriedstra ?

capital-G avatar Oct 08 '24 20:10 capital-G

I'd be glad to! It might be a while before I have the time though, so if someone else gets to it first that's fine too :)

daveriedstra avatar Oct 08 '24 21:10 daveriedstra