-
Notifications
You must be signed in to change notification settings - Fork 93
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature suggestion: shorthand to create a picture #449
Comments
I agree that having to repeat the
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 |
Context manager is okay. And I already figure out the 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 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;
} |
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;
} |
Actually that's not a bad idea. Using Maybe then the suggestion is then "is it worth it to include it by default". |
John, would you support this? Personally, I think both functions ( Random note from conversations with John Bowman: going forward, camelCase names should be preferred to runoncase. |
Do we want to refactor and gradually deprecate old names then...? (How do we support "both versions" of things like |
Actually thinking about it, there's a little problem. What if you want to use variables defined inside the 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 Problem: we cannot use 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 |
I'd go with your first option. If you need to modify
Speaking as someone who rarely uses |
Mypy has 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 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). |
Currently, if we want to create a picture then add it, we write
I think this is rather cumbersome, which requires a temporary variable.
Does it make sense if we can write the following?
In other words, the expression
picture(new void(){…})
evaluates to apicture
object, which consist of all the things drawn inside the anonymous function.Ideally we could make a "special syntax" like
picture{…}
evaluates to a picture, but that requires language modification and makingpicture
into a keyword, which may be undesirable.The text was updated successfully, but these errors were encountered: