Crystal: Suggestion: one-line def

Created on 3 May 2017  路  3Comments  路  Source: crystal-lang/crystal

Opening as mentioned here: https://github.com/crystal-lang/crystal/issues/4372#issuecomment-298999533

I propose that we allow one-line defs, similar to what is done with abstract method definitions

# As used in abstract def
abstract def implement_me

# Also available in other types of method definitions
def initialize(@name : String)

Most helpful comment

This is a bit messy parser-wise.

Given the following:

def initialize(@x)
x = 1

is x = 1 supposed to be evaluated inside initialize or at the class scope? Crystal wouldn't be able to know this at all without knowing whether or not there's an extra end...somewhere. It's not indentation-sensitive, like Python is.

I do wish that we could just do def f end without the semicolon, though...

All 3 comments

This is a bit messy parser-wise.

Given the following:

def initialize(@x)
x = 1

is x = 1 supposed to be evaluated inside initialize or at the class scope? Crystal wouldn't be able to know this at all without knowing whether or not there's an extra end...somewhere. It's not indentation-sensitive, like Python is.

I do wish that we could just do def f end without the semicolon, though...

Closing. Very unlikely to happen. The parser would have to recover on quirks (e.g. def inside def) and so on as @kirbyfan64 states. Is an end keyword that ugly, noisy or boring to type (if not automagically inserted by your editor)?

I don't think it's that ugly, no. I just thought it might be better as one line, but I understand why it would be hard to do. Thanks for considering it

Was this page helpful?
0 / 5 - 0 ratings

Related issues

oprypin picture oprypin  路  3Comments

jhass picture jhass  路  3Comments

oprypin picture oprypin  路  3Comments

pbrusco picture pbrusco  路  3Comments

Papierkorb picture Papierkorb  路  3Comments