by smlckz · 5y ago · 0 comments
class foo {
// int x;
int y;
int bar();
};
// in another file
int x;
foo::bar() // "with whole friggin class scope ..."
{
return x + y; // global ::x plus class member y
}
Now uncomment the x in the class declaration, and the x + y now adds two class members.So when you add, remove or rename members of a record, you are potentially influencing the contents of scopes in other files all over the project. You can cause variable shadowing that way.
VAR
B : INTEGER;
WITH ^P DO
A := B; (* B is just a local, A is a field *)
END
If B is added as a field, the above suddenly means ^P.A = ^P.B.Any time you edit a Modula-2 record, you have to search the program for all the places where its type may be targeted by WITH.
Probably if you stick to some rules, like never using WITH on another module's record type, you will be fine.
Diagnostics related to shadowing could help.
Oh, you can do it.
his is the TXR Lisp interactive listener of TXR 249.
Quit with :quit or Ctrl-D on an empty line. Ctrl-X ? for cheatsheet.
Use full-strength TXR for tough tasks, or dilute with shell scripts.
1> (defmacro with (struct obj . body)
(with-gensyms (obj-sym)
^(let ((,obj-sym ,obj))
(symacrolet ,(mapcar (op list @1 ^(qref ,obj-sym ,@1)) (slots struct))
,*body))))
with
2> (with time (time-struct-utc (time))
(list year month day))
(2021 1 30)
Expansion: 3> (macroexpand '(with time (time-struct-utc (time))
(list year month day)))
(let ((#:g0016 (time-struct-utc (time))))
(symacrolet ((time-local
#:g0016.time-local)
(time-utc #:g0016.time-utc)
(time-string
#:g0016.time-string)
(time-parse #:g0016.time-parse)
(year #:g0016.year)
(month #:g0016.month)
(day #:g0016.day)
(hour #:g0016.hour)
(min #:g0016.min)
(sec #:g0016.sec)
(dst #:g0016.dst)
(gmtoff #:g0016.gmtoff)
(zone #:g0016.zone))
(list year month
day)))
Total expansion; symacrolet gone, a.b desugared into (slot a 'b): 4> (expand '(with time (time-struct-utc (time))
(list year month day)))
(let ((#:g0017 (time-struct-utc (time))))
(list (slot #:g0017 'year)
(slot #:g0017 'month)
(slot #:g0017 'day)))