Comments (9)
John, would you support this?
Personally, I think both functions (onPicture
and onNewPicture
, possibly with different names) could be useful and worth including in plain_picture.asy
and documenting.
Random note from conversations with John Bowman: going forward, camelCase names should be preferred to runoncase.
from asymptote.
I agree that having to repeat the p
every time is annoying and error-prone. Fortunately, there's already a way to get around that:
- Save
currentpicture
to a temporary variable (say,oldpicture
). - Set
currentpicture = new picture;
- Run all the
size
,draw
, etc. commands as usual, without specifying the picture. - Add
currentpicture
tooldpicture
. - Set
currentpicture = oldpicture;
.
See also https://sourceforge.net/p/asymptote/discussion/409349/thread/282f4337de/
The syntax is still a little cumbersome, but if we wanted to slim it down even more, I think something like Python's context manager syntax would make more sense than a picture
-specific syntax.
from asymptote.
Context manager is okay. And I already figure out the currentpicture
thing.
But what I really want is to make it into an expression instead of a function.
The context manager would still be useful though. E.g.
picture p;
with setcurrentpicture(p) {
draw((0, 0) -- (1, 1));
}
Another case where contextmanager is useful is to locally apply transformation, like TikZ.
with applytransform(rotate(45)) {
draw((0, 0) -- (1, 1));
}
// equivalent to draw(rotate(45)*((0, 0) -- (1, 1)));
But note that contextmanager can already be implemented with closure, it's just a bit heavy on syntax (need another pair of parentheses and new void()
). (This is not Lisp, but closure is not too bad.)
onpicture(p, new void(){
draw((0, 0) -- (1, 1));
})
// implemented by
void onpicture(picture p, void f()){
var backup=currentpicture;
currentpicture=p;
f();
currentpicture=backup;
}
from asymptote.
So, why can't you just alter this to do what you wanted? Perhaps I'm misunderstanding?
add(currentpicture, onNewPicture(new void(){
draw((0, 0) -- (1, 1));
}));
// implemented by
picture onNewPicture(void f()){
var backup=currentpicture;
currentpicture=new picture;
f();
picture result = currentpicture;
currentpicture=backup;
return result;
}
from asymptote.
Actually that's not a bad idea. Using quote{…}
instead of new void()
would be slightly shorter as well.
Maybe then the suggestion is then "is it worth it to include it by default".
from asymptote.
Do we want to refactor and gradually deprecate old names then...?
(How do we support "both versions" of things like currentpicture
even? Is operator=
overloadable?)
from asymptote.
Actually thinking about it, there's a little problem. What if you want to use variables defined inside the onNewPicture
construct?
Current code:
picture p;
save(); // I don't remember if this works but theoretically
currentpicture=p;
object X=draw("abc", box);
object Y=draw("def", ellipse);
restore();
// use X and Y here
New code:
object X, Y;
picture p=newPicture(quote{
X=draw("abc", box);
Y=draw("def", ellipse);
});
(Side note: I think if onNewPicture
returns a new picture, it makes more sense to name it newPicture
.)
Problem: we cannot use var
for X
and Y
anymore.
Proposal 2:
var [p, X, Y]=newPicture(quote{
X=draw("abc", box);
Y=draw("def", ellipse);
return [X, Y];
});
Problem: destructured assignment (or even automatic tuple
of arbitrary type) isn't available in Asymptote yet.
from asymptote.
I'd go with your first option. If you need to modify X
and Y
within the void function and have them available afterwards, then make them global variables. (Or if you want to be really clever, put the void function inside an object of which X
and Y
are fields. In most cases, though, I doubt this is worth it.)
Problem: we cannot use
var
forX
andY
anymore.
Speaking as someone who rarely uses var
at all, I don't see this as a big issue. But not all workflows are the same. How bit
a pain point is it for you if you can't use var
? Is there some other alternative (e.g., a tool for figuring out the type of an object while you are writing the code) that would mitigate the pain here?
from asymptote.
Mypy has reveal_type()
special construct that just tells what the type is.
I think Asymptote already have LSP (although I haven't gotten around to figure out how to use it), which isn't too bad.
Also we have asy command-line and write
which can occasionally write out the type (for things like typedef drawer ⟨complicated function type⟩
though…)is
And then there's also go-to-definition (Asymptote haven't gotten universal ctags integration yet, but then universal ctags is written in C which is far from the most powerful language)
Actually they need not be global variables, they just need to be variables in the enclosing scope ("nonlocal" variables in Python's terminology).
from asymptote.
Related Issues (20)
- asy-latex one line processing
- Linking error on macOS with clang: `Undefined symbols for architecture x86_64: "_add_history"` etc. HOT 5
- Can’t compile LSP – <boost/filesystem/directory.hpp> missing from LspCpp/src/lsp/utils.cpp HOT 1
- Algolia DocSearch integration?
- Defining struct in a loop gives confusing error message HOT 1
- Feature request: Convert tab to 4 [/8] spaces in error message reporting HOT 3
- Feature request: `var oldAsin=asin;` copies the function HOT 2
- Bug: unitsize() does not work to set unitsize of other pictures HOT 3
- Feature suggestion: automatic generation of constructor HOT 1
- Documentation of cputime is in Structure section? HOT 1
- Feature suggestion: Remove `add(picture dest, picture src, pair position)` variant as it's dangerous HOT 4
- Stack traceback on error?
- Suggestion: integrate with Typst? HOT 3
- Suggestion: `envelope` type don't need `above` passed in HOT 1
- Why does the bounding box of a frame containing just a label not just the label's bounding box? HOT 1
- Suggestion: transformations for shading tools? HOT 1
- Feature suggestion: baseline specification for inclusion in LaTeX HOT 1
- Grammar railroad diagram HOT 2
- scale command breaks graph output HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from asymptote.