Closed horasal closed 8 years ago
Base: class {
value: Int
init: func (=value)
print: func { "Value: #{this value}" printfln() }
}
A: class extends Base { init: func (.value) { super(value) } }
B: class extends Base { init: func (.value) { super(value) } }
C: class extends Base { init: func (.value) { super(value) } }
// this works fine
(A new(10), B new(20), C new(30)) print()
// error Unsupported expression type VariableAccess for VariableDeclTuple.
(av, bv, cv) := (A new(1), B new(2), C new(3)) value
// Note: It works if av, bv and cv are pre-defined:
av, bv, cv: Int
(av, bv, cv) = (A new(1), B new(2), C new(3)) value
I'm not sure if this is strictly required by the bounty, but it would be nice to be able to do a one-liner like in the first case. @fredrikbryntesson and @marcusnaslund what do you say?
@thomasfanell member variable access in variabledecl is supposed to be supported in this implementation. I will fix it when I get back to workplace.
@zhaihj :+1:
Fixed.
If the replacing happens after expr resolve
, compiler will finall loop back to resolve variabledecl. So just move the replacement before expr resolve.
@zhaihj Nice job! Contact @fredrikbryntesson as per bounty instructions.
I'm wondering should I contact @fredrikbryntesson each time when a pr is merged, or just wait from the second time?
No it is not necessary, I got your contact information now so I will contact you when needed.
Description
This pull request add the ability of caling functions/accessing variable/accessing property on Tuples. For example, the following expression:
will be unwrapped to:
Implementation Details
The unwrap process only happes in two module:
VariableAccess
andFunctionCall
.VariableAccess
VariableAccess handles the member variable/property access. If the
expr
is a Tuple, it is necessary to do the unwrap. In current rock, tuple-tuple assignment is already able to be unwrapped. Thus, the only work is to turnTuple method
to(TupleElements method(), ...)
.FunctionCall
FunctionCall handlse the member function call access. Similar with
VariableAccess
, we just "move" the method call into the tuple. One thing needed to be noticed is that becauseTuple
is an expression, so to make the following codes work, modification is also done in the return type check.Current rock do not write standalone tuple to the c sources but just put a
null
. Such as the example given inFunctionCall
, if the unwrapped tuple is just replaced with aNULL
, the memory ofa, b,c
leaks. Thus, avisitTuple
method is added into theVisitor
to allow backend write each element of tuples.