Subclassing Float?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Subclassing Float?

Ch Lamprecht
Hello,

I tried to build a subclass of Float without success.
More precisely, I did not find out how to build instances from it.
Is subclassing Float something I should not do?
I am trying to build objects, that represent Audio frequencies.
They should behave like Floats but have some additional methods like
'addCent: anOffset'  or 'deltaCent: aFrequency' (cent refers to a
logarithmic scale used to express frequency relations).

Cheers, Chris

_______________________________________________
Pharo-users mailing list
[hidden email]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-users
Reply | Threaded
Open this post in threaded view
|

Re: Subclassing Float?

Marcus Denker-4

On Mar 6, 2010, at 1:52 AM, Ch Lamprecht wrote:

> Hello,
>
> I tried to build a subclass of Float without success.
> More precisely, I did not find out how to build instances from it.
> Is subclassing Float something I should not do?
> I am trying to build objects, that represent Audio frequencies.
> They should behave like Floats but have some additional methods like
> 'addCent: anOffset'  or 'deltaCent: aFrequency' (cent refers to a
> logarithmic scale used to express frequency relations).
>

You could just add these methods as extension to float. This way,
you can write much cleaner code as you can call your methods on
the literal floats that the compiler generates:


        4.5 addCent: ...

In addition, you should have a look at double dispatch and especially Dan's
parametarized double dispatch used for the operators.
This allows you to actually use the math symbols... with your own classes if it makes sense:

e.g.

        4.5 + 5 cents

Plus is implemented like this:

+ aNumber
        "Primitive. Answer the sum of the receiver and aNumber. Essential.
        Fail if the argument is not a Float. See Object documentation
        whatIsAPrimitive."

        <primitive: 41>
        ^ aNumber adaptToFloat: self andSend: #+


when the primitve fails (aNumber is not a float), than is sends a message to your
object so that you can implement the calculation in the class (whatever that is), by implementing
this method:

         adaptToFloat: aFloat andSend: aSelector
       

No idea if that makes sense in your case... there have been Music software developed in Squeak
years ago that extenden numbers quite extensively to provide what one would these days call
a "Music Domain Specific Language" ;-)

        Marcus


--
Marcus Denker  -- http://www.marcusdenker.de
INRIA Lille -- Nord Europe. Team RMoD.


_______________________________________________
Pharo-users mailing list
[hidden email]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-users