Code Monkey home page Code Monkey logo

Comments (7)

gomoripeti avatar gomoripeti commented on August 29, 2024 1

@Zalastax no-no, I just meant these macros for internal use

from gradualizer.

zuiderkwast avatar zuiderkwast commented on August 29, 2024 1

I like it very much!

@josefs wrote

We'd have to be very careful about normalizing the types in order for pattern matching to work correctly with the annotations

I think it shouldn't matter. The macros should use _ for matching the annotation.

Analogous to the type/1,2 functions (for expressions), I suggest these ?type/1,2 macros for patterns:

-define(type(T), {type, _, T, []}).
-define(type(T, Args), {type, _, T, Args}).

I don't think we need macros for expressions, since we have the functions for that. Possibly we could name the macros type_pat to make it clear that it's a pattern.

from gradualizer.

josefs avatar josefs commented on August 29, 2024

I like the idea of simplifying pattern matching using macros. We'd have to be very careful about normalizing the types in order for pattern matching to work correctly with the annotations, but that's a good thing.

I interested to hear what other people think about this.

from gradualizer.

UlfNorell avatar UlfNorell commented on August 29, 2024

A possibility is to have separate macros for patterns and expressions. Something like

-define(t_any, {type, erl_anno:new(0), any, []}).
-define(t_any_p, {type, _, any, []}).

Another, perhaps nicer, option is to have a variant which takes the annotation as an argument that you can use in patterns:

-define(t_any(Ann), {type, Ann, any, []}).
-define(t_any(), ?t_any(erl_anno:new(0))).

from gradualizer.

gomoripeti avatar gomoripeti commented on August 29, 2024

The good thing about record syntax is that #type{name = any, params = []} matches any value in the anno field (so we don't have to care whether the type was normalised and position removed or not). When constructing the record the not mentioned field (anno) gets the default value, again win-win :)
I think it is an advantage that we don't have to have separate macros for matching and construction (so I wouldn't spoil it). Ann as extra macro arg otoh can be ambiguous (eg ?t_list(Ann) vs ?t_list([?t_integer])) - but if we only allow extra Ann arg with the variant with the most args then its fine (eg ?t_list, ?t_list([?_integer]), ?t_list([?t_integer], ?t_nil), ?t_list(Ann, [?t_integer], ?t_nil)

In the rare case when we want to copy the annotation from one type to the other, we could use the tuple or record representation directly (bit inconsistent look) or have a pair of getter/setter functions (or the extra Ann arg to the macro, that Ulf proposed).

from gradualizer.

Zalastax avatar Zalastax commented on August 29, 2024

Does this relate to #85?

from gradualizer.

zuiderkwast avatar zuiderkwast commented on August 29, 2024

Now, the macros for patterns are added in master and we have the type/1,2 functions for expressions. Perhaps we can close this?

At first I didn't get Péter's macro trick, but it's pretty clever: using a record to allow the same macro to be used in patterns and in expressions. The annotation is ignored in patterns and gets the annotation erl_anno:new(0) in expressions. It is pretty clever, but it wouldn't pass the Gradualizer, unless we fix #121.

from gradualizer.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.