Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

This pull request features a full overhaul of the pattern matching expression #279

Merged
merged 1 commit into from
Nov 13, 2015
Merged

This pull request features a full overhaul of the pattern matching expression #279

merged 1 commit into from
Nov 13, 2015

Conversation

EliasC
Copy link
Contributor

@EliasC EliasC commented Nov 12, 2015

It:

  • Adds guards.
  • Adds support for matching on objects via Scala-like extractor methods.
  • Adds tuples and matching on tuples. However, at the moment the only way to get a value out of a tuple is via pattern matching.
  • Adds documentation to the match expression.

Tuples are necessary in order to do anything interesting with extractor methods which is why they are included in the same pull request.

It also fixes some related issues:

Sample code implementing a polymorphic association list using pattern matching in the lookup method:

def strCmp(s1 :string, s2 :string) :int
  embed int
    strcmp(#{s1}, #{s2});
  end

passive class Link<t>
  value :t
  next :Link<t>

  def init(value :t, next :Link<t>) :void {
    this.value = value;
    this.next = next;
    }

  -- Methods with the type void -> Maybe T are extractor methods. They define
  -- new patterns that may be used to match against objects of this class
  def link() : Maybe(t, Link<t>)
    Just (this.value, this.next)

passive class AssocList<kt, vt>
  first :Link<(kt, vt)>
  cmp :(kt, kt) -> int

  def init(cmp :(kt, kt) -> int) :void {
    this.first = null;
    this.cmp = cmp
    }

  def put(key :kt, value :vt) :void {
    this.first = new Link<(kt, vt)>((key, value), this.first)
    }

  def lookup(key :kt) : Maybe vt {
    this.lookupHelper(key, this.first, this.cmp)
    }

  def lookupHelper(key :kt, current :Link<(kt, vt)>, cmp :(kt, kt) -> int) :Maybe vt{
    match current with
      null => Nothing : Maybe vt
      link((k, v), next) when cmp(k, key) == 0 => Just v
      link(x, next) => this.lookupHelper(key, next, cmp)
      }

class Main
  def main() : void
    let l = new AssocList<string, int>(strCmp)
    in{
      l.put("foo", 42);
      l.put("bar", 4711);
      match l.lookup("bar") with
        Just res => print res
        Nothing => print "Error"
    }

…pression.

It:
* Adds guards.
* Adds support for matching on objects via Scala-like extractor methods.
* Adds tuples and matching on tuples. However, at the moment the only way to get a value out of a tuple is via pattern matching.
* Adds documentation to the match expression.

Tuples are necessary in order to do anything interesting with extractor methods which is why they are included in the same pull request.

It also fixes some related issues:
* #265
* #253

Sample code implementing a polymorphic association list using pattern matching in the lookup method:
```encore
def strCmp(s1 :string, s2 :string) :int
  embed int
    strcmp(#{s1}, #{s2});
  end

passive class Link<t>
  value :t
  next :Link<t>

  def init(value :t, next :Link<t>) :void {
    this.value = value;
    this.next = next;
    }

  -- Methods with the type void -> Maybe T are extractor methods. They define
  -- new patterns that may be used to match against objects of this class
  def link() : Maybe(t, Link<t>)
    Just (this.value, this.next)

passive class AssocList<kt, vt>
  first :Link<(kt, vt)>
  cmp :(kt, kt) -> int

  def init(cmp :(kt, kt) -> int) :void {
    this.first = null;
    this.cmp = cmp
    }

  def put(key :kt, value :vt) :void {
    this.first = new Link<(kt, vt)>((key, value), this.first)
    }

  def lookup(key :kt) : Maybe vt {
    this.lookupHelper(key, this.first, this.cmp)
    }

  def lookupHelper(key :kt, current :Link<(kt, vt)>, cmp :(kt, kt) -> int) :Maybe vt{
    match current with
      null => Nothing : Maybe vt
      link((k, v), next) when cmp(k, key) == 0 => Just v
      link(x, next) => this.lookupHelper(key, next, cmp)
      }

class Main
  def main() : void
    let l = new AssocList<string, int>(strCmp)
    in{
      l.put("foo", 42);
      l.put("bar", 4711);
      match l.lookup("bar") with
        Just res => print res
        Nothing => print "Error"
    }
```
@EliasC
Copy link
Contributor Author

EliasC commented Nov 12, 2015

@glundi: Note that you are the author of the commit, even though I created the PR (your contributions will still go into history 😃)

@ghost
Copy link

ghost commented Nov 12, 2015

Thanks! Should I close the other PR?

@EliasC
Copy link
Contributor Author

EliasC commented Nov 12, 2015

We have a meeting tomorrow, after which we should have decided which branch this goes into.

EliasC added a commit that referenced this pull request Nov 13, 2015
This pull request features a full overhaul of the pattern matching expression
@EliasC EliasC merged commit c64807b into parapluu:features/plenary Nov 13, 2015
@EliasC EliasC deleted the features/pattern-matching branch November 13, 2015 10:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants