I think that it may complain about the first g[T] proc. It only works in the case T = B but unless that's the case you never create the variable l. So either you have to indent the echo statement to be within the when statement or you add a else statement to the when statement to handle the case where T isn't B, and define the variable l there as well.
Writing this from my phone so can't really write the code but I hope you get the point.