I have a vague memory of someone tweeting me a way to avoid having to put a type annotation on every field in an array like so:
var gradZ = [
0'f32, 0'f32, 0'f32, 0'f32,
1'f32, 1'f32,-1'f32,-1'f32,
1'f32, 1'f32,-1'f32,-1'f32]
But I can't remember, is there a way?
You can specify just the first term and the others are inferred.
var gradZ = [
0'f32, 0, 0, 0,
1, 1,-1,-1,
1, 1,-1,-1]
Alternatively, there was discussion for an 'asArray' macro.
https://github.com/nim-lang/Nim/issues/6563 https://github.com/nim-lang/Nim/pull/6640
It's actually 'mapLiterals' from sequtils
import sequtils
var x = mapLiterals([ 0, 0, 1, 1, -1, -1], float32)
but this method appears to have a bug with negative values.
i'd really like to be able to define arrays like this:
let myArray: array[auto, float32] = [1, 2, 3, 4]
@Arrrrrrrrr : if you are on devel, you can use mapLiterals from sequtils
import sequtils
let x = mapLiterals([0.1, 1.2, 2.3, 3.4], int)
doAssert x is array[4, int]
I generally use the fact that you can omit the parentheses (or dot) for conversions of literals, leading to a pretty natural looking notation:
let a = [float 1, 2, 3, 4]
let b = [int8 1, 2, 3, 4]