Coder Social home page Coder Social logo

Comments (9)

charlesstaats avatar charlesstaats commented on July 20, 2024 1

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.

charlesstaats avatar charlesstaats commented on July 20, 2024

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 to oldpicture.
  • 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.

user202729 avatar user202729 commented on July 20, 2024

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.

charlesstaats avatar charlesstaats commented on July 20, 2024

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.

user202729 avatar user202729 commented on July 20, 2024

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.

user202729 avatar user202729 commented on July 20, 2024

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.

user202729 avatar user202729 commented on July 20, 2024

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.

charlesstaats avatar charlesstaats commented on July 20, 2024

I'd go with your first option. If you need to modify X and Ywithin 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 for X and Y 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.

user202729 avatar user202729 commented on July 20, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.