More Elegant Settable Methods?
jwatson-CO-edu
real.name at colorado.edu
Thu Jan 26 19:41:59 UTC 2023
On Sunday, 22 January 2023 at 02:28:11 UTC, ryuukk_ wrote:
> ```D
> TestStruct ts = {
> a: 2, b: 3,
> op: (s) {
> return s.a + s.b;
> }
> };
> ```
>
>
> This simple! just like with C's designated initializers
Ah, I had forgotten about this handy initializer form! However, I
may not have been clear. I was hoping that by assigning a
function as a member to a `TestStruct`, that it would already
have access to the other members of `TestStruct`. I can see now
that this would not pass muster during type checking and is
therefore a luxury afforded to dynamically typed languages only,
like Python:
```python
from types import MethodType
class TestStruct:
def __init__( self, A, B ):
self.a = A
self.b = B
def op( self ):
return self.a + self.b
ts = TestStruct(2, 3)
ts.op = MethodType(op, ts)
print( ts.op() )
```
More information about the Digitalmars-d-learn
mailing list