-
Notifications
You must be signed in to change notification settings - Fork 0
/
constructors.txt
99 lines (77 loc) · 2.53 KB
/
constructors.txt
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
89
90
91
92
93
94
95
96
97
98
99
Constructors are problematic:
* All parent constructors must have been called before a child construtor is called. (Some native objects have binary blobs set up by the constructor. Not calling it will cause crashes.)
* It would be nice to be able to alter the parameter list sent to the parent constructor in the subtype constructor
* If a child does not specify how to call the parent constructor, it should be done behind the scenes. How is the argument list determined in this case?
* If a subtype does not specify any constructor, the parent's constructor should be available. How do we insert all the cool custom assignment stuff then?
We probably need to do some weird magic at the type level in order to
work this stuff out - the classType macro doesn't have access to the
ast of the parent types, meaning it can't really do all the stuff
required.
How things might ideally work:
class Base
{
def __init__(Int aaa)
{
print("Base %\n" % [aaa]);
}
}
/*
This subtype specifies that the aaa value should always be 0
*/
class Derived1 (extends(Base))
{
def __init__(String bbb)
(super(aaa:0))
{
print("Derived %\n" % [bbb]);
}
}
/*
This subtype implicitly passes on the value of aaa to the parent constructor
*/
class Derived2 (extends(Base))
{
def __init__(Int aaa, String bbb)
{
print("Derived %\n" % [bbb]);
}
}
/*
This subtype is an error - the type of aaa is incompatible with the type of
aaa in the base class
*/
class Derived3 (extends(Base))
{
def __init__(Float aaa, String bbb)
{
print("Derived %\n" % [bbb]);
}
}
/*
This subtype is an error - no value is provided for aaa
*/
class Derived4 (extends(Base))
{
def __init__(String bbb)
{
print("Derived %\n" % [bbb]);
}
}
/*
This subtype has no explicit constructor. Implicitly, a constructor with
the same signature as the parent is created.
*/
class Derived5 (extends(Base))
{
}
Implementation plan:
One probably workable method is to leave the constructors as they are and create a special function that does all the parameter mangling and other constructing, including calling the right constructors in the right order.
Type constants and constructors:
Types should have constant members, which can only be assigned to early on in the construction process, before the constructor is run. Syntax proposal:
class MyClass
{
const Int myFirstConst;
def __init__(Int someValue) (myConst = arbitraryLibrary.someFunction(someValue+1))
{
}
}