Skip to content

Commit a243858

Browse files
author
Michael Phelps
committed
Improve inference of composite types
1 parent 1a9e20b commit a243858

File tree

12 files changed

+244
-101
lines changed

12 files changed

+244
-101
lines changed

README.md

Lines changed: 112 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -727,10 +727,10 @@ package main
727727
import "fmt"
728728

729729
func main() {
730-
a := map[interface{}]struct{}{1: {}, 2: {}, 3: {}, 4: {}}
731-
b := map[interface{}]struct{}{4: {}, 5: {}, 6: {}}
730+
a := map[int]struct{}{1: {}, 2: {}, 3: {}, 4: {}}
731+
b := map[int]struct{}{4: {}, 5: {}, 6: {}}
732732
b[7] = struct{}{}
733-
fmt.Println(func(s1 map[interface{}]struct{}, s2 map[interface{}]struct{}) map[interface{}]struct{} {
733+
fmt.Println(func(s1 map[int]struct{}, s2 map[int]struct{}) map[interface{}]struct{} {
734734
union := map[interface{}]struct{}{}
735735
for elt := range s1 {
736736
union[elt] = struct{}{}
@@ -740,7 +740,7 @@ func main() {
740740
}
741741
return union
742742
}(a, b))
743-
fmt.Println(func(s1 map[interface{}]struct{}, s2 map[interface{}]struct{}) map[interface{}]struct{} {
743+
fmt.Println(func(s1 map[int]struct{}, s2 map[int]struct{}) map[interface{}]struct{} {
744744
intersection := map[interface{}]struct{}{}
745745
for elt := range s1 {
746746
if func() bool {
@@ -752,7 +752,7 @@ func main() {
752752
}
753753
return intersection
754754
}(a, b))
755-
fmt.Println(func(s1 map[interface{}]struct{}, s2 map[interface{}]struct{}) map[interface{}]struct{} {
755+
fmt.Println(func(s1 map[int]struct{}, s2 map[int]struct{}) map[interface{}]struct{} {
756756
difference := map[interface{}]struct{}{}
757757
for elt := range s1 {
758758
if !func() bool {
@@ -764,7 +764,7 @@ func main() {
764764
}
765765
return difference
766766
}(a, b))
767-
fmt.Println(func(s1 map[interface{}]struct{}, s2 map[interface{}]struct{}) map[interface{}]struct{} {
767+
fmt.Println(func(s1 map[int]struct{}, s2 map[int]struct{}) map[interface{}]struct{} {
768768
symmetric_difference := map[interface{}]struct{}{}
769769
for elt := range s1 {
770770
if !func() bool {
@@ -784,7 +784,7 @@ func main() {
784784
}
785785
return symmetric_difference
786786
}(a, b))
787-
fmt.Println(func(s1 map[interface{}]struct{}, s2 map[interface{}]struct{}) bool {
787+
fmt.Println(func(s1 map[int]struct{}, s2 map[int]struct{}) bool {
788788
for elt := range s1 {
789789
if !func() bool {
790790
_, ok := s2[elt]
@@ -795,7 +795,7 @@ func main() {
795795
}
796796
return true
797797
}(a, b))
798-
fmt.Println(func(s1 map[interface{}]struct{}, s2 map[interface{}]struct{}) bool {
798+
fmt.Println(func(s1 map[int]struct{}, s2 map[int]struct{}) bool {
799799
for elt := range s2 {
800800
if !func() bool {
801801
_, ok := s1[elt]
@@ -2036,7 +2036,7 @@ package main
20362036
import "fmt"
20372037

20382038
func main() {
2039-
s := map[interface{}]struct{}{1: {}, 2: {}, 3: {}}
2039+
s := map[int]struct{}{1: {}, 2: {}, 3: {}}
20402040
x := 1
20412041
fmt.Println(len(s))
20422042
fmt.Println(func() bool {
@@ -2116,7 +2116,7 @@ func main() {
21162116
b := "hello world"
21172117
fmt.Println(strings.Contains(b, "hello"))
21182118
fmt.Println(!strings.Contains(b, "Hello"))
2119-
c := map[interface{}]interface{}{"hello": 1, "world": 2}
2119+
c := map[string]int{"hello": 1, "world": 2}
21202120
fmt.Println(func() bool {
21212121
_, ok := c["hello"]
21222122
return ok
@@ -2393,11 +2393,9 @@ package main
23932393
import "fmt"
23942394

23952395
var (
2396-
SITE = "https://www.google.com/"
2397-
NAME = []string{"Michael", "Wayne", "Phelps"}
2398-
KEYS = map[interface{}]interface{}{1: 2, 3: 4}
2399-
)
2400-
var (
2396+
SITE = "https://www.google.com/"
2397+
NAME = []string{"Michael", "Wayne", "Phelps"}
2398+
KEYS = map[int]int{1: 2, 3: 4}
24012399
AGE = 1000
24022400
BIRTH_YEAR = 2050
24032401
)
@@ -2851,13 +2849,13 @@ func main() {
28512849
if q := map[interface{}]struct{}{}; len(q) != 0 {
28522850
fmt.Println(q)
28532851
}
2854-
if r := map[interface{}]struct{}{1: {}, 2: {}, 3: {}}; len(r) != 0 {
2852+
if r := map[int]struct{}{1: {}, 2: {}, 3: {}}; len(r) != 0 {
28552853
fmt.Println(r)
28562854
}
28572855
if s := map[interface{}]interface{}{}; len(s) != 0 {
28582856
fmt.Println(s)
28592857
}
2860-
if t := map[interface{}]interface{}{1: 2}; len(t) != 0 {
2858+
if t := map[int]int{1: 2}; len(t) != 0 {
28612859
fmt.Println(t)
28622860
}
28632861
}
@@ -3501,6 +3499,68 @@ func main() {
35013499
}
35023500
}
35033501
```
3502+
### timemodule
3503+
#### Python
3504+
```python
3505+
import time
3506+
3507+
def main():
3508+
print("Hello")
3509+
time.sleep(3)
3510+
print("... time!")
3511+
3512+
3513+
if __name__ == '__main__':
3514+
main()
3515+
```
3516+
#### Go
3517+
```go
3518+
package main
3519+
3520+
import (
3521+
"fmt"
3522+
"time"
3523+
)
3524+
3525+
func main() {
3526+
fmt.Println("Hello")
3527+
time.Sleep(3 * time.Second)
3528+
fmt.Println("... time!")
3529+
}
3530+
```
3531+
### exit
3532+
#### Python
3533+
```python
3534+
import sys
3535+
3536+
3537+
def main():
3538+
quit()
3539+
quit(1)
3540+
exit()
3541+
exit(1)
3542+
sys.exit()
3543+
sys.exit(1)
3544+
3545+
3546+
if __name__ == '__main__':
3547+
main()
3548+
```
3549+
#### Go
3550+
```go
3551+
package main
3552+
3553+
import "os"
3554+
3555+
func main() {
3556+
os.Exit(0)
3557+
os.Exit(1)
3558+
os.Exit(0)
3559+
os.Exit(1)
3560+
os.Exit(0)
3561+
os.Exit(1)
3562+
}
3563+
```
35043564
### algomajorityelement
35053565
#### Python
35063566
```python
@@ -3581,48 +3641,33 @@ func main() {
35813641
}())
35823642
}
35833643
```
3584-
### timemodule
3644+
### retroactive_composite_types
35853645
#### Python
35863646
```python
3587-
import time
3588-
35893647
def main():
3590-
print("Hello")
3591-
time.sleep(3)
3592-
print("... time!")
3648+
a = []
3649+
a.append(3)
35933650

3651+
b = []
3652+
b += a
35943653

3595-
if __name__ == '__main__':
3596-
main()
3597-
```
3598-
#### Go
3599-
```go
3600-
package main
3654+
c = {}
3655+
c["hello"] = 1
36013656

3602-
import (
3603-
"fmt"
3604-
"time"
3605-
)
3657+
d = {}
3658+
d[1] = 2
3659+
d["gonna_be_an_interface"] = "yup"
36063660

3607-
func main() {
3608-
fmt.Println("Hello")
3609-
time.Sleep(3 * time.Second)
3610-
fmt.Println("... time!")
3611-
}
3612-
```
3613-
### exit
3614-
#### Python
3615-
```python
3616-
import sys
3661+
e = set()
3662+
e.add(1)
36173663

3664+
f = [[]]
3665+
f[0].append(1)
36183666

3619-
def main():
3620-
quit()
3621-
quit(1)
3622-
exit()
3623-
exit(1)
3624-
sys.exit()
3625-
sys.exit(1)
3667+
g = {}
3668+
g[(1, 2)] = 3
3669+
3670+
print(a, b, c, d, e, f, g)
36263671

36273672

36283673
if __name__ == '__main__':
@@ -3632,15 +3677,25 @@ if __name__ == '__main__':
36323677
```go
36333678
package main
36343679

3635-
import "os"
3680+
import "fmt"
36363681

36373682
func main() {
3638-
os.Exit(0)
3639-
os.Exit(1)
3640-
os.Exit(0)
3641-
os.Exit(1)
3642-
os.Exit(0)
3643-
os.Exit(1)
3683+
a := []int{}
3684+
a = append(a, 3)
3685+
b := []int{}
3686+
b = append(b, a...)
3687+
c := map[string]int{}
3688+
c["hello"] = 1
3689+
d := map[interface{}]interface{}{}
3690+
d[1] = 2
3691+
d["gonna_be_an_interface"] = "yup"
3692+
e := map[int]struct{}{}
3693+
e[1] = struct{}{}
3694+
f := [][]int{{}}
3695+
f[0] = append(f[0], 1)
3696+
g := map[[2]int]int{}
3697+
g[[2]int{1, 2}] = 3
3698+
fmt.Println(a, b, c, d, e, f, g)
36443699
}
36453700
```
36463701

examples/contains.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func main() {
3535
b := "hello world"
3636
fmt.Println(strings.Contains(b, "hello"))
3737
fmt.Println(!strings.Contains(b, "Hello"))
38-
c := map[interface{}]interface{}{"hello": 1, "world": 2}
38+
c := map[string]int{"hello": 1, "world": 2}
3939
fmt.Println(func() bool {
4040
_, ok := c["hello"]
4141
return ok

examples/globals.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@ package main
33
import "fmt"
44

55
var (
6-
SITE = "https://www.google.com/"
7-
NAME = []string{"Michael", "Wayne", "Phelps"}
8-
KEYS = map[interface{}]interface{}{1: 2, 3: 4}
9-
)
10-
var (
6+
SITE = "https://www.google.com/"
7+
NAME = []string{"Michael", "Wayne", "Phelps"}
8+
KEYS = map[int]int{1: 2, 3: 4}
119
AGE = 1000
1210
BIRTH_YEAR = 2050
1311
)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
a := []int{}
7+
a = append(a, 3)
8+
b := []int{}
9+
b = append(b, a...)
10+
c := map[string]int{}
11+
c["hello"] = 1
12+
d := map[interface{}]interface{}{}
13+
d[1] = 2
14+
d["gonna_be_an_interface"] = "yup"
15+
e := map[int]struct{}{}
16+
e[1] = struct{}{}
17+
f := [][]int{{}}
18+
f[0] = append(f[0], 1)
19+
g := map[[2]int]int{}
20+
g[[2]int{1, 2}] = 3
21+
fmt.Println(a, b, c, d, e, f, g)
22+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
def main():
2+
a = []
3+
a.append(3)
4+
5+
b = []
6+
b += a
7+
8+
c = {}
9+
c["hello"] = 1
10+
11+
d = {}
12+
d[1] = 2
13+
d["gonna_be_an_interface"] = "yup"
14+
15+
e = set()
16+
e.add(1)
17+
18+
f = [[]]
19+
f[0].append(1)
20+
21+
g = {}
22+
g[(1, 2)] = 3
23+
24+
print(a, b, c, d, e, f, g)
25+
26+
27+
if __name__ == '__main__':
28+
main()

0 commit comments

Comments
 (0)