I agree with that, David. That's actually closely related to why this is hard to do in a dynamic language. The meaning of the code can only be understood with non-local knowledge, rather than simply from the syntax (which is strictly local).
On Thu, May 21, 2015 at 9:29 AM, David Anthoff <[email protected]> wrote: > I was never a fan of `with`. It makes it really hard to read/reason about > unfamiliar code because you need to know exactly what members a type has in > order to understand what is going on. > > > > *From:* [email protected] [mailto:[email protected]] > *On Behalf Of *Stefan Karpinski > *Sent:* Thursday, May 21, 2015 9:22 AM > *To:* Julia Users > *Subject:* Re: [julia-users] Has anyone done a Pascal-style "with" > statement (i.e., macro)? > > > > This seems hard to make sane in a dynamic language. The issue is that > `foo` doesn't give you enough information at compile time to decide which > names are fields of `foo` and which are just local variables. If the first > argument to the @with macro had to be a name of a type – i.e. Foo rather > than foo – then you could do it. You could loosen that up to allow any > expression that can be evaluated at compile time in the global scope where > the @with occurs, but that seems pretty confusing. For example, you > couldn't write `@with typeof(foo)` which is pretty much the first thing > that people will try to do. > > > > On Thu, May 21, 2015 at 9:13 AM, Magnus Lie Hetland <[email protected]> > wrote: > > A couple of decades ago, I remember using the with statement in Pascal. I > have since wished for it in several languages, but I guess in Julia, it > would probably be feasible without altering the language? > > > > Basically, it'd be something like > > > > type Foo > a > end > > foo = Foo(42) > > @with foo do > a = 1 > b = "frozzbozz" > # ... > end > > # foo.a is now 1 > > That is, let the fields of a composite type be available (syntactically) > as local variables. > > Now, hacking together something like this wouldn't be hard – but making it > really work (without weirdness or performance degradation) seems harder. > And maybe it's a bad idea to begin with. Or maybe someone has already done > it?-) > > > > > > >
