[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: What's so cool about Scheme?



[Mike Newhall <mike@newhall.net>]
> >> I think of OO as being the ability to provide user-defined types
> >> with
> >invariants
> >
> >I'm not sure what "invariants" are (but I do know that use-defined
> >types are). Is it possible to have "user-defined types with
> >invariants" in a language that would not be considered
> >object-oriented (Modula, Pascal, C, ML)?
> 
> 	An invariant is merely a guarantee: if I create a "prime number"
> 	object, my construction / destruction / etc. code will ensure that
> 	it will invariably be a prime, even though it is represented as a
> 	general int, because only my code will be allowed to touch the
> 	representation.  Perhaps this is insufficient but that's the most
> 	minimal definition of OO I can think of.  By that definition you
> 	can't really provide invariants, at least not easily, generally,
> 	and elegantly, in the non-OO languages you mentioned.  I could in


I'm not sure that's true. Certainly it's tough in C, but with devious
use of include files, externs and typedefs you could come close (for
practical purposes. Obviously someone can do an end-run around
anything you try to enforce in C...). But I think you can do exactly
what you described in SML:

    abstype prime = P of int
    with
        val firstPrime = P 2
        fun nextPrime p = ...
        fun toInt (P n) = n
        ...
    end

SML won't let me treat a prime as an int, assign to one or inspect it.
I can only operate on primes using the "methods" defined in the
abstype. What's wrong with this? Note that I definitely wouldn't call
SML OO, but I argue that it does offer invariants...

Matt

-- 
Matt Hellige                  matt@immute.net
http://matt.immute.net