From: ptkwt@... (Phil Tomson) Date: 2006-02-18T05:18:38+09:00 Subject: Re: recontextualizing a block (looking for deep magic) In article <45lhnsF707hgU1@individual.net>, Robert Klemme wrote: >Phil Tomson wrote: > >How ugly do you feel about > >define_behavior {|in,out,var| > if in[:rst] > var[:count]=0 > elsif in[:clk] > var[:count]+=1 > end >} I'd rather just go ahead and use the '@'. > >This has the added benefits of > > - giving you the choice what to provide as arguments (could be simply >hashes but anything else that supports the interface) > > - introducing separate namespaces for each type of variable > >But then again, using plain instance variables might be even simpler and >less cluterrish... > >> So that's why I was trying to dynamically create a method that would >> have the variables in the scope of the method and then the proc bound >> to that scope. >> >> I suppose another way to do it would be to do: >> >> class Counter < RHDL >> inputs :clk, :rst >> outputs :count_out >> >> def initialize >> count = 0 >> define_behavior { >> #.... do something with count ... >> } >> end >> end >> >> This is sort of how RHDL is now. There's no need for a 'variables' >> method since you just declare your variables in the 'initialize' >> method. I was trying to get away from the explicit 'initialize' >> definition. But maybe it's not such a bad thing. 'count' would then >> be available to the block passed to define_behavior. > >IMHO explicit is not bad. Often it makes code more readable and >maintainable. True, but.... but since this is a DSL it seems to add a bit of clutter. With the other 'syntax' shown earlier, the 'initialize' method is generated automatically so that it takes the inputs and outputs: ag = AndGate.new(a,b,result) that seems kind of nice... > >> The other alternative is the one that Jim Freeze mentioned: >> >> class Counter < RHDL >> inputs :clk, :rst >> outputs :count_out >> variables :count=>0, :foo=>42 >> >> define_behavior %q{ >> #.... do something with count, foo ... >> } >> end >> >> In that case defne_behavior takes a string instead of a block. That >> solves the problem because the string can be evaluated later in >> different contexts. >> But the '%q' is kind of ugly ;-) > >Yes. :-) ....but it could be the least of the evils. Phil