Skip to content
This repository has been archived by the owner on Jun 20, 2021. It is now read-only.

Support for var and strict. #42

Open
Youngestdev opened this issue Oct 12, 2018 · 12 comments
Open

Support for var and strict. #42

Youngestdev opened this issue Oct 12, 2018 · 12 comments
Labels
enhancement New feature or request to-do An issue worthy of doing and looking into
Milestone

Comments

@Youngestdev
Copy link
Contributor

Youngestdev commented Oct 12, 2018

From the ROADMAP, I saw this :

add and use the keyword var to allow variable declaration

What's this all about ? .
Do you mean to declare variables in simple-lang after this is accomplished, it'll be:

var name = "simple-lang"

instead of the current version of variable declaration ?

name = "simple-lang"

Also,

The support for strict is really nice but what are the plans for this ? Are we moving to a strictly typed syntax following the introduction of #38 ? . What improvements will the strictness bring especially to error handling ?

@Thecarisma
Copy link
Contributor

Thecarisma commented Oct 13, 2018

First and foremost. No matter what new feature or changes brought to the lang it will always be backward compatible and all previous structure and syntax will remain.

Use strict

The implementation of the use keyword will be as a directive to the compiler and VM for developer to instruct the lang to do something i.e it an instructional directive and can also be achieved with ## when implemented. The Use strict instruction will allow the compiler and VM to enforce variable declaration in program. In large and enterprise program weakly typed variables can misbehave and do what it not suppose to do or execute what not to hence costing alot hence the need for a strict instruction where needed e.g look at the below examples and see one of weak typed languages pitfall.

finalCounter = 0 
value = 2

for i = 0 to 10 { 
    count = i * value 
    finalCountre = finalCounter + count
}

@finalCounter

the expected output is

110

but this is what will be outputed

0

WAIT WHAT WHY
check this line finalCountre = finalCounter + count. Do you find the pitfall yet well it this finalCountre was never declared in the program and due to the fact the the lang is weakly typed the variable finalCountre is initialized with the value of finalCounter + count, but the variable finalCountre was just a simple typo error and thus make the program fails as the wrong output is been delivered.

There are other pitfall in weak type, The introduction of the use strict will bring the strong typed feature to the lang by request. Note that simple-lang is weak type by default. Thus for any variable to be used it has to be declared first but how ?

var

Thus for any variable to be used it has to be declared first but how ? by using the var keyword or the variable Object or Datatype to declare variable first before use hence we ll have

use strict

var one = 1 
int32 two = new int32(2)

What happening here

It simple the variable one is initialize as 1 if the use strict instruction has not been issued to the VM then the above is by default weak type so it same as

one = 1 
two = new int32(2)

but if the use strict instruction has been issued to the VM then variable one and two are true to there object/datatype and they cannot be assigned to another type except if init with var so this happens

use strict
...

one = "One as String" // since it initialized with `var`
two = "as two?" //throws an error as "as two" is a string and the variable is of the int32 data type

So the above program that output 0 if written in this way after the use strict and var has been added will ensure the program is ran as expected

use strict

var finalCounter = 0 
var value = 2

for i = 0 to 10 { 
    count = i * value 
    finalCountre = finalCounter + count
}

@finalCounter

we get this in our std err

Line 7 -> RUNTIME ERROR 24 : The varible is not initialized: finalCountre
    in file consoledemo.sim

All will be well documented in future.

@Thecarisma Thecarisma added this to the 1.0.0 milestone Oct 13, 2018
@Youngestdev
Copy link
Contributor Author

From your explanation, all I can summarise is this :

The var keyword places a permanent datatype to its child such that a string cannot be changed to object, list or so. Isn't it ? And as such, it means for every variable used, they must be declared one way or the other ?

@Thecarisma
Copy link
Contributor

Thecarisma commented Oct 13, 2018

Not at all.

The var keyword if use on normal context it useless e.g var one = 1 is same as one = 1 but if var is combined with use strict then var is useful as it the only way to declare variable after use strict

use strict

var one = 1          // correct we initialize 'one'

one = 4               // correct since one has been initialized with 'var' above
one = "a string"     // correct var does not lock datatype it just for initializing
two = 3              //throws error 'two has not been initialized'

Therefore using the var keyword without first issuing use strict is meaningless

@Youngestdev
Copy link
Contributor Author

Okay I understand now. The use of var and strict is to ensure the proper variables used in a programme are declared already and give warning whilst compiling at runtime to avoid unseen bugs preventing app from giving expected output .

@Thecarisma
Copy link
Contributor

Yes

@Youngestdev
Copy link
Contributor Author

Well then, I'll close the issue. So when will this implemented ?

@Thecarisma
Copy link
Contributor

Anytime not so soon

@Youngestdev
Copy link
Contributor Author

Okay then. I'll sure look out on this.

@Thecarisma
Copy link
Contributor

Let leave it open till it done

@Thecarisma Thecarisma reopened this Oct 13, 2018
@appcypher
Copy link
Contributor

appcypher commented Oct 13, 2018

Simple is still new and the issue of backwards compatibility shouldn't be a major concern at this point.
Why not get rid of the old syntax and make use strict the default compiler context; I know a lot of people will simply use use strict everywhere instead of relying on their senses to catch the bugs.

And a var keyword doesn't make simple any less dynamic it just makes it more apprehensive of declarations. Languages like Javascript and Dart for example have var for marking declaration. Not having a way to mark declaration point has always been a thorn in the side of designers of dynamic language because then they have to introduce other keywords like local, global, etc.

@Youngestdev
Copy link
Contributor Author

I think @appcypher has a point. Not everyone find the bugs easily perhaps having a syntax change is okay since we have the final implemented we could have var and something like const for constants.

@Thecarisma
Copy link
Contributor

making use strict the default compiler context will totally eliminate the weak typed feature of simple-lang and make it less dynamic. strong type can be safer but it still does not override the benefits of the weak type feature

@Thecarisma Thecarisma modified the milestones: 1.0.0, 0.5.0 Nov 1, 2018
@Thecarisma Thecarisma added enhancement New feature or request to-do An issue worthy of doing and looking into labels Nov 1, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request to-do An issue worthy of doing and looking into
Projects
None yet
Development

No branches or pull requests

3 participants