Re: A Walrus :=
Posted: Wed Sep 13, 2023 12:43 pm
I am of the same mindLord wrote: Wed Sep 13, 2023 7:14 am Please stay BASIC.
There are too many influences from other programming languages lately.![]()
I am of the same mindLord wrote: Wed Sep 13, 2023 7:14 am Please stay BASIC.
There are too many influences from other programming languages lately.![]()
That's sort of what I was going for: a more BASIC-way, using a keyword in plain English rather than special symbolic operator. (Though, ":=" does harken back fun times dabbling in Delphi, as well as more recently Go).the.weavster wrote: Wed Sep 13, 2023 9:00 amIf an alternative syntax is considered to be more BASIC that's cool, I'd just like to see that pattern, which is common in PB, compressed.yuki wrote: Tue Sep 12, 2023 7:27 pm Though, I might put forward an alternative syntax suggestion:Code: Select all
If Define result = OpenFile(#PB_Any, filename$) ; do work with result EndIf
Code: Select all
; Try and open main or fallback file, and read all text from it.
If (file := ReadFile(#PB_Any, "path") Or file := ReadFile(#PB_Any, "path-fallback")) And (fileText$ := ReadString(file, #PB_File_IgnoreEOL)
; File exists and has non-empty text. Do something with it. Maybe parse.
*something = DoSomethingWithFileContents(fileText$)
Else
; File does not exist or is empty. Do some other thing. Maybe create/init defaults.
*something = DoSomethingWhenMissingFile()
EndIf
Code: Select all
; Try and read from main or fallback locations...
file = ReadFile(#PB_Any, "path")
If Not file
file = ReadFile(#PB_Any, "path-fallback")
EndIf
; Read file text if we've opened one...
If file
fileText$ = ReadString(file, #PB_File_IgnoreEOL)
EndIf
; Process contents if any, otherwise create defaults...
If fileText$
*something = DoSomethingWithFileContents(fileText$)
Else
*something = DoSomethingWhenMissingFile()
EndIf
What do you mean? Even with the walrus you can still use = plus an if-clause and avoid it.
PB is one of the furthest BASIC-style languages from actual BASIC, so no idea what you're on about. I've never seen another BASIC language use . for types, for example.
Um...no it doesn't? We're not at all asking for the old syntax to be removed. Even if the docs were updated with this syntax, that still doesn't mean you need it.
As I wrote before, I have nothing at all against the Walrus operator ...jacdelad wrote: Wed Sep 13, 2023 4:18 pmEven with the walrus you can still use = plus an if-clause and avoid it.
The following “argument” comes up repeatedly year after year:Code: Select all
If Define result = OpenFile(#PB_Any, filename$) ; do work with result EndIf
I'm somewhat tired of responding to that, so here is just a short version.Nobody would be forced to use it...
I figured you meant the code-sharing aspect of things, thus taking on the feature from 3rd parties. That's a straightforward, respectable opinion.Little John wrote: Mon Sep 25, 2023 6:44 pm However, the problem is about readability. People are exchanging code e.g.here on the forum, aren't they?
So if I want to discuss PureBasic code posted here about a topic I am interested in, then I have to read it. Usage of code does not only include writing, but also reading. And I don't want to get headache when reading PureBasic code.
Code: Select all
If Not file := OpenFile(#PB_Any, "file.txt")
MessageRequester("Error", "Can not open file.")
EndIf
Code: Select all
If Not (file := OpenFile(#PB_Any, "file.txt"))
MessageRequester("Error", "Can not open file.")
EndIf
Code: Select all
If (length := Len(text)) > 2
Debug "String too long."
EndIf
Code: Select all
If (file := CreateFile(#PB_Any, "create_me.txt")) And CloseFile(file)
Debug "File created."
Else
Debug "Oh oh."
EndIf
Code: Select all
; Without Walrus (option 1)
Repeat
*element = NextElement(mylist())
If Not *element
Break
EndIf
; do stuff
ForEver
; Without Walrus (option 2)
*element = NextElement(mylist())
While *element
; do stuff
*element = NextElement(mylist())
Wend
; With Walrus
While *element := NextElement(mylist())
; do stuff
Wend
That's a good example!NicTheQuick wrote:Code: Select all
; With Walrus While *element := NextElement(mylist()) ; do stuff Wend
Watch out: in this case, your program will always "oh oh"NicTheQuick wrote: Tue Sep 26, 2023 10:34 am And can you reuse the assigned variable in the same condition?Code: Select all
If (file := CreateFile(#PB_Any, "create_me.txt")) And CloseFile(file) Debug "File created." Else Debug "Oh oh." EndIf
I'm very prone to this pattern.NicTheQuick wrote: Tue Sep 26, 2023 10:34 amCode: Select all
While *element := NextElement(mylist()) ; do stuff Wend
Haha, you're right. CloseFile() never returns anything, at least according to the documentation. Well, I think you've understood what I meant with that example.yuki wrote: Tue Sep 26, 2023 5:39 pmWatch out: in this case, your program will always "oh oh"NicTheQuick wrote: Tue Sep 26, 2023 10:34 am And can you reuse the assigned variable in the same condition?Code: Select all
If (file := CreateFile(#PB_Any, "create_me.txt")) And CloseFile(file) Debug "File created." Else Debug "Oh oh." EndIf
The more versatile the better imo. Just expanding a little on the OpenFile() example:NicTheQuick wrote: Tue Sep 26, 2023 10:34 am But besides a simple If condition we could use it with other things quite well
Code: Select all
If file := OpenFile(#PB_Any, "file.txt")
While text.s := ReadString(file)
; do work with text
Wend
CloseFile(file)
EndIf