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

Proposal: Switch lambdas #4

Open
vihanb opened this issue Jun 15, 2016 · 13 comments
Open

Proposal: Switch lambdas #4

vihanb opened this issue Jun 15, 2016 · 13 comments
Labels
Milestone

Comments

@vihanb
Copy link
Member

vihanb commented Jun 15, 2016

This proposal details the workings of the planned switch (optionally *) statement.

Overview

The switch lambda is a block statement which functions both as a literal and a conditional block. In it's literal form it owns its own class rather than a TC link.

switch foo {
    case 1, 2: print "Foo is one or two"
    case 3, 4: print "Foo is three or four"
    else: print "Foo is something else"
}

Definition

The syntax is either switch or switch * where switch * specifies a switch literal. There may be any amount of whitespace between switch and *, if * exists. Following is the expression to be evaluated if the switch matches. This is followed by a block surrounded with braces {} of newline-seperated statements, which are the conditionals. Conditionals either begin with case or else, if case, then the beginning description is optional. Following is an expression which returns the value to be matched against, commands in these expressions serve as a logical OR during matching. These are followed by a : representing a single statement may follow. Or curly braces {} which allows for a multi-line code block

Formal Grammar

EBNF

@         = "switch" , [ "*" ] , expression , "{" , @case

@case     = [ ["case"] | "default" ] , @caselist , @block , @case
          | "}"

@caselist = expression , { "," , expression }

@block    = "{" code block "}"
          | ":" statement
@vihanb vihanb modified the milestones: v1-alpha, v0.1-alpha Jun 15, 2016
@ConorOBrien-Foxx
Copy link
Member

"Following is the expression returning the value to be evaluated if the switch matches" -- does that mean one can assign a value to the switch? I hope not. If not, here is a better wording: "Following is the expression to be evaluated if the switch matches."

@vihanb vihanb modified the milestones: v0.4-alpha, v1-beta Jun 27, 2016
@somebody1234
Copy link
Contributor

somebody1234 commented Jul 15, 2016

I'd also like a Ruby-style match syntax for non-primitives (i.e. using functions/regeces):
(maybe it can be in normal switch if comparing functions/regeces/objects isn't needed)

match foo {
    case /1$/: print "Foo ends with a one"
    case =>(foo) foo % 4: print "Foo is not divisible by 4"
    //maybe this as well when lazy ranges (a bit like generators) are implemented
    case 'foo'...'bar.': print "Foo is greater than 'foo' but less than 'bar.'"
}

@vihanb
Copy link
Member Author

vihanb commented Jul 18, 2016

@somebody1234 hm okay. I can understand lambdas but with regexes, how will it know if the two items are equal?

@ConorOBrien-Foxx
Copy link
Member

@vihanb what do you mean by "equal"?

@RikerW
Copy link

RikerW commented Jul 18, 2016

regeces

Regexes....

@rjhunjhunwala
Copy link

If you implement it using regexes for non primitives you lose the speed up offered by switch statements on close ranges of integers.

@somebody1234
Copy link
Contributor

@vihanb Ruby makes it match when it's a string and the regex matches IIRC

@vihanb
Copy link
Member Author

vihanb commented Aug 6, 2016

@somebody1234 right, but what I meant was. what function will it check to see if it matches a case. Should it use the == operator? Should it have a special .matches?

@somebody1234
Copy link
Contributor

somebody1234 commented Aug 6, 2016

case should use the default ===, or a function that is specified, I guess? (In case you want to compare objects by certain properties or something)

@ConorOBrien-Foxx
Copy link
Member

Idea: when <x> for literal matching, case <x> for === matching.

@somebody1234
Copy link
Contributor

@ConorOBrien-Foxx Literal matching? like regex/lambda matching?

@schas002
Copy link

schas002 commented Oct 9, 2016

Yay, tag action galore 😑

@ConorOBrien-Foxx
Copy link
Member

The syntax is kind of vague as of now. When does the switch end? When the next condition begins?
Simple example:

let truth_machine = n -> {
    switch n {
        case 0: print n;
        case 1: print n; truth_machine(1);
        else: print "...?";
    }
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants