Is it possible to access Erlang macros from Elixir?

1,464 views
Skip to first unread message

Matt Gushee

unread,
Aug 6, 2014, 10:50:48 PM8/6/14
to elixir-l...@googlegroups.com
Hello, all--

I am interested in doing some GUI programming with Elixir, and toward
that end I've been working through Doug Edmunds' wxerlang tutorial
(http://wxerlang.dougedmunds.com/), translating the code to Elixir as
needed.

So far everything seems to be going well--I can create windows and add
widgets--except for one thing: at several points, the tutorial code
calls for using what I would call constants--but apparently they are
technically macros--with names like:

?wxID_EXIT

and

?wxVERTICAL

As far as I can tell there is no way to use these macros in Elixir
code. The leading question mark certainly is invalid syntax; and I've
tried various clever transformations of the names, but nothing seems
to work.

Of course, I can look up the integer values represented by these
macros in the Erlang source code; that makes my code work as expected.
But of course it would be preferable to use the symbolic names. Is
there any way to do that?

--
Matt Gushee

Josh Adams

unread,
Aug 6, 2014, 10:59:49 PM8/6/14
to elixir-l...@googlegroups.com

Extracted from my elixir sips episode on wx, and maybe not how I'd ultimately prefer to do it, but this works:

-------

So there's a problem with this code, and that's that there's no such thing as
the `wx_const` module we used in the sizer.  The issue here is that wxwidgets
provides some constants in an `.hrl` file that define magic numbers for things
like "make this box sizer horizontally oriented", but there's no trivial way
presently to get at the constants from an .hrl file from Elixir.

What we're going to do is define a trivial erlang module that will just return
that value.  To add an erlang module to your Elixir project, just make a `src`
directory and put an `.erl` file in there:

```sh
mkdir src
vim src/wx_const.erl
```

Alright, so this module is trivial:

```erlang
-module(wx_const).
-compile(export_all).

-include_lib("wx/include/wx.hrl").

wx_horizontal() ->
    ?wxHORIZONTAL.

wx_vertical() ->
    ?wxVERTICAL.

wx_expand() ->
    ?wxEXPAND.

wx_all() ->
    ?wxALL.
```

Alright, with that in place, let's run it again:

```sh
iex -S mix
```

OK, cool, so now we've got multiple buttons in the frame.  We want to build a
grid of numbers.  We'll do this with three horizontal boxsizers embedded in a
vertical boxsizer.  Let's see what that looks like.  We're just going to do this
in a very naive way, more to learn about wx than to end up with code we're
thrilled with here:

```elixir
defmodule WxCalc.Window do
  @title 'WxCalc'

  def start do
    wx = :wx.new
    panel = :wxFrame.new(wx, -1, @title)

    first_row  = :wxBoxSizer.new(:wx_const.wx_horizontal)

--
You received this message because you are subscribed to the Google Groups "elixir-lang-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-ta...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Matt Gushee

unread,
Aug 6, 2014, 11:08:41 PM8/6/14
to elixir-l...@googlegroups.com
Hi, Josh--

Great! Well, as you said, not quite great--but usable. Thank you!

BTW, on my excursion through Erlang-land, I noticed there were some
macros that looked analogous to some of the magic values in Elixir,
e.g.:

?MODULE ~= __MODULE__ # ?

So maybe, if the Elixir team is inclined to make those Erlang macros
directly accessible in Elixir, it might make sense to use that syntax,
so:

__wxVERTICAL__

or maybe

__WX_VERTICAL__

?

[OMG, what am I saying?! I *hate* underscores! Oh well. LOL ;-) ]

--
Matt Gushee

José Valim

unread,
Aug 7, 2014, 2:51:08 AM8/7/14
to elixir-l...@googlegroups.com
__MODULE__ is by coincidence similar to the erlang macro but it is no related to macro at all. There is no way we can support erlang macros in Elixir code (both for technical and semantic reasons).
José Valim
Skype: jv.ptec
Founder and Lead Developer

Matt Gushee

unread,
Aug 7, 2014, 4:00:59 AM8/7/14
to elixir-l...@googlegroups.com
On Thu, Aug 7, 2014 at 12:51 AM, José Valim
<jose....@plataformatec.com.br> wrote:
> __MODULE__ is by coincidence similar to the erlang macro but it is no
> related to macro at all. There is no way we can support erlang macros in
> Elixir code (both for technical and semantic reasons).

OK, well, that's too bad, but not the end of the world. And it's good
to have a definite answer - that way I can implement the necessary
kludges and not worry about it ;-) Thanks.

--
Matt Gushee

Alex Shneyderman

unread,
Aug 7, 2014, 7:25:26 AM8/7/14
to elixir-l...@googlegroups.com
this might be useful -

Robert Virding

unread,
Aug 7, 2014, 9:17:13 AM8/7/14
to elixir-l...@googlegroups.com
I made a tool like this for LFE which translates erlang .hrl files into LFE macros, functions and record definitions, so you should be able to do the same for elixir. It won't be able to handle everything, for example erlang macros which don't generate complete syntactic forms, but most things. You will need an erlang to elixir translator though.

Another question is whether you want to. There are some erlang libraries we wanted to be able to access, for example eunit, which have a large set of macro definitions.

Robert
Reply all
Reply to author
Forward
0 new messages