All Questions

Tagged with
Filter by
Sorted by
Tagged with
43 votes
1 answer
13k views

What does the tt metavariable type mean in Rust macros?

I'm reading a book about Rust, and start playing with Rust macros. All metavariable types are explained there and have examples, except the last one – tt. According to the book, it is a “a single ...
Pavel Davydov's user avatar
31 votes
8 answers
36k views

How can I simulate macros in JavaScript?

I know that JavaScript doesn't support macros (Lisp-style ones) but I was wondering if anyone had a solution to maybe simulate macros? I Googled it, and one of the solutions suggested using eval(), ...
Anders Rune Jensen's user avatar
22 votes
1 answer
2k views

Generating Clojure code with macro containing type hints

I'm trying to generate some Clojure code with type hints, however the type hints seem to disappear whenever I build some code (they also don't function when the code is compiled) e.g. `(let [^...
mikera's user avatar
  • 106k
20 votes
3 answers
2k views

Is there any Template Haskell tutorial for someone who doesn't know Lisp?

I wanted to learn Template Haskell but all tutorials I find either assume that you learned lisp and know what lisp macros are, or that you know some cs theory jargon - things as splices, ...
Rafael S. Calsaverini's user avatar
16 votes
1 answer
6k views

Scala macros, where are they used?

I just noticed that Scala has macros, but I have never seen any code that uses them. They also seem quite different from C preprocessor macros and the like. Reading through the overview of macros, it ...
Zavior's user avatar
  • 6,442
15 votes
4 answers
3k views

F# Type Providers vs. Lisp macros

I've been reading about F# 3.0 type providers (e.g. here) and it seems that they are based on a kind of compile-time code generation. In that respect I was wondering how they compare against Lisp ...
Marcus Junius Brutus's user avatar
13 votes
2 answers
1k views

Metaprogramming C/C++ using the preprocessor

So I have this huge tree that is basically a big switch/case with string keys and different function calls on one common object depending on the key and one piece of metadata. Every entry basically ...
bastibe's user avatar
  • 16.8k
12 votes
5 answers
13k views

C/C++ macro to repeat code

is there any way to repeat a C code N times with a macro? Also N is a macro. For example if I have this macros: #define N 5 #define COODE "nop\n\t" #define REPEAT [...] When I call repeat the ...
Alex's user avatar
  • 886
10 votes
3 answers
1k views

How can I avoid repeating myself when creating a C++ enum and a dependent data structure? [duplicate]

Possible Duplicate: Enum to string : return the enum integer value if invalid / not found In brief, the (working) definition code that I have is something like this: enum Gadget { First, ...
GrandOpener's user avatar
  • 2,013
9 votes
2 answers
2k views

Quote-unquote idiom in Julia & concatenating Expr objects

I'd like to write a simple macro that shows the names & values of variables. In Common Lisp it would be (defmacro dprint (&rest vars) `(progn ,@(loop for v in vars collect `(...
vukung's user avatar
  • 1,844
9 votes
2 answers
3k views

Elixir macros and bind_quoted

I have a macro that defines a module like so. defmodule Bar do def bar do IO.puts "I am #{inspect __MODULE__}" end end defmodule MacroFun do defmacro define_module(name) do quote do ...
cjbottaro's user avatar
  • 856
7 votes
6 answers
2k views

How can one make a 'passthru' function in C++ using macros or metaprogramming?

So I have a series of global functions, say: foo_f1(int a, int b, char *c); foo_f2(int a); foo_f3(char *a); I want to make a C++ wrapper around these, something like: MyFoo::f1(int a, int b, char* ...
Ryan's user avatar
  • 616
7 votes
1 answer
2k views

Elixir: use macro in the body of same module that defined it

This is common elixir: defmodule Fizz do defmacro asdf, do: IO.puts("asdf") end defmodule Buzz do require Fizz Fizz.asdf end However, although you can reference macros in the same context ...
ichigolas's user avatar
  • 7,685
6 votes
5 answers
566 views

Lisp / Clojure: Is it a good idea to write function generating macros?

This question asks to create a Clojure macro to generate several functions. We figured out a way to do this but were stuck with the question of "Is this a good idea?". My initial reaction is not ...
Paul's user avatar
  • 7,976
6 votes
6 answers
4k views

C++ macro/metaprogram to determine number of members at compile time

I am working on an application with a message based / asynchronous agent-like architecture. There will be a few dozen distinct message types, each represented by C++ types. class message_a { long ...
MW_dev's user avatar
  • 2,146
6 votes
1 answer
231 views

Non-recursive symbol-macrolet

I'd like to expand all symbols x in a certain fragment into (value x). E.g. (lambda () (* x x)) should become (lambda () (* (value x) (value x))) A simple use of symbol-macrolet wouldn't work, ...
6502's user avatar
  • 113k
6 votes
3 answers
962 views

How can I print a variable name and its value without typing the name twice?

When you are debugging, it's very useful to do this: var = calc() print("var:", var) Is there any language where this is easy to do? In C and C++ you can use the stringify macro operator # and in ...
marcus's user avatar
  • 5,079
6 votes
1 answer
202 views

How to get the literal value of a TemplateHaskell named variable

If I have a Name in TemplateHaskell and want to find out the value of the variable that it names, provided that the variable is declared as a literal, can this be done? var = "foo" -- Can `contentsOf`...
dflemstr's user avatar
  • 26.1k
6 votes
1 answer
780 views

Nested macro iteration with C preprocessor

With the C preprocessor you can have some kind of high-order macros. Something like this: #define ABC(f) f(a) f(b) f(c) #define XY(f) f(x) f(y) #define CODE(x) foo_ ## x ABC(CODE) #undef CODE #...
Roland Leißa's user avatar
5 votes
2 answers
1k views

How to evaluate an expression in nim?

I'm trying to perform the equivalent of the eval method from Python in nim. I was under the impression that parseStmt from the macros package should help me with this, but I'm facing a compilation ...
Benjamin Audren's user avatar
5 votes
2 answers
344 views

Is there a relationship between untyped/typed code quotations in F# and macro hygiene?

I wonder if there is a relationship between untyped/typed code quotations in F# and the hygiene of macro systems. Do they solve the same issues in their respective languages or are they separate ...
soc's user avatar
  • 28.2k
5 votes
6 answers
5k views

Using C++ Macros To Check For Variable Existence

I am creating a logging facility for my library, and have made some nice macros such as: #define DEBUG myDebuggingClass(__FILE__, __FUNCTION__, __LINE__) #define WARING myWarningClass(__FILE__, ...
rcv's user avatar
  • 6,168
5 votes
1 answer
524 views

Is there an equivalent of Julia's metaprogramming and macros for JavaScript and TypeScript?

I'm used to Julia macros (metaprogramming), that are a quite handy way of generating flexible code. https://docs.julialang.org/en/v1/manual/metaprogramming/ Is there an equivalent for JavaScript or ...
Morgane's user avatar
  • 155
5 votes
1 answer
1k views

scope of expressions and variables in Julia macro in modules

For some reason, I had to put quote...end block in the macro and ex is generated programmatically. This code works. macro addsum_out() quote ex = :(x+y) sum(eval(ex)) end end x = [1 1 1] ...
Chang's user avatar
  • 846
5 votes
1 answer
333 views

Term expansion for a list of terms

Say I want to have a number of rules that all follow the same pattern. I have ran into this situation when I want to avoid non-deterministic behavior by explicitly listing all possible first arguments....
user avatar
5 votes
1 answer
619 views

error: "struct" expression not at top level

function check(str,arg;type=DataType,max=nothing,min=nothing,description="") @argcheck typeof(arg)==type @argcheck arg>min @argcheck arg<max @argcheck typeof(...
ahm5's user avatar
  • 653
5 votes
3 answers
2k views

How could my code tell a compile-time constant versus a variable?

Here's my problem. I have a BINARY_FLAG macro: #define BINARY_FLAG( n ) ( static_cast<DWORD>( 1 << ( n ) ) ) Which can be used either like this ("constant" scenario): static const ...
sharptooth's user avatar
  • 169k
5 votes
1 answer
705 views

Why does macro hygiene not prevent collisions between multiple const definitions?

I thought "hygiene" would prevent collisions between Xs defined within my macro m! but that turned out not to be the case. What am I misunderstanding? macro_rules! m { ($e:expr) => { ...
nodakai's user avatar
  • 7,871
5 votes
1 answer
668 views

conditionally implement trait inside macro invocation

I'm looking to create a macro to generate thin wrappers for some basic types, for example, the syntax: wrapper!(String => Email); would expand to something roughly like: struct Email { inner: ...
cameron1024's user avatar
  • 9,663
5 votes
0 answers
290 views

Java's efficient imperative loop through Scala macros?

Scala macros take well-typed terms as arguments instead of arbitrary AST, so it is not obvious to do certain things with them, in particular, introducing a new binding form. Let's say I want to have ...
Phil's user avatar
  • 5,615
4 votes
1 answer
405 views

How to create a macro that suppresses errors and warnings?

I'd like to redefine or overwrite some functions in Base, without the users noticing about this. I found this trick sometime ago: original_stderr = STDERR redirect_stderr() # code redirect_stderr(...
HarmonicaMuse's user avatar
4 votes
2 answers
277 views

A group of variadic macros

I would like to have a group of variable number of arguments passed into a macro. I have following macros which is incorrect: #define M_NARGS(...) M_NARGS_(__VA_ARGS__, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, ...
yuefengz's user avatar
  • 3,358
4 votes
2 answers
557 views

A macro for expanding an arbitrary macro a certain number of times?

I want to write the C macros which takes either an integer literal or something akin to an integer literal, and the name of another macro, and expands that other macro as many times as the value of ...
einpoklum's user avatar
  • 124k
4 votes
5 answers
445 views

Emacs metaprogramming, dynamically define methods

I am trying to define some helper functions to quickly jump to different projects from within emacs. I started by defining a macro as follows (defmacro project-alias (name path) `(defun ,name () ...
eightbitraptor's user avatar
4 votes
2 answers
620 views

Creative/interesting uses for C macros; metaprograming [closed]

I have been looking for interesting and preferably useful C macro/defines examples that wold be a bit more extensive than defining consts or min/max functions. It is my understanding that macros ...
morphles's user avatar
  • 2,473
4 votes
1 answer
189 views

Is there a way to convert a function to expressions in Julia?

I am trying to create a macro which is capable of (probably recursively) turn a function into expressions so as to reach the operations applied within that function. As a visualization: function ...
emirhan422's user avatar
4 votes
1 answer
1k views

Printing the current line in elixir source code

In elixir we have the Pseudo-variables __MODULE__, __DIR__ et al. In erlang there is also the functionality to get the current line, using an erlang macro. How do I get the current file line in ...
Peter Saxton's user avatar
  • 4,556
4 votes
2 answers
592 views

Julia: inject code into function

I would like to inject code into a function. For concreteness, consider a simple simulater: function simulation(A, x) for t in 1:1000 z = randn(3) x = A*x + z end end ...
user45893's user avatar
  • 733
4 votes
2 answers
619 views

Can Julia macros be used to generate code based on specific function implementation?

I am fairly new to Julia and I am learning about metaprogramming. I would like to write a macro that receive in input a function and returns another function based on the implementation details of ...
Lillo's user avatar
  • 41
4 votes
1 answer
95 views

Generate functions at macros expansion time

I would like to generate functions for a class accepting 1 type parameter case class C[T] (t: T) depending on the T type parameter. The functions I would like to generate are derived by the ...
user2468425's user avatar
4 votes
1 answer
341 views

Interpolating an expression into an expression inside of a quote

This question builds off of a previous SO question which was for building expressions from expressions inside of of a macro. However, things got a little trucker when quoting the whole expression. For ...
Chris Rackauckas's user avatar
3 votes
3 answers
3k views

Common Lisp: Passing Symbol to Macro

The purpose of this macro is to create a macro that gives a name to accessing a certain key of an associated list. (defmacro generate-accessor (key-symbol prefix) (let ((mac-name (...
SquareCrow's user avatar
3 votes
4 answers
623 views

C++ Meta-programming

During development of my project in C++ I have a frequent need of debugging and I usually use this macro to do it #define DBUG(a) {std::cout << #a << " : " << a << std::endl;};...
user2833292's user avatar
3 votes
5 answers
509 views

Need meta-programming magic to define a mother lode of bit fields in an error-free way

The goal is to control which types of users are allowed to perform which operations at the UI level. This code has been in place for a while; I just want to improve it a bit. The file which I am ...
Hamish Grubijan's user avatar
3 votes
1 answer
112 views

Julia create multiple slightly modified versions of a function

I have a function that looks like function eom!(du, u, p) @views a, b = u[:,1], u[:,2]; @views da, db = du[:,1], du[:,2]; y = # some stuff involving p and a; da .= f(a, b, y); db ...
Grayscale's user avatar
  • 1,502
3 votes
4 answers
361 views

Adding default handle_info in __using__ macro

I'm trying to make a little wrapper around ExIrc, but I have encountered a bit of a problem. __using__ macro appends ast to the beginning of the module and I want to append a function definition to ...
Haito's user avatar
  • 2,039
3 votes
2 answers
256 views

Macros iterating over undefined Symbols

When applying a macro multiple times with a another macro, bare Symbols are not inserted into the current context: (defmacro ty [type] `(deftype ~type [])) (defmacro empties [& args] (doseq [...
Daniel Ribeiro's user avatar
3 votes
1 answer
200 views

How to “splat” the function arguments for dynamically created function

I am generating some functions in the module: defmodule M do funs = [do_it: [:arg1, :arg2]] Enum.each(funs, fn {name, args} -> args = Enum.map(args, & {&1, [], Elixir}) def ...
Aleksei Matiushkin's user avatar
3 votes
2 answers
359 views

Why is Elixir Logger composed of Macros?

Had to use Logger in one of my applications today, and remembered that I needed to call require Logger first. So I decided to look at the Logger source code to see why debug, info, error, etc. are ...
Sheharyar's user avatar
  • 74.6k
3 votes
2 answers
368 views

How can I invoke a macro with all points in a catersian product of argument sets?

Suppose I have a macro FOO(a, b) and I want to invoke this macro with a certain (fixed) set of possible values for a; and a certain fixed set of values for b. Thus, if my set of b values is bar and ...
einpoklum's user avatar
  • 124k