nvie / nox-ideas

Ideas for a new kind of programming language

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

General formatted syntax for conditionals?

bskinn opened this issue · comments

Looking at this:

func fib(n: Int): Int {
  return
    | n <= 1  -> min(n, 0)
    | _       -> fib(n-1) + fib(n-2)
}

I'm curious, are you envisioning that pipe-prefixed multiline syntax as a general way for expressing conditionals, or just as something specific to function returns?

Hi @bskinn! How awesome you already found this little toy playground which is really nothing more than a figment of my imagination at this point. I'm using this repo to collect ideas I like in other programming languages, and trying to imagine what this programming language would be like. No concrete plans for implementing a grammar yet, even. Just collecting and codifying some ideas.

Yeah, I'm not sure about this construct yet, but something is appealing about it to me. In this case, it'd be basically another syntax for a "switch", like:

return cond1
  ? result1
  : cond2
  ? result2
  : result3

But if you look at this, it would also allow you to "fall through" if none of the conditions match.

I think the general shape of this expression would roughly be like

return
  | <expr1> -> <result1>
  | <expr2> -> <result2>

I haven't made up my mind yet about the shape (or grammar complexity for that matter), but I like the readability of the multiline conditional expression for some reason. This expression's shape is very similar, but not quite the same as the "pattern matching" one, which would start with an expression:

return <expr>
  | <pattern1> -> <result>
  | <pattern2> -> <result>

Which is more comparable to a classic "switch" like:

switch (expr) {
  case <pattern1>: return <result>;
  case <pattern2>: return <result>;
}

There's nothing special about the fact that it's used as part of the return here. It's just an expression. You could also assign variables this way:

output = n
  | 0 -> <result>
  | 1 -> <result>
  | _ -> <result>   // or omit, to "fall through"
return output

After some more thinking about this with "real world code", I decided to abandon this "fancy conditional" syntax idea with the pipes, as it would be too closely resembling the pattern matching syntax, and not add much value — all cases can also be expressed using a normal pattern match or normal conditionals. Removed in 5b0ed12.