This post has been de-listed

It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.

11
Can I make a truly generic type?
Post Body

For example, can I implement a Vector(n) to represent a mathematical n-dim vector?

var Vector(dim: u32) = struct {
    .data: [dim]f64
 }

fn wedgeProduct(vec1: Vector(m), vec2: Vector(k)) Vector(m*k) {
    //... Implementation that references m and k
}

In this Vector type, .dim is runtime-known and constant (i.e. Vectors don't change size)

I know I can return struct definitions from functions, but that doesn't allow me to write a function with a signature like wedgeProduct, or I haven't been able to figure out how.

Should I give up on the array notation, and have my structs hold a dim field and a pointer to an appropriately-sized array? How would I enforce that the array size cannot change?

Author
Account Strength
100%
Account Age
9 years
Verified Email
Yes
Verified Flair
No
Total Karma
13,881
Link Karma
1,039
Comment Karma
12,809
Profile updated: 2 days ago
Posts updated: 5 months ago

Subreddit

Post Details

We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
3 years ago