Something about classes that are nested inside other classes suddenly being available as top level classes seems wrong. I'm not super deep on the innards of Python, but that doesn't look right.
Macros in Python
21–30 of 71 posts
Re: Macros in Python
#22Earlier quoted context omitted.
Author here. If you look at my github, you'll see I do use plenty of Scala!
Well yes; the code is clearly the work of someone who uses and likes scala. My point was: the library seems designed to let you write "scala in python"; wouldn't it be better to use scala when you want scala syntax, and write more idiomatic python when using python?
Re: Macros in Python
#23Earlier quoted context omitted.
I'm sure there's a use-case for this, but I'm not coming up with one off the top of my head. This comes across as an extremely rare type of problem to encounter. What's the purpose of only binding x and z if the first argument of Bar matches? What if it doesn't match? Is an exception thrown? Are x,z just None? [Note: I'm talking about the example, not the @case decorator, which does seem useful] Edit: After reading t…
Author here. The ` The purposes of pattern matching is to replace code that looks like this: if (isinstance(tree, BinOp) and type(tree.left) is Name and type(tree.op) is Mod and tree.left.id in module.expr_registry): ... with code that looks like this if BinOp(Name(id), Mod(), body) Which looks much nicer, and more clearly says what you want: that `tree` "looks like" a particular shape. EDIT: Here's another example.…
Re: Macros in Python
#24Can anyone give a Tl;DR about how this works? Don't x and y need to exist beforehand?
It's hard to give a TL;DR on the whole library, but case classes can be used with for example pattern matching ( https://github.com/lihaoyi/macropy#pattern-matching ). The following code: with patterns: Foo(x, Bar(3, z)) The constructor after the Does this help? :)
Is that a macro created by this library or a feature of the library?
Edit: I did some reading and it looks to be a feature macro of the library.
Re: Macros in Python
#25Something about classes that are nested inside other classes suddenly being available as top level classes seems wrong. I'm not super deep on the innards of Python, but that doesn't look right.
Other author here: Like lihaoyi said, it's true that the way we defined things doesn't match standard Python namespacing, but it saves some typing and I think it's easy to get used to the semantics of our case classes. Our macro just transforms the "namespace tree" that you would normally get from writing code like that into an "inheritance tree", where all the subclasses' names are at the level of the top class. If…
Re: Macros in Python
#26Re: Macros in Python
#27Earlier quoted context omitted.
Other author here: Like lihaoyi said, it's true that the way we defined things doesn't match standard Python namespacing, but it saves some typing and I think it's easy to get used to the semantics of our case classes. Our macro just transforms the "namespace tree" that you would normally get from writing code like that into an "inheritance tree", where all the subclasses' names are at the level of the top class. If…
I urge you to take this criticism more seriously. With your library a programmer is changing the semantics of class nesting in the language, all by pre-pending a very missable and ambiguous @case to the class. I'm no expert but I think this is pretty bad in any language, and especially one that says "explicit is better than implicit"
We think that with some discipline, it is possible to come up with macro transformations which are both useful and understandable by a programmer. This means setting clearly defined semantics for the transformations, which is difficult but doable.
If you think macros are kinda crazy (well, they are!) you should look at the implementation of such pythonic constructs like `namedtuple`, the `ast` module and the new Enum library coming out!
Re: Macros in Python
#28Re: Macros in Python
#29Earlier quoted context omitted.
I urge you to take this criticism more seriously. With your library a programmer is changing the semantics of class nesting in the language, all by pre-pending a very missable and ambiguous @case to the class. I'm no expert but I think this is pretty bad in any language, and especially one that says "explicit is better than implicit"
We do take it seriously; it's a hard tradeoff between Novelty and Utility. Given that the whole point of macros is to change the semantics of the Python language, this (valid) criticism applies not just to case classes but macros in general. If you go haywire with macros, all hell breaks loose. We think that with some discipline, it is possible to come up with macro transformations which are both useful and understan…
Re: Macros in Python
#30Earlier quoted context omitted.
Other author here: Like lihaoyi said, it's true that the way we defined things doesn't match standard Python namespacing, but it saves some typing and I think it's easy to get used to the semantics of our case classes. Our macro just transforms the "namespace tree" that you would normally get from writing code like that into an "inheritance tree", where all the subclasses' names are at the level of the top class. If…
I urge you to take this criticism more seriously. With your library a programmer is changing the semantics of class nesting in the language, all by pre-pending a very missable and ambiguous @case to the class. I'm no expert but I think this is pretty bad in any language, and especially one that says "explicit is better than implicit"
One thing case classes let you do is implement functionality externally to a class using pattern-matching functions. When you do this, your case class definition will consist of nothing but empty class definitions:
@case
class List:
class Nil(): pass
class Cons(x, xs): pass
In any case, Python's enums will use fully qualified names, so we may change our current system for compatibility and consistency.