Crystal: Stricter grammar for `/`

Created on 25 Apr 2018  路  2Comments  路  Source: crystal-lang/crystal

This is probably intentional, but IMO it's messy:

class Foo
  def x(arg = 10)
    return arg
  end
end
foo = Foo.new

m = 1
x = 10

    x / 2/m  # => 5
    x /2/m   # => 5
foo.x / 2/m  # => 5
foo.x /2/m   # => /2/m
foo.x/2/m    # => 5
foo.x/ 2     # => 5
foo.x / 2    # => 5
#foo.x /2    # unterminated regular expression

For consistency, I think / should always signify division when used immediately after tokens that constitute or terminate expressions (i.e. the only places infix operators make sense) and a Regex literal after any other tokens (left-delimiters like ([{, commas or mathematical operators, conditional keywords such as if) or at the beginning of a statement.

This would break a lot of uses of String#match (many of which could be replaced with =~) and String#gsub (which would need added parentheses or a different Regex syntax) but it would make the language grammar and lexing slightly saner.

Most helpful comment

IMO binary operators - at least those that can be ambiguous (/+-) - should generally be surrounded with whitespace.

See also #5411

All 2 comments

IMO binary operators - at least those that can be ambiguous (/+-) - should generally be surrounded with whitespace.

See also #5411

The language works pretty much like Ruby. I think that's fine.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

asterite picture asterite  路  71Comments

asterite picture asterite  路  139Comments

chocolateboy picture chocolateboy  路  87Comments

stugol picture stugol  路  70Comments

malte-v picture malte-v  路  77Comments