Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save spytheman/3aaf3a3a1126f3585a853e9aed9eb021 to your computer and use it in GitHub Desktop.

Select an option

Save spytheman/3aaf3a3a1126f3585a853e9aed9eb021 to your computer and use it in GitHub Desktop.
type SuccessCode int
type FailureCode int
type Code = SuccessCode | FailureCode
fn (x SuccessCode) hex() string { return 'my hex: ' + int(x).hex() }
//
fn (x SuccessCode) str() string { return 'SC(' + int(x).str() +')' }
fn (x FailureCode) str() string { return 'FC(' + int(x).str() +')' }
fn (x Code) str() string {
mut res := ''
match x {
SuccessCode { res = x.str() }
FailureCode { res = x.str() }
}
return 'Code( ' +typeof(x)+ ' : $res )'
}
fn main() {
s := Code( SuccessCode(1) )
f := Code( FailureCode(2) )
println('calling hex for SC: ' + SuccessCode(1023).hex())
println('calling hex for FC: ' + FailureCode(1023).hex())
all := [s,f]
for x in all {
println('x: $x')
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment