Skip to content

feat: conditional expression - #2

Open
m0t9 wants to merge 29 commits into
masterfrom
feature/ternary-operator
Open

feat: conditional expression#2
m0t9 wants to merge 29 commits into
masterfrom
feature/ternary-operator

Conversation

@m0t9

@m0t9 m0t9 commented Oct 21, 2025

Copy link
Copy Markdown
Owner

Conditional expression implementation in Go language. Allows conditional variables and constants short-circuited evaluation

Example

package main

import (
	"fmt"
)

const num int = 10
const s string = if num > 0 { "positive" } else { "negative" }

type Person struct {
	status string
}

func NewPerson(age int) Person {
	return Person{
		status: if age >= 18 { "adult" } else { "child" },
	}
}

func main() {
	p := NewPerson(12)
	fmt.Println(p)
}
$ GOEXPERIMENT=condexpr ./go run main.go
child

m0t9 added 8 commits October 18, 2025 23:37
- add new token "then"
- allow parsing of ternary operator
- enable typecheck
- required IR write-up
- needed to add type conversion for Then and Else parts
- constants for some reason do not work
ternary operator can be evaluated if condition is represented as
constant
Comment thread src/cmd/compile/internal/walk/order.go Outdated
@m0t9 m0t9 changed the title [WIP] feat: ternary operator [WIP] feat: ternary conditional operator Dec 28, 2025
m0t9 added 3 commits December 28, 2025 14:26
fix all except API diffs
if all the subexpressions of ternary one are constants, it can be
evaluated during compilation
@m0t9 m0t9 changed the title [WIP] feat: ternary conditional operator feat: ternary conditional operator Jan 20, 2026
@m0t9 m0t9 changed the title feat: ternary conditional operator feat: conditional expression Jan 20, 2026
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

Successfully merging this pull request may close these issues.

2 participants