Page 1 of 1
Syntax Enhancements: If on-the-fly & expression w/ var asgn.
Posted: Tue Apr 13, 2010 9:56 pm
by crackhead
Hi,
I don't know, if this is already requested, implemented, not implementable due to other syntactical constructs or that ugly and unuseful, that it'll never be wanted
Sorry, I'm new..
1. If "on-the-fly":
something like
Code: Select all
SetGadgetText(#MyButton, (MyVar > 0) ? "MyVar > 0" : "MyVar <= 0")
like in C, maybe adapted to BASIC-syntax
2. Expressions with variable-assignment:
Code: Select all
While (MyVar = MyProcedure) > 0
MessageRequester("", Str(MyVar))
Wend
3. Short if:
using if - endif seems to be a bit too much for a single command like
Code: Select all
If Not DoneYet
MessageRequester("", "Not done yet.")
EndIf
and
Code: Select all
If Not DoneYet : MessageRequester("", "Not done yet.") : EndIf
isn't that readable
What about something like
Code: Select all
If Not DoneYet Then MessageRequester("", "Not done yet.")
and
Code: Select all
While MyProcedure > 0 Do MessageRequester("", Str(MyVar))
4. Also, how about ForEach with an array?
One could use For with ArraySize,
but couldn't that be implemented internally into ForEach, too?
I hope my requests aren't too dumb,
Thanks.
Re: Syntax Enhancements: If on-the-fly & expression w/ var a
Posted: Tue Apr 13, 2010 11:01 pm
by netmaestro
4. Also, how about ForEach with an array?
One could use For with ArraySize,
but couldn't that be implemented internally into ForEach, too?
I like this one.
Re: Syntax Enhancements: If on-the-fly & expression w/ var a
Posted: Wed Apr 14, 2010 8:29 am
by #NULL
1. i would like to have a conditional operator too. see also these threads:
viewtopic.php?f=3&t=28740&start=0&sid=d ... 2991a4f34e
http://forum.purebasic.com/english/view ... 7d8b07d471
2. this was requested from time to time iirc, but i don't think the pb-team will do it. it's C-ish and too ambiguous for PB style.
3. personally i like the : operator and use it often especially for many sequent single-line If-Endifs. i don't like the Then nor leaving off the EndIf. remember you don't have a explicit block syntax รก la { and } in PB.
4. i didn't miss that yet, but why not. it would look much simpler for multi-dim arrays instead of many nested For-loops. but then i would still want to specify index identifier to be able to know where i am during the looping.
Code: Select all
Dim arr(3,4,5)
ForEach arr(x,y,z)
If y = 2
arr(x,y,z) = 0
Else
arr(x,y,z) = 123
EndIf
Next
Re: Syntax Enhancements: If on-the-fly & expression w/ var a
Posted: Wed Apr 14, 2010 8:57 am
by Kaeru Gaman
If you want C, why don't you use C?
1. this is clearly C syntax. PureBasic is a BASIC language.
Evalution for Expressions is planned.
Boolean( Expr ) will evaluate 1 or 0 depending on Expr to be True or False.
Your example just compresses five lines of code into one for the cost of readability.
the compiled result still needs to be the same ASM to provide same functionality,
so where should be any win in this cryptic mess?
2. this is also C syntax. may I repeat: PureBasic is a BASIC language.
it is not at all possible without introducing an assignment operator devided from the comparison operator.
in BASIC syntax, your example should evaluate the comparison (MyVar = MyProcedure) if it's True.
PureBasic does not support such evaluating expessions yet (see above)
3. the EndIf was introduced into BASIC decades to enable the Then branch to hold a block of code in more than one line.
in older times you had to write all commands of the Then branch into the same Line.
nevertheless, maybe it is possible the implement this additional alternative syntax.
4. why not?
never thought about it, don't think ever need it, but why not?
Re: Syntax Enhancements: If on-the-fly & expression w/ var a
Posted: Wed Apr 14, 2010 3:16 pm
by crackhead
I guess there's a difference between syntactical approaches and a whole different language + compiler.
1)
"for the cost of readability"
you can't simply generalize it, also it wouldn't mean one would have to use it.
2)
Ah, right, I didn't think of the possible need of a different operator
3)
yeah, just as an alternative syntax so that you can choose,
since I think, it'd be more readable than such a block
Code: Select all
If a = "he"
DoThis()
Endif
If y = z
DoThat()
Endif
If x
DoSomething()
Endif
If Error
ClosePlease()
Endif
alternative syntax/1-line-if:
Code: Select all
If a = "he" Then DoThis()
If y = z Then DoThat()
If x Then DoSomething()
If Error Then ClosePlease()
Or formatted:
Code: Select all
If a = "he" Then DoThis()
If y = z Then DoThat()
If x Then DoSomething()
If Error Then ClosePlease()
Re: Syntax Enhancements: If on-the-fly & expression w/ var a
Posted: Wed Apr 14, 2010 3:28 pm
by ts-soft
you can use this:
Code: Select all
Macro Then
:
EndMacro
Define a = 1
If a = 1 Then Debug "True" Then EndIf

Re: Syntax Enhancements: If on-the-fly & expression w/ var a
Posted: Thu Apr 15, 2010 7:41 am
by blueznl
My eyes! My eyes!
Re: Syntax Enhancements: If on-the-fly & expression w/ var a
Posted: Thu Apr 15, 2010 10:06 am
by Foz
I like inline conditionals. At least for when we deal with grammer:
Code: Select all
; n.b. macro to be removed when the actual Boolean function is released.
Macro boolean(_expression_)
((_expression_) Or #False)
EndMacro
Procedure.s IIFs(condition, True.s, False.s)
If condition
ProcedureReturn True
Else
ProcedureReturn False
EndIf
EndProcedure
a = 1
Debug Str(a) + " Carton" + IIFs(boolean(a = 1), "", "s")
a = 7
Debug Str(a) + " Carton" + IIFs(boolean(a = 1), "", "s")