]> ocean-lang.org Git - ocean-D/blob - Ocean-methods
updates
[ocean-D] / Ocean-methods
1 Methods in Ocean
2
3 methods are any function that is found from a given value.
4 It can be associated with the type or the value
5
6 An interface is a set of methods.  A concrete type can export an
7 interface and a formal type can require an interface.
8
9 A value can be combined with a concrete interface to override or augment
10 what it's type natively has.  Value methods are not so easily
11 overridden.
12
13 The first arg of a method must match the owner, and other args can be of
14 that type
15
16 Interface names are per module.
17 Method names are per interface.  When a variable supports multiple
18 interfaces and names don't conflict, interface name can be left out.
19
20 maybe value method use . and type methods use : ??
21     that would make "case functionname :" tricky to parse.
22
23
24 Thought:
25  Rather than anon structs, allow struct AND references to be marked as 'follow'
26  so anything behind the object appears in the parent.
27  Then foo.method can possible follow a chain of pointers from foo to find the method.
28
29  foo might be an inode and have a pointer to a superblock, which has a
30  pointer to a methods structure contains 'getattr'.  Then 'foo.getattr'
31  transparently becomes 'foo.super.super_methods.getattr.
32  
33  Question.  In what circumstances is 'foo' passed as first argument?
34  Probably it happens by default, but some syntactic escape allows something else
35  to be passed.  Maybe foo::something() ??
36  Or foo..something() ??
37  Maybe if type of first arg is compatible, the value before '.' is interpolated
38  unless ".." is used.
39
40  So the name after "." can:
41   - follow a pointer
42   - be field in struct/record
43   - follow a transparent field
44   - be constant in type - which includes functions.
45
46  Transparent fields add extra distance and a closer name will match first.
47  So if foo.bar and foo.baz.bar are both valid and baz is transparent, then
48  "foo.bar' gets the former - no conflict is reported.  Obviously foo.bax.bar
49  gets the latter
50
51 Methods can be added to values to provide required interface for passing to
52 a function.  This probably only works for references.
53 We can specify the methods using a type which includes them, or a value
54 which already has them, or individually
55 Syntax might be tricky.  For 'sort' I want to pass an array, and a method
56 for comparing two elements of the array.  I want the function to see an array
57 where the element are already comparable.
58 So the function declares that it needs a type
59
60  func qsort(type:comparable, target:[size::]type)
61
62 function will need size of members of array, size of array, and compare function.
63  func qsort (target:[size::]type:comparable)
64
65  ra:[32]int
66  qsort(ra, :ra[].cmp(a, b) = a >= b)
67
68 Same syntax could add a destructor
69
70  foo: bar .free = myfunction