summary refs log tree commit diff stats
path: root/tests/array/troof1.nim
blob: 96669a121076a13ead544ffc320ed8ee2eb80f8b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
discard """
  output: '''@[2, 3, 4]321
9.0 4.0
(a: 1.0, b: 2.0, c: 8.0)2.0'''
"""

proc foo[T](x, y: T): T = x

var a = @[1, 2, 3, 4]
var b: array[3, array[2, float]] = [[1.0,2], [3.0,4], [8.0,9]]
echo a[1.. ^1], a[^2], a[^3], a[^4]
echo b[^1][^1], " ", (b[^2]).foo(b[^1])[^1]

type
  MyArray = object
    a, b, c: float

var
  ma = MyArray(a: 1.0, b: 2.0, c: 3.0)

proc len(x: MyArray): int = 3

proc `[]=`(x: var MyArray; idx: range[0..2]; val: float) =
  case idx
  of 0: x.a = val
  of 1: x.b = val
  of 2: x.c = val

proc `[]`(x: var MyArray; idx: range[0..2]): float =
  case idx
  of 0: result = x.a
  of 1: result = x.b
  of 2: result = x.c

ma[^1] = 8.0
echo ma, ma[^2]