How to use detectMsg

can someone post a quick explanation of how to use (Collection) detectMsg (please)

it’s in Collection.sc … but I can’t seem to decipher the implementation, I keep getting errors even with basic [1, 2, 3] syntax

I cannot say I understand what it can be used for, but at least this doesn’t throw an error:

(
var test = (
	\doSomething : { | self, a1, a2 | a1.debug("a1"); a2.debug("a2"); (a1 == a2).debug("check a1 == a2") },
);
var test2 = (
	\doSomething : { | self, a1, a2 | a1.debug("a1"); a2.debug("a2"); (a1 != a2).debug("check a1 != a2") },
); 
var test3 = (
	\doSomething : { | self, a1, a2 | a1.debug("a1"); a2.debug("a2"); (a1 < a2).debug("check a1 < a2") },
);
// iteration seems to stop at first succeeding test and returns that test
// compare this
List[test3, test, test2].detectMsg(\doSomething, 1, 2);
// to this
List[test, test2, test3].detectMsg(\doSomething, 1, 2);
// to check return values
(List[test3, test, test2].detectMsg(\doSomething, 1, 2) == test3).debug("return value is test3");
(List[test, test2, test3].detectMsg(\doSomething, 1, 2) == test2).debug("return value is test2");
)
1 Like