|
Re: What if Constant Values were also valid Types?
|
Posted: Jan 19, 2006 4:20 AM
|
|
To Christopher: What can you do with instances of these "types", and how are the instances different from the "types"?
Types usually denote the possible operations you can do on its instances. I have a little difficulty mapping that to this. > I think this is a good idea. It is used in functional > languages like Haskel and Scala to great effect. The > classic example is factorial. > > Functional languages go even further and allow conditions > to be put on the arguments, the case of a constant is just > the condition that it matches the argument exactly. IE: > > int factorial( int x ; x < 1 ) { throw new
> IllegalArgumentException( "Factorial is undefined for
> negative numbers" ); }
> int factorial( 1 x ) { return 1; }
> int factorial( 2 x ) { return 2; } // optimization
> int factorial( int x ) { return x * factorial( x - 1 ); }
>
> (I made up the above syntax!)
Hm, but can you call this "types"? Sure, functional programming languages have such pattern matching, but it's my impression that they match on values, not types. Consider an analogue example using C++ (ref my mentioning of templates being similar to FP pattern matching):
template<unsigned int N> struct factorial { static const int value=N * factorial<N-1>::value; };
template<> struct factorial<0> { static const int value=1; };
std::cout << factorial<4>::value; // Prints "24"
What we match on, here, is most definiely values, not types. We have a template with an argument of type int, which is specialised for various values of it.
As you say, the FP way may be considered "overloading on value", but that still doesn't make the values types...
|
|