1. Short form of "if" statement
- Posted by SnakeCharmer Dec 26, 2012
- 1112 views
Offer to include "then" keyword for "if" with only consequence and without "else" branch. Example:
if x < 0 then x = -x
instead of
if x < 0 x = -x end if
I think, it would be very useful.
2. Re: Short form of "if" statement
- Posted by ghaberek (admin) Dec 26, 2012
- 1098 views
Offer to include "then" keyword for "if" with only consequence and without "else" branch. Example:
if x < 0 then x = -x
instead of
if x < 0 x = -x end if
I think, it would be very useful.
You could use iif...
include "std/utils.e" x = iif( x < 0, -x, x )
Of course, mind the warnings...
Warning Note: You must take care when using this function because just like all other Euphoria routines, this does not do any lazy evaluation. All parameter expressions are evaluated before the function is called, thus, it cannot be used when one of the parameters could fail to evaluate correctly. For example, this is an improper use of the iif method
first = iif(sequence(var), var[1], var)
The reason for this is that both var[1] and var will be evaluated. Therefore if var happens to be an atom, the var[1] statement will fail. In situations like this, it is better to use the long method.
if sequence(var) then first = var[1] else first = var end if
Although, it looks like you're trying to get the absolute value of an atom, for which we already have an abs function:
include "std/math.e" x = abs({10.5, -12, 3}) -- x is {10.5, 12, 3} i = abs(-4) -- i is 4
-Greg
3. Re: Short form of "if" statement
- Posted by DerekParnell (admin) Dec 26, 2012
- 1065 views
Offer to include "then" keyword for "if" with only consequence and without "else" branch. Example:
if x < 0 then x = -x
instead of
if x < 0 x = -x end if
I think, it would be very useful.
Note that 'then' is required for the 'if' statement, so your second code example should be ...
if x < 0 then x = -x end if
Also note that you do not have to use multiple lines when using the 'if - then - end if' form. eg...
if x < 0 then x = -x end if
Which is very close to what your suggestion was about.
The reason that the syntax requires 'then', and 'end if' is to allow multiple statements to exist in an 'if' statement and to not rely on text (line) formatting as a syntax requirement.
4. Re: Short form of "if" statement
- Posted by jimcbrown (admin) Dec 26, 2012
- 1180 views
Although, it looks like you're trying to get the absolute value of an atom, for which we already have an abs function:
include "std/math.e" x = abs({10.5, -12, 3}) -- x is {10.5, 12, 3} i = abs(-4) -- i is 4
-Greg
I think this is the right way to do it.
Offer to include "then" keyword for "if" with only consequence and without "else" branch. Example:
if x < 0 then x = -x
instead of
if x < 0 x = -x end if
I think, it would be very useful.
You could use iif...
include "std/utils.e" x = iif( x < 0, -x, x )
Of course, mind the warnings...
Warning Note: You must take care when using this function because just like all other Euphoria routines, this does not do any lazy evaluation. All parameter expressions are evaluated before the function is called, thus, it cannot be used when one of the parameters could fail to evaluate correctly. For example, this is an improper use of the iif method
first = iif(sequence(var), var[1], var)
The reason for this is that both var[1] and var will be evaluated. Therefore if var happens to be an atom, the var[1] statement will fail. In situations like this, it is better to use the long method.
if sequence(var) then first = var[1] else first = var end if
One could use the iif preprocessor to add support for lazy evaluation: http://scm.openeuphoria.org/hg/euphoria/rev/cf8a5675c514
5. Re: Short form of "if" statement
- Posted by SnakeCharmer Dec 27, 2012
- 1016 views
Also note that you do not have to use multiple lines when using the 'if - then - end if' form. eg...
if x < 0 then x = -x end if
Which is very close to what your suggestion was about.
Yeah, you are right. Two BASIC dialects were mixed in my brain. Excuse for troubling.
P.S. I am going to refuse the second dialect - PureBasic. Euphoria is able everything that I need. The euphoric feeling better than purity!