summaryrefslogtreecommitdiff
path: root/notes/interface_types.txt
blob: fbca305148bcadade674e0af814dc7ca759a1a14 (plain)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88

there are two types of interface types:
- "open interfaces", like in Java and C#
-- (classes is a superset of interfaces, so most OO languages have them)
- "closed interfaces", e.g. a union with a "kind" flag

performance wise:
- data memory usage should be about the same (implementation pointer vs. implementation enum value)
- enum value should result in faster usage (no indirect lookup via vtable, no pointer call)
- instanceof operation is also faster 

here is a sample of a closed interface (or class tree, perhaps)
    type <T>Set = classtree of Iterable<T> (
        HashSet
        LinkedHashSet
        Tree
    )

<T>Set is now a struct that begins with an enum, and has an unknown size
(so any Set variable must be a ref/rwref/wref)


mixed-closed-open inteface:
    type <T>Set = classtree of Iterable<T> (
        CustomSet
        HashSet
        LinkedHashSet
        TreeSet
    )


syntax:

    openinterface <T>Iterable
    {
        func iterator(ref this) return shared(this) <T>Iterator
                                                     # ^ tricky!
        # an internal struct type is created so the interface can
        # also be passed around (as a reference)
    }

    openinterface <T>Iterator
    {
        func has_next(ref this) return bool
        func next(rwref this) return dedup ref T
                                     # ^ tricky!
    }

    

    classtree <T>Set extends <T>Iterable
    {
        CustomSet
        HashSet
        LinkedHashSet
        TreeSet

        size elemsize = sizeof T;
                        # ^ tricky!

        func add(rwref this, dedup ref takeown T elem) return bool
                             # ^ tricky!
        func contains(ref this, dedup ref T elem) return bool
        func remove(rwref this, dedup ref dropown T elem) return bool
        func count(ref this) return size
        func clear(wref this) when is_dedup T
                                   # ^ tricky!
        
        #interface func iterator(ref this) return takeshare(this) <T>SetIterator
        #                                         # ^ tricky!

        # An internal function for the interface should also be generated,
        # which fills a struct with pointers to the implementation functions
        func _LRL_get_iface_Iterable(ref this, wref _LRL_iface_Iterable iface)
    }

    classtree <T>SetIterator extends <T>Iterator
    {
        CustomSetIterator
        HashSetIterator
        LinkedHashSetIterator
        TreeSetIterator

        size elemsize = sizeof T
        ref <T>Set
        
        #interface func has_next(ref this) return bool
        #interface func next(rwref this) return dedup ref T
    }