February 25, 2020, 03:19:06 PM

Author Topic: Random question about languages  (Read 3161 times)

Offline Xaron

  • Sr. Member
  • ****
  • Posts: 276
Random question about languages
« on: June 24, 2018, 07:19:35 PM »
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?

Offline GW

  • Full Member
  • ***
  • Posts: 184
Re: Random question about languages
« Reply #1 on: June 24, 2018, 08:02:02 PM »
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.   

Offline Henri

  • Full Member
  • ***
  • Posts: 220
Re: Random question about languages
« Reply #2 on: June 24, 2018, 08:46:45 PM »
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

Offline Steve Elliott

  • Hero Member
  • *****
  • Posts: 2241
  • elgol developer
Re: Random question about languages
« Reply #3 on: June 24, 2018, 09:13:12 PM »
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.
Windows 10, 64-bit, 16Gb RAM, CPU Intel i5, 3.2 GHz, Nvidia GeForce GTX 1050 (2Gb).
MacOS Catalina, 64-bit, 8Gb RAM, CPU Intel i5, 2.3 Ghz, Intel Iris Plus Graphics 640 1536 MB.
Linux Mint 19.3, 64-bit, 16Gb RAM, CPU Intel i5, 3.2 GHz, Nvidia GeForce GTX 1050 (2Gb).
C64, Raspberry pi 3 and 4.

Offline Derron

  • Hero Member
  • *****
  • Posts: 2719
Re: Random question about languages
« Reply #4 on: June 24, 2018, 10:53:48 PM »
Code: BlitzMax
  1. Type TTest
  2.   Field x:int
  3.  
  4.   Method superduper()
  5.     local x:int = 10
  6.     self.x = 20
  7.     print x + "  " + self.x
  8.   End Method
  9. End Type
  10.  
  11. new TTest.superduper()
  12.  

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


bye
Ron

Offline col

  • Sr. Member
  • ****
  • Posts: 441
Re: Random question about languages
« Reply #5 on: June 25, 2018, 12:03:13 AM »
I think the point Xaron is trying to get across is something like

 'Why not use this'
Code: [Select]
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'.

Quote
Second 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.
To be is to be perceived.

https://github.com/davecamp

Offline Steve Elliott

  • Hero Member
  • *****
  • Posts: 2241
  • elgol developer
Re: Random question about languages
« Reply #6 on: June 25, 2018, 12:08:10 AM »
Quote
Personally I agree that the Global, Field and Local keywords are unnecessary.

Definitely.
Windows 10, 64-bit, 16Gb RAM, CPU Intel i5, 3.2 GHz, Nvidia GeForce GTX 1050 (2Gb).
MacOS Catalina, 64-bit, 8Gb RAM, CPU Intel i5, 2.3 Ghz, Intel Iris Plus Graphics 640 1536 MB.
Linux Mint 19.3, 64-bit, 16Gb RAM, CPU Intel i5, 3.2 GHz, Nvidia GeForce GTX 1050 (2Gb).
C64, Raspberry pi 3 and 4.

Offline Derron

  • Hero Member
  • *****
  • Posts: 2719
Re: Random question about languages
« Reply #7 on: June 25, 2018, 07:11:41 AM »
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

Offline col

  • Sr. Member
  • ****
  • Posts: 441
Re: Random question about languages
« Reply #8 on: June 25, 2018, 09:38:14 AM »
Quote
In German "statisch" is the translation for "static". One of the synonyms of "statisch" is "konstant"
Interesting.

Quote
Why 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...
Code: [Select]
Local a:Int = 1, b:Int = 2, c:Int = 4, d:Float = 10.0
'type' before the variable without Local...
Code: [Select]
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?

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


To be is to be perceived.

https://github.com/davecamp

Offline Xaron

  • Sr. Member
  • ****
  • Posts: 276
Re: Random question about languages
« Reply #9 on: June 25, 2018, 09:44:29 AM »
Thanks guys, very helpful insights! :)

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

Offline Xaron

  • Sr. Member
  • ****
  • Posts: 276
Re: Random question about languages
« Reply #10 on: June 25, 2018, 09:46:26 AM »
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.

Offline Derron

  • Hero Member
  • *****
  • Posts: 2719
Re: Random question about languages
« Reply #11 on: June 25, 2018, 10:38:13 AM »
@ (white)space before comment token
as written:
Code: BlitzMax
  1. ' hey whitespace
  2. '  even more whitespace
  3. 'no whitespace
  4.  

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


bye
Ron

Offline col

  • Sr. Member
  • ****
  • Posts: 441
Re: Random question about languages
« Reply #12 on: June 25, 2018, 10:59:21 AM »
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'?
To be is to be perceived.

https://github.com/davecamp

Offline Derron

  • Hero Member
  • *****
  • Posts: 2719
Re: Random question about languages
« Reply #13 on: June 25, 2018, 12:33:06 PM »
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

Offline col

  • Sr. Member
  • ****
  • Posts: 441
Re: Random question about languages
« Reply #14 on: June 25, 2018, 03:19:19 PM »
Ahh I see.
I prefer a space between them.

@Xaron
What are you going to work on?
To be is to be perceived.

https://github.com/davecamp

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal