JMU-CS / JMU-CS/less-java

Remove void "type"; convert statements to expressions

Offen
#56 2 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
proposal
Vorherrschende Sprache
Java
Sterne
6
Forks
7
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

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](https://en.wikipedia.org/wiki/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.

Beitragsleitfaden

Für dieses Repository ist kein Beitragsleitfaden indexiert

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.