Please consider allowing a single line of code to be constructed using a prefix if expression

54 views
Skip to first unread message

Shooting Fly

unread,
Oct 27, 2018, 6:48:44 AM10/27/18
to Crystal
I know in Ruby or Crystal, we can use a suffix if expression to make something readable. 

def demo(a, b)
 
return false if (a - b > 10)    
 
return false if (b - a > 10)
 
true
end


But in C, C++, Java, PHP, Python, Go, Elixir, Rust, and etc, it allows a single line of code to be constructed using a prefix if expression.

def demo(a, b)
   
if (a - b > 10) return false;
   
if (b - a > 10) return false;  
   
true
end


As a Chinese, because we use if statement at first in Chinese grammar, the second one leads the code more natural to read for us. 

Ary Borenszweig

unread,
Oct 27, 2018, 7:16:50 AM10/27/18
to crysta...@googlegroups.com
You can do it:

---
def demo(a, b)
  if (a - b > 10); return false; end
  if (b - a > 10); return false; end
  true
end

demo(1, 2)
---


--
You received this message because you are subscribed to the Google Groups "Crystal" group.
To unsubscribe from this group and stop receiving emails from it, send an email to crystal-lang...@googlegroups.com.
To post to this group, send email to crysta...@googlegroups.com.
Visit this group at https://groups.google.com/group/crystal-lang.
To view this discussion on the web visit https://groups.google.com/d/msgid/crystal-lang/490d9d27-ffb8-4def-9576-2486085dd218%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Shooting Fly

unread,
Oct 29, 2018, 11:52:51 PM10/29/18
to Crystal
I know, but it is very ugly to use two semicolons and an `end`. What I want is to offer a new way to write single line if.

在 2018年10月27日星期六 UTC+8下午7:16:50,Ary Borenszweig写道:

Jack Thorne

unread,
Oct 30, 2018, 2:16:28 PM10/30/18
to Crystal
I would try opening up an issue in github issues page.
I am guessing with any syntax change there will be some concerns about effects across the codebase.
Reply all
Reply to author
Forward
0 new messages