Random question about languages

Started by Xaron, June 24, 2018, 19:19:35

Previous topic - Next topic

Xaron

In this thread I'd like to ask some questions where they just pop up in my mind. I just stumbled across things because I currently write a tokenizer/parser and just wonder about things while doing this.

So here's the first question: Why do languages like BlitzBasic, Monkey, even AGK need the keyword "Local" to declare a locale variable?

Why not just:
i:Int = 1

instead of:
Local i:Int = 1

Is there a deeper reason for this? Same is true for the Global keyword even though one could declare a global within a scope even though I personally think this is a bad design decision. What do you think?

Second question: What do you like as a starting character for comments?
;
'
//

? Does it even matter?

GW

Without the word local, it's an assignment, not a declaration. Or at least ambiguous to a parser.  Local also defines the scope of a variable. It helps compiler optimize things and free's the programmer having to hold a large mental model of the total scope.   

Henri

Hi,

local also means that variable resides in dynamic  memory (we need resources, allocate them, use them and then discard and left to be collected)
and global means static (c-keyword) that is accessible everywhere, and has reserved memory that stays through program lifecycle.

without this distinction would every variable be dynamic ? As far as I know, every language has these declarations (I could be wrong but anywho -:)).

-Henri
- Got 01100011 problems, but the bit ain't 00000001

Steve Elliott

Quote
So here's the first question: Why do languages like BlitzBasic, Monkey, even AGK need the keyword "Local" to declare a locale variable?

Partly why I don't use those languages - unnecessarily verbose.  In C++ and my own language scope is obvious without adding extra words.
Win11 64Gb 12th Gen Intel i9 12900K 3.2Ghz Nvidia RTX 3070Ti 8Gb
Win11 16Gb 12th Gen Intel i5 12450H 2Ghz Nvidia RTX 2050 8Gb
Win11  Pro 8Gb Celeron Intel UHD Graphics 600
Win10/Linux Mint 16Gb 4th Gen Intel i5 4570 3.2GHz, Nvidia GeForce GTX 1050 2Gb
macOS 32Gb Apple M2Max
pi5 8Gb
Spectrum Next 2Mb

Derron

Code (BlitzMax) Select

Type TTest
  Field x:int

  Method superduper()
    local x:int = 10
    self.x = 20
    print x + "  " + self.x
  End Method
End Type

new TTest.superduper()


Without the "local" you would see "20 20" instead of "10 20" printed.


bye
Ron

col

#5
I think the point Xaron is trying to get across is something like

'Why not use this'

Type TTest
  x:int

  Method superduper()
    x:int = 10
    self.x = 20
    print x + "  " + self.x
  End Method
End Type

new TTest.superduper()


Personally I agree that the Global, Field and Local keywords are unnecessary, and Global should be replaced with 'Static'.

QuoteSecond question: What do you like as a starting character for comments?
I use ' for a complete line comment and

'/ comment /'

for multi-line and inline comments.
https://github.com/davecamp

"When you observe the world through social media, you lose your faith in it."

Steve Elliott

Quote
Personally I agree that the Global, Field and Local keywords are unnecessary.

Definitely.
Win11 64Gb 12th Gen Intel i9 12900K 3.2Ghz Nvidia RTX 3070Ti 8Gb
Win11 16Gb 12th Gen Intel i5 12450H 2Ghz Nvidia RTX 2050 8Gb
Win11  Pro 8Gb Celeron Intel UHD Graphics 600
Win10/Linux Mint 16Gb 4th Gen Intel i5 4570 3.2GHz, Nvidia GeForce GTX 1050 2Gb
macOS 32Gb Apple M2Max
pi5 8Gb
Spectrum Next 2Mb

Derron

Quote from: col on June 25, 2018, 00:03:13
Personally I agree that the Global, Field and Local keywords are unnecessary, and Global should be replaced with 'Static'.

In German "statisch" is the translation for "static". One of the synonyms of "statisch" is "konstant" (English: "constant"). It means that something is not moving, does not change, fixed, invariable - but also "is there forever". Nonetheless the main connotation is "fixed/not changing".

"Global" on the other hand just translates to the German "global" as well, meaning "everywhere around the world", "worldwide", ... which pretty much is the same as "everywhere in the code".


So for me - as a German speaking coder - "global" really suits better than "static".
In Java "static" is some kind of "class-wide variable" - so what about "program wide variables"?


@ comments
I prefer /* ... */ if possible, but the apostrophe ' is also good to go ...
But there could also be the tertiary question if you put a whitespace after the comment token?

' hi
'hi
//commented out
// commented out

I tend to comment out code by steering the caret to the start of the line's actual content and just prepend the comment token - so no whitespace between "code" and "//". This of course most often is done that way as I use "ctrl + left/right" to navigate in lines and that "Geany" knows where you move along (move from "begin to begin"), so cursor up/down does not strictly move the caret from line 3:20 to line 4:20 but to the equal part of the above/below line.


bye
Ron

col

#8
QuoteIn German "statisch" is the translation for "static". One of the synonyms of "statisch" is "konstant"
Interesting.

QuoteWhy not just:
i:Int = 1

In many other languages the 'type' of the variable comes first. I think this makes sense when it comes to declaration/definition of a variable and is less verbose than 'basic' languages that have the 'type' after the variable.

'type' after the variable...
Local a:Int = 1, b:Int = 2, c:Int = 4, d:Float = 10.0

'type' before the variable without Local...
Int a=1, b=2, c=3, Float d = 10.0

While on this subject - what would people prefer to denote type for constant numerals...
10.f    ' float value
10.0f  ' float value
10.d   ' double value
10.0   ' double value
1010110b ' binary
$123ABC   ' hex
0x123ABC ' hex
0b110110 ' binary

??
I'm sure there are more options too, which syntax do you prefer?

QuoteBut there could also be the tertiary question if you put a whitespace after the comment token?
Could you elaborate with what you mean here.


https://github.com/davecamp

"When you observe the world through social media, you lose your faith in it."

Xaron

Thanks guys, very helpful insights! :)

@col: Yes, that's exactly what I meant.

Xaron

#10
Quote from: col on June 25, 2018, 09:38:14
In many other languages the 'type' of the variable comes first. I think this makes sense when it comes to declaration/definition of a variable and is less verbose than 'basic' languages that have the 'type' after the variable.

Ah haven't thought about that. Yes, in that case it makes sense to declare it with 'Local' before. It's more error prove then I guess.

So that means that for languages like Basic which have the type after the variable name it makes sense to have a 'Local' before the name to make it more clear.

Derron

@ (white)space before comment token
as written:
Code (BlitzMax) Select

' hey whitespace
'  even more whitespace
'no whitespace


BTW: {code=BlitzMax}...{/code} if you prefer semi-proper syntax highlighting.


bye
Ron

col

#12
I still don't understand what you mean  :P
Those comment lines are all the same thing to a parser. That is once the parser sees the 'line comment token' ( the apostrophe - ascii character 39 ) it will classify everything after it as a comment until either end-of-line or end-of-file, whichever is reached first.

Thanks for the 'code=BlitzMax' tip :)

edit: Correct if I'm wrong in that are you suggesting different things depending on if there is whitespace between the 'line comment token' and the 'commented text'?
https://github.com/davecamp

"When you observe the world through social media, you lose your faith in it."

Derron

No I just wanted to know what you prefer.
Eg. Brucey tends to use a whitespace between comment token and comment value - others too.

I most often skip adding any whitespace (as described above). But hey, I also set my Geany to remove whitespace at the end of lines (can look scary if you edit someone elses code files .... and the DIFF consists of 90 changed lines while you only appended a single line of code -- you then need to disable the auto-trim).


bye
Ron

col

Ahh I see.
I prefer a space between them.

@Xaron
What are you going to work on?
https://github.com/davecamp

"When you observe the world through social media, you lose your faith in it."