This is an automated archive made by the Lemmit Bot.

The original was posted on /r/programminglanguages by /u/Tasty_Replacement_29 on 2024-10-17 07:10:41+00:00.


My language is supposed to be very easy to learn, C-like, fast, but memory safe. I like my language to have as little syntax as possible, but the important use cases need to be covered. One of the important (in my view) cases is this operator ? :. I think I found an alternative but would like to get feedback.

My language supports generics via templates like in C++. It also supports uniform function call syntax. For some reason (kind of by accident) it is allowed to define a function named “if”. I found that I have two nice options for the ternary operator: using an if function (like in Excel), and using a then function. So the syntax would look as follows:

C:       ?  : 
Bau/1:  if(, , )
Bau/2:  ().then(, )

Are there additional alternatives? Do you see any problems with these options, and which one do you prefer?

You can test this in the Playground:

# A generic function called 'if'
fun if(condition int, a T, b T) T
    if condition
        return a
    return b

# A generic function on integers called 'then'
# (in my language, booleans are integers, like in C)
fun int then(a T, b T) const T
    if this
        return a
    return b

# The following loop prints:
# abs(-1)= 1
# abs(0)= 0
# abs(1)= 1
for i := range(-1, 2)
    println('abs(' i ')= ' if(i < 0, -i, i))
    println('abs(' i ')= ' (i < 0).then(-i, i))

Update: Yes right now both the true and the false branch are evaluated - that means, no lazy evaluation. Lazy evaluation is very useful, specially for assertions, logging, enhanced for loops, and this here. So I think I will support “lazy evaluation” / “macro functions”. But, for this post, let’s assume both the “if” and the “then” functions use lazy evaluation :-)