Re: [myhdl-list] Proposal: SignalType symbol
Brought to you by:
jandecaluwe
From: Jan D. <ja...@ja...> - 2010-06-09 21:00:36
|
Tom Dillon wrote: > Jan, > > I have lost track of the changes to MyHDL in the last year or so but > just happened to get tripped up over this a week or so ago. > > I had to go into some of my old code and change: > > if isinstance(x,Signal) : > > > to: > > if isinstance(x,type(Signal(0))) : > > I am assuming this is the same issue. Yes. Apparently the change was much less innocent than I thought. (I found the problem in old project code of mine also.) > My question is, what advantage does a factory function bring? I hoped it provided an easy way out for me to fix an earlier mistake of too much magic :-) Seriously, here is the story. Originally, I designed a Signal with this interface: Signal(val, delay=0) With the delay parameter you can create a Signal with an "automatic" delay. (I don't know if many people use this but anyway.) The easiest way to implement the "delayed signal" behavior is as a Signal subclass: class DelayedSignal(Signal): ... The idea is that the user shouldn't need to worry that this is technically a different class (albeit a subclass): all he sees is the Signal interface. However, this means that the Signal constructor had to construct either a Signal or a DelayedSignal depending on a parameter of the constructor ... Believe it or not, but you can do this in Python using the __new__ method. However, now I am introducing all kinds of other Signal subclasses (ShadowSignal et all) and this becomes really confusing. This design choice was a mistake in the first place. The right thing to do is to decouple the classes from the functional interface that creates the required objects: hence the factory function. This does the same without magic, expect that its name is no longer available to refer to the type of the objects. Hence, my proposal to create dedicated symbols as needed for myhdl types, named in line with the Python convention. Jan -- Jan Decaluwe - Resources bvba - http://www.jandecaluwe.com Python as a HDL: http://www.myhdl.org VHDL development, the modern way: http://www.sigasi.com Analog design automation: http://www.mephisto-da.com World-class digital design: http://www.easics.com |