~mna/snow

snow/pkg/semantic/testdata/interface_satisfied.snow -rw-r--r-- 1.3 KiB
424066c5Martin Angers doc: v0.0.5 1 year, 8 months ago
                                                                                
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
interface Empty {
}

interface Foo {
  fn foo(i: int) -> int
}

struct S1 {
  fn foo(x: int) -> int {}
  fn bar(s: string) {}
}

struct S2 {
  fn bar(s: string) {}
  fn quz(b: bool) {}
}

struct S3 {
  fn foo(u: uint) -> int {}
}

struct S4 {
  ref fn foo(i: int) -> int {}
}

fn get_s4() -> S4 {
  return S4()
}

fn main() {
  var empty: Empty
  var f: Foo
  var integer: int
  var s1: S1

  empty = empty   # self assignment should work
  empty = f       # ok, everything satisfies empty
  empty = s1      # yep
  empty = integer # well no, not quite everything!

  f = S1() # all good, S1.foo exists and same types
  f = S2() # nope
  f = S3() # types don't match

  var vs4 = S4()
  let ls4 = S4()

  # this works, vs4 is a var
  f = vs4
  # not this, ls4 is immutable
  f = ls4
  # not this either, rhs is a value
  f = S4()

  var tup: (int, Foo) 
  # this is fine
  tup = (1, vs4)
  # this is not, should fail because ls4 is immutable
  tup = (1, ls4)

  var tup2: (int, Foo, (bool, Foo))
  tup2 = (1, vs4, (true, vs4)) # works, var s4 in both Foo places
  tup2 = (1, ls4, (true, get_s4())) # fails, immutable S4 and value S4

  # those all work (var S4 for Foo, and whatever for Empty)
  var tup3: (int, (Foo, (Empty)))
  tup3 = (3, (vs4, (ls4)))
  tup3 = (3, (vs4, (get_s4())))
}