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

ADTs + Match #11

Open
jprochazk opened this issue Feb 24, 2023 · 0 comments
Open

ADTs + Match #11

jprochazk opened this issue Feb 24, 2023 · 0 comments

Comments

@jprochazk
Copy link
Owner

jprochazk commented Feb 24, 2023

enum Modifier:
  Shift = 0x01
  Ctrl = 0x02
  Alt = 0x04

enum Event:
  Click:
    x: int
    y: int
  MouseMove:
    dx: int
    dy: int
  KeyPress:
    mod: Modifier
    code: int
match v:
  case "a": pass
  case "b" | "c": pass

match response.status:
  case 100..=199: print "info"
  case 200..=299: print "success"
  case 300..=399: print "redirect"
  case 400..=499: print "client error"
  case 500..: print "server error"

match v:
  case true: pass
  case false: pass

match v:
  case _: pass

fn first(list):
  match items:
    case [v]:
      return v
  return none

fn last(list):
  match items:
    case [*, v]:
      return v
  return none

fn middle(list):
  match items:
    case [_, *v, _]:
      return v
  return []

print first([3, 4, 5]), first([])
print last([3, 4, 5]), last([])
print middle([3, 4, 5, 6]), middle([3, 6])

for action in actions:
  match action:
    case {text: message, color: c}:
      ui.set_text_color(c)
      ui.display(message)
    case {sleep: duration}:
      ui.wait(duration)
    case {sound: url, format: "ogg"}:
      ui.play(url)
    case {sound: _, format: _}:
      warning("Unsupported audio format")

match Test(a=0, b=1):
  case Test(a=0..=10, b=1..=5):
    print a, b
  case _:
    print "?"

match event():
  case Event.Click(x, y):
    print "click", x, y
  case e:
    print "unhandled event", e
class A: ...
   
enum Test:
  A:
    a: A
  B:
   x: int
   y: int

match Test.A(a=A(5)):
  case Test.A(a=A(x)): ...
  case Test.B(x, y): ...

# vs
enum Test:
  A(A)
  B: 
    x: int
    y: int

match Test.A(A(5)):
  case Test.A(A(x)): ...
  case Test.B(x, y): ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Status: No status
Development

No branches or pull requests

1 participant