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

Remove void "type"; convert statements to expressions #56

Open
Zamua opened this issue Oct 7, 2019 · 2 comments
Open

Remove void "type"; convert statements to expressions #56

Zamua opened this issue Oct 7, 2019 · 2 comments
Labels

Comments

@Zamua
Copy link
Contributor

Zamua commented Oct 7, 2019

Perhaps we should remove the use of void as a return type. Objects that perform an operation that modify themselves and don't necessarily have something to return can return themselves (similar to the builder pattern).

Example:

aList = [].add(1).add(2).add(3)
print(aList) // [1, 2, 3]

Procedures that aren't related to a class can always return a boolean indicating success or failure. We can even make the lack of a return be considered a success.

Example:

procedure1() {
    a = 10
    b = 10
    c = a + b
    print(c)
}

procedure1() // prints: '20', returns true

procedure2() {
    number = readInt()
    
     // This if "statement" would also technically be an expression that returns a boolean in this proposal
    if (number % 2 != 0) {
        // This can also be an expression maybe?
        return false
    }
}

procedure2() // < 1 returns false; < 2 returns true

It might encourage chaining procedures together like so:

checkCondition1(args) && checkCondition2(args) && checkCondition3(args) && doGuardedProcedure(args)

This is possible in the current state of LJ, but it might be further encouraged if we default return types to boolean (is this something we want to encourage?).

Presumably function definitions and class definitions would now also be expressions if we were strict about "no statements" which would mean LJ has first-class functions (and potentially lambdas).

function1 = main() { println("Hello, world!" }

function1() // "Hello, world!"
main() // "Hello, world!

function2 = (arg1, arg2) { printf("%s, %s", arg1, arg2) }
function2("cats", "dogs") // "cats, dogs"

function3(otherFunction, arg1, arg2) {
    return otherFunction(arg1, arg2)
} 

function3(function2, "cats", "dogs") // "cats, dogs"

class1 = Dog { ...class stuff... }
dog1 = class1() // Returns a new Dog instance
dog2 = Dog() // Returns a new Dog instance

class2 = { ...class stuff... } // LJ uses '{ }' for sets, so there needs to be some disambiguation here
object1 = class2()

someFactoryFunction(someClass, args) {
    return someClass(args)
}

someFactoryFunction(class1, args) // Returns a new Dog instance

Treating everything like an expression feels more clear. It might seem odd to beginners that some funny words "return" stuff while other funny words "do" stuff. Some beginners have trouble figuring out when to use print() and when to use a return for example.

@lam2mo
Copy link
Member

lam2mo commented Oct 7, 2019

Very interesting. Are there any other languages with a "default true" return value?

@Zamua
Copy link
Contributor Author

Zamua commented Oct 7, 2019

Ruby by default returns nil, and nil is a falsy value so it's similar. LJ wouldn't have truthy and falsy values (unless we feel strongly about adding that)

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

No branches or pull requests

2 participants