[deleted]
Macros in Python
11–20 of 71 posts
Re: Macros in Python
#12Earlier quoted context omitted.
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? :)
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…
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. Turning this:
if ((isinstance(tree, ClassDef) or isinstance(tree, FunctionDef))
and len(tree.decorator_list) == 1
and tree.decorator_list[0]
and type(tree.decorator_list[0]) is Name
and tree.decorator_list[0].id in module.decorator_registry):
...
into: if ((isinstance(tree, ClassDef) or isinstance(tree, FunctionDef))
and [Name(id)]
Doesn't quite work yet, but we're getting thereRe: Macros in Python
#13Earlier quoted context omitted.
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? :)
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…
myfoo = Foo(4, Bar(3, 8))
with patterns:
Foo(x, Bar(3, z))
So in this case, the assertion will only pass if the Foo contains a Bar in the second slot, and that Bar contains a 3 in its first slot, while also binding the 4 and 8 to their own names, x and z respectively.Re: Macros in Python
#14[deleted]
I'm a little sad that the enum PEP got accepted, without them being actual ADTs.
Re: Macros in Python
#15[2] https://github.com/natefaubion/adt.js
Re: Macros in Python
#16Re: Macros in Python
#17Something 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.
Re: Macros in Python
#18Re: Macros in Python
#19[deleted]
Author here. If you look at my github, you'll see I do use plenty of Scala!
Re: Macros in Python
#20I love love love the PEG implementation.