Using `with()` on a struct that contains alias this only works within a single module.
realhet
real_het at hotmail.com
Sat Apr 4 18:02:34 UTC 2026
Hi,
I developed a DB transaction class. It's a nested class, have
some methods.
Later I decided to use it from with(){} blocks, so I wrapped it
inside a struct and exposed its fields using `alias this = ...;`.
It worked fine up until I moved the testing into another module.
From that point, the with() was unable see the effect of `alias
this=...`.
I've fixed it by delegating all the methods from the Transaction
class to the Wrapper struct. It is not nice, and redundant.
Is there a better way to do it?
I really need that scope wrapper struct, so the transaction
object will be closed exactly when the CPU exits from the with()
block.
```d
static struct ScopedTransaction
{
private Transaction transaction_;
@property Transaction transaction() => transaction_;
/+
//alias this = transaction_;
This FAILS when using with() in another module.
+/
@property active() => transaction.active;
void enforceActive() { transaction.enforceActive; }
void commit() { transaction.commit; }
void rollback() { transaction.rollback; }
scope execute(Args...)(Args args) =>
transaction.execute!(Args)(args);
scope 查(Args...)(LOCATION_t loc, Args args) =>
transaction.查!(Args)(loc, args);
@disable this(this);
this(Transaction transaction)
{ transaction_ = transaction; }
~this()
{ freeTransaction(transaction_); }
}
scope transaction(bool modify = false)
=> ScopedTransaction(startTransaction(modify: modify));
```
More information about the Digitalmars-d-learn
mailing list