Add terse syntax for defining macros?

186 views
Skip to first unread message

Ismael Venegas Castelló

unread,
May 3, 2016, 5:14:21 PM5/3/16
to julia-dev

Now that macros are more like generic functions than ever, I kept thinking if there was a reason why we didn’t have terse syntax for defining macros, ie:

julia> isint(x::Int) = true
isint (generic function with 1 method)

julia> isint(2)
true

julia> @isint(x::Int) = true
@isint (macro with 1 method)

julia> @isint 2
true

Instead of:

macro isint(x::Int) true end

Maybe short macro definitions are not that common, but I keep seeing them in string macros for example and other simple macros:

macro raw_str(s::AbstractString); s; end
@raw_str(s::AbstractString) = s

macro thingy_str(s::ASCIIString); do_my_ascii_thing!(s); end
@thingy_str(s::ASCIIString) = do_my_ascii_thing!(s)

I know this is being picky, but I didn’t find a previous discussion about this open for discussion. So what do you think guys?

Yichao Yu

unread,
May 3, 2016, 5:21:29 PM5/3/16
to Julia Dev
It's ambiguous with macro call.

Ismael Venegas Castelló

unread,
May 3, 2016, 5:56:04 PM5/3/16
to julia-dev
That's one reason I thought of, but then how is simple function definition `foo(x) = 1` not conflicting with function call `foo(x)`? I'm trying to understand how both are parsed.

Yichao Yu

unread,
May 3, 2016, 7:27:27 PM5/3/16
to Julia Dev
On Tue, May 3, 2016 at 5:56 PM, Ismael Venegas Castelló
<ismael...@gmail.com> wrote:
> That's one reason I thought of, but then how is simple function definition
> `foo(x) = 1` not conflicting with function call `foo(x)`? I'm trying to
> understand how both are parsed.

```
julia> macro m()
esc(:x)
end
@m (macro with 1 method)

julia> @m() = 1
1
```

foo(x) = 1 is exactly parsed as an assignment to a function call.
However, this is not ambiguous since it can't mean anything else.
However, a macro call is allowed to return something that can be
assigned to.

Stefan Karpinski

unread,
May 3, 2016, 7:27:30 PM5/3/16
to juli...@googlegroups.com
A function call can't be the left-hand side of an assignment (except for the meaning of defining a method). A macro call can expand to anything, including expressions that are valid left-hand-sides of assignments. Of course, I don't think such usage is particularly common, but it is currently valid, e.g.:

julia> foo
ERROR: UndefVarError: foo not defined
 in eval(::Module, ::Any) at ./boot.jl:229

julia> macro x(y) y end
@x (macro with 1 method)

julia> @x(foo) = 123
123

julia> foo
123

Ismael Venegas Castelló

unread,
May 3, 2016, 9:35:10 PM5/3/16
to juli...@googlegroups.com
Oh but of course, thank you very much for your comments!

Ismael Venegas Castelló

Data Analyst

Cel. 044 55 6434 0229

iven...@richit.com.mx

Cerro San Francisco 357, C.P. 04200

Campestre Churubusco, Coyoacán

Ciudad de México

  

Tel. 6718 1818

richit.com.mx
Reply all
Reply to author
Forward
0 new messages