TodePond / DreamBerd

perfect programming language
https://dreamberd.computer
Other
11.38k stars 352 forks source link

[Feature Idea] Function assignment returns? #5

Open Magnogen opened 2 years ago

Magnogen commented 2 years ago

A cool way to do function returns, without the return keyword! (adapted from hawkw/HawkLang.md)

The idea

(Function syntax hasn't really been specified yet, so I'll just do what's showed in the booleans section)

The premise is, replace return ... with name_of_function() = ...! There are a few interesting consequences of this, but could it work? Maybe...

function fib(x) => {
    if (x <= 0) fib(x) = 0!
    if (x == 1 || x == 2) fib(x) = x!
    fib(x) = fib(x - 1) + fib(x - 2)!
}

This also has the wonderful consequence of having to rename all of your return statements if you rename the function!

An interesting way to extend this is by using conditions in the argument slots for implicit ifs!

This code does exactly the same thing as the one above, but makes the programmer think of it in a different way!

function fib(x) => {
    fib(x <= 0) = 0!
    fib(x == 1 || x == 2) = x!
    fib(x) = fib(x - 1) + fib(x - 2)!
}

I'm not sure what would happen if another function is used for the return statement, maybe it'll return a value from that one, if it's nested in it? Idk that'd be pretty weird... Maybe a bit too weird even for berds!!1!

function a() => {
    b()!
    a() = "I dont run?!"!
}
function b() => {
    a() = "I do!"!
    "but then does this output?"?
    b() = "returned from b()"!
}
a()?
// but what would happen with 
b()?
// an error?
// printing "but then does this output?" and then outputting "returned from b()"?
// would it depend on if `b()` was called by `a()`?
// side note: github syntax highlight really doesnt like using ! instead of ; lol

I enjoyed doing this little (maybe not so little) write-up! What do you think?

TodePond commented 2 years ago

Very interesting. This perhaps goes to dangerous and brave heights not even known before to DreamBerd. But you make a good point: goto is evil bad practice, and return is just another type of goto! Look at this:

function fizzbuzz(n) => {
   if (n % 3 = 0) return "Fizz"!
   if (n % 3 = 0) return "Buzz"!
   if (n % 3 = 0 & n % 5 = 0) return "FizzBuzz"! // BUG: This line never runs...
   return n!
}

Maybe return could be a variable that gets assigned in every function body. FIXED:

function fizzbuzz(n) => {
   return = n!
   if (n % 3 = 0) return = "Fizz"!
   if (n % 3 = 0) return = "Buzz"!
   if (n % 3 = 0 & n % 5 = 0) return = "FizzBuzz"!
}

PS: remember to indent with 3 spaces when writing DreamBerd :)