Syntax Enhancements: If on-the-fly & expression w/ var asgn.

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
crackhead
User
User
Posts: 12
Joined: Tue Apr 06, 2010 9:26 pm

Syntax Enhancements: If on-the-fly & expression w/ var asgn.

Post 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.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Syntax Enhancements: If on-the-fly & expression w/ var a

Post 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.
BERESHEIT
#NULL
Addict
Addict
Posts: 1497
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: Syntax Enhancements: If on-the-fly & expression w/ var a

Post 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
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Re: Syntax Enhancements: If on-the-fly & expression w/ var a

Post 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?
oh... and have a nice day.
crackhead
User
User
Posts: 12
Joined: Tue Apr 06, 2010 9:26 pm

Re: Syntax Enhancements: If on-the-fly & expression w/ var a

Post 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()
Last edited by crackhead on Wed Apr 14, 2010 3:33 pm, edited 1 time in total.
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: Syntax Enhancements: If on-the-fly & expression w/ var a

Post by ts-soft »

you can use this:

Code: Select all

Macro Then
  :
EndMacro

Define a = 1

If a = 1 Then Debug "True" Then EndIf
:mrgreen:
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Re: Syntax Enhancements: If on-the-fly & expression w/ var a

Post by blueznl »

My eyes! My eyes!
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
Foz
Addict
Addict
Posts: 1359
Joined: Tue Nov 13, 2007 12:42 pm
Location: Manchester, UK

Re: Syntax Enhancements: If on-the-fly & expression w/ var a

Post 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")
Post Reply