Add terse syntax for defining macros?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Add terse syntax for defining macros?

Ismael Venegas Castelló

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?

Reply | Threaded
Open this post in threaded view
|

Re: Add terse syntax for defining macros?

Yichao Yu
On Tue, May 3, 2016 at 5:14 PM, Ismael Venegas Castelló
<[hidden email]> wrote:

> 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?

It's ambiguous with macro call.
Reply | Threaded
Open this post in threaded view
|

Re: Add terse syntax for defining macros?

Ismael Venegas Castelló
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.

El martes, 3 de mayo de 2016, 16:21:29 (UTC-5), Yichao Yu escribió:
On Tue, May 3, 2016 at 5:14 PM, Ismael Venegas Castelló
<<a href="javascript:" target="_blank" gdf-obfuscated-mailto="Ovw9zr1LAQAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">ismael...@...> wrote:

> 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?

It's ambiguous with macro call.
Reply | Threaded
Open this post in threaded view
|

Re: Add terse syntax for defining macros?

Yichao Yu
On Tue, May 3, 2016 at 5:56 PM, Ismael Venegas Castelló
<[hidden email]> 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.

>
> El martes, 3 de mayo de 2016, 16:21:29 (UTC-5), Yichao Yu escribió:
>>
>> On Tue, May 3, 2016 at 5:14 PM, Ismael Venegas Castelló
>> <[hidden email]> wrote:
>> > 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?
>>
>> It's ambiguous with macro call.
Reply | Threaded
Open this post in threaded view
|

Re: Add terse syntax for defining macros?

Stefan Karpinski
In reply to this post by Ismael Venegas Castelló
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

On Tue, May 3, 2016 at 5:56 PM, Ismael Venegas Castelló <[hidden email]> 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.

El martes, 3 de mayo de 2016, 16:21:29 (UTC-5), Yichao Yu escribió:
On Tue, May 3, 2016 at 5:14 PM, Ismael Venegas Castelló
<[hidden email]> wrote:

> 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?

It's ambiguous with macro call.

Reply | Threaded
Open this post in threaded view
|

Re: Add terse syntax for defining macros?

Ismael Venegas Castelló
Oh but of course, thank you very much for your comments!

Ismael Venegas Castelló

Data Analyst

Cel. 044 55 6434 0229

[hidden email]

Cerro San Francisco 357, C.P. 04200

Campestre Churubusco, Coyoacán

Ciudad de México

  [hidden email]

Tel. 6718 1818

richit.com.mx

2016-05-03 18:26 GMT-05:00 Stefan Karpinski <[hidden email]>:
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

On Tue, May 3, 2016 at 5:56 PM, Ismael Venegas Castelló <[hidden email]> 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.

El martes, 3 de mayo de 2016, 16:21:29 (UTC-5), Yichao Yu escribió:
On Tue, May 3, 2016 at 5:14 PM, Ismael Venegas Castelló
<[hidden email]> wrote:

> 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?

It's ambiguous with macro call.