Type of a vector


#1

Let’s consider the following array definition:

let v = [[0, 3], [10, 13]];

This is simple and compact, as long as the type can be inferred.

For an array of type f32 I need to write either:

let v = [[0f32, 3f32], [10f32, 13f32]];

or:

let v: [[f32; 2]; 2] = [[0.0, 3.0], [10.0, 13.0]];

which seems a lot of additional typing just to specify the type.

Is there a better way?

Thanks


#2

You can do the following:

let v  = [[0f32, 3.], [10., 13.]];

All digits have the same type (because arrays work like that) so only one need be specified. Rfc#260 merging would allow integers to be inferred as floats, then this will probably be allowed:

let v  = [[0f32, 3], [10, 13]];

Until then, you either need to annotate floats with a decimal (and usually zero for looks) or a f32/f64 suffix.


#3

Thanks! I had tried your second suggestion and it did not work, but I had not tired the first one (my bad). I hope rfcs#260 is merged.


#4

I should clarify: rfc#260 is an issue. An actual fix would require a PR written up and merged and then implemented. This would closed the issue.

Thus it’s not as simple as closing the issue. It would definitely be a nice feature to have though.


#5

Ok, I agree it would be a nice feature to have, so I hope the issue is fixed.