caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* fixed length arrays as types
@ 2000-11-05  3:06 Chris Hecker
  2000-11-07  8:41 ` Judicael Courant
                   ` (3 more replies)
  0 siblings, 4 replies; 9+ messages in thread
From: Chris Hecker @ 2000-11-05  3:06 UTC (permalink / raw)
  To: caml-list


Is there any way to do this:

type vector3 = [| float; float; float |];

Basically, I want an array of a given length to be a given type, so I can use the type system to check add_vector3 rather than throwing if the arrays don't match.  I know I can make records {x:float, y:float} but I'd like it to be parameterizable at compile time.

Something like this C++:

template <int unsigned N> class vector { float a[N]; };
vector<3> add( vector<3> v1, vector<3> v2 );

vector<3> v3 = add(vector<3>(),vector<3>());    // works
vector<3> v4 = add(vector<5>(),vector<3>());    // type error (note v<5>

I guess the higher level question is whether scalar constants can be part of the type signature like they can be in C++.  Or, the related but different question is whether there's a way to differentiate between "float a[]" (or "float *a"), the variable length array type, and "float a[3]", a fixed length array type, which C++ doesn't do, but it lets you wrap the ideas in template classes which do allow you to represent this to the type system.

Chris



^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2000-11-11 16:19 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-11-05  3:06 fixed length arrays as types Chris Hecker
2000-11-07  8:41 ` Judicael Courant
2000-11-07 13:28 ` Sven LUTHER
2000-11-07 17:00 ` Brian Rogoff
2000-11-08  9:53 ` Xavier Leroy
2000-11-08 17:20   ` Chris Hecker
2000-11-09  4:03     ` William Lee Irwin III
2000-11-09 21:53       ` Chris Hecker
2000-11-11 15:15   ` John Max Skaller

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).