Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFC] Destructuring With Inline Transforms #5

Open
synergixe opened this issue Jun 23, 2019 · 3 comments
Open

[RFC] Destructuring With Inline Transforms #5

synergixe opened this issue Jun 23, 2019 · 3 comments

Comments

@synergixe
Copy link

synergixe commented Jun 23, 2019

The current standing of simple lang is that string and array variables cannot be destructured. This seeks to provide a basis for syntax to destructure string and array variables into other arrays or string variable(s) while transforming them to other data types like integer . The dollar sign in there stands as a placeholder variable for a matched type variable to be transformed.

NEW KEYWORDS:

  • match
  • oftype

Synergixe hopes to sponsor an RFC that changes this slightly and adds the concept of Dsetructuring with Inline transformation of data types as they are destrcutured (derived from JavaScript programming language) into the simple language.

Code Examples



 block access(params...)
	@params
	
	return params



garth... ~ match { oftype "integer" => ($ * 3) } ?= access(1, 2, 3) // garth is new array variable created

day, month, year ?= "14-09-2019" // day, month , year - new variables created

day, month, year ~ match { oftype "string" => ( toDecimalInt($) ) } ?= "14-09-2019"

We believe this proposition will reduce boilerplate code

What do you think ?

@synergixe synergixe changed the title [RFC] Destructuring With And Without Inline Transforms [RFC] Destructuring With Inline Transforms Jun 23, 2019
@Thecarisma
Copy link
Contributor

This will definitely happen but with limit.
The keyword oftype is not needed the keyword is exist in new simple-lang and has same functionality as copied from kotlin.

Destructuring with match I do not see it happening within the core. simple-lang is structure in a way the VM is small and does basic operation only, high level operations will rely on libraries and functions. The following type of Destructuring will be implemented

var day, month, year ?= "14-09-2019" // day, month , year - new variables created

The following will also exist with helper functions and not as a core feature

var day, month, year ~ match { oftype "string" => ( toDecimalInt($) ) } ?= "14-09-2019"

Understanding the Destructuring expression above means that for each of the characters in the string "14-09-2019" if any matches a DecimalInt it should be destructured such that the result is as followed

day = 14
month = 19
year = 2019

The same result can be achieved using a function for matching the type from the string and returns the array e.g

block destruct(value:string, type:string, func)
  ...
end
var day, month, year ?= destruct("14-09-2019",  "string",  toDecimal)

@synergixe
Copy link
Author

synergixe commented Jun 25, 2019


var day, month, year ?= "14-09-2019" // day, month , year - new variables created


block destruct(value:string, type:string, func)
  ...
end
var day, month, year ?= destruct("14-09-2019",  "string",  toDecimal)

Do love these example above. I believe they are apt for simple going forward. The compromise is balanced and good to go!

Why not make destruct function a standard function in simple (since it will kind of be part of the language signature) ?

Also, we could use standard constants to depict the type by which the variable to be destructed must be matched. The constant can be composed in form similar to a struct (as in the C language)

Code Example


block destruct(value:string, type:string, func)
  ...
end
var day, month, year ?= destruct("14-09-2019",  TypeMatch.string,  toDecimal)

@isocroft
Copy link

isocroft commented Jul 9, 2019

Interesting discussion here... I do have a suggestion however

Drawing from your assertion @Thecarisma below,

The following will also exist with helper functions and not as a core feature

var day, month, year ~ match { oftype "string" => ( toDecimalInt($) ) } ?= "14-09-2019"

We can borrow from Rust lang to overload the match keyword for "pattern matching"

Example Rust code below:

loop {
     let ch = self.next()?;
     match ch {
         '"' | '\'' => {
             
         }
         '0' => {

          }
         ';' => {

         }
         _ if ch.is_digit(10) => {

         }     
     }
}

So, the above code does pattern matching on a char variable (in a loop) and tries to do something upon a character match.

We can have the below in Simple lang


var ch = "g"
~ match ch {
     is "k" {
         # do something when ch is a "k"
     }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants