The Crystal programming language | http://crystal-lang.org | Fund Crystal's development: http://is.gd/X7PRtI | Docs: http://crystal-lang.org/docs/ | API: http://crystal-lang.org/api/
<raz> in many (most?) languages assignment in conditional is a compiler warning or even error. and i think for good reason.
that is not true though
if
, have to double-take almost every time i see it. kotlin's solution feels cleaner to me, they made the safe-nav operators convenient (even added elvis-op) and
if foo = some_nilable
I think that very clearly expresses the intent that the branch depends on some_nilable
being non-nil.
foo = some_nilable; if foo
as well. And I definitely prefer that if foo
is used outside the conditional as well.
foo
is just used as non-nil inside the branch, combining it with the condition itself seems like a good idea to me.
.not_nil!
and .try
as unwieldy as they are was to motivate users to avoid them. but then at the same time we have this little backdoor (if =)
?.
instead of .try &
)
if foo = some_nilable
would be foo = some_nilable; if foo
(or maybe a dedicated assignment operator as @asterite mentioned)
if foo = anything
is not allowed at all. i imagine they may have had a similar discussion in their past)
if foo = nilable
instead of foo.try {}
list-style-type: lower-alpha
in CSS)
i += 1
by i = i.succ
makes it even more generically useful
brew install crystal
and with --build-from-source
T
is restricted to be of type Vec
, which is used for both arguments and the return type. How can I do similar with Crystal, without a type alias or relying on the inferred call site types?proc abs[T: Vec](o: var T, v: T): var T = ...