vlang / rfcs

RFCs for changes to V

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

null (or none) and anonymous sumtypes

igotfr opened this issue · comments

mut i := (u16 | null)(78)

i = (u16 | null)(49)
// or
i = u16(49)

i = (u16 | null)(null)
// or
i = null

// ?u16 is equivalent to u16 | null, so the above is equivalent to:
mut i := ?u16(78)

i = ?u16(49)
// or
i = u16(49)

i = ?u16(null)
// or
i = null

// or
mut i := u16_null(78)
i = u16_null(49)
i = u16_null(null)
mut s := (string | null)('bagaço')

s = (string | null)('sebo')
// or
s = 'sebo'

s = (string | null)(null)
// or
s = null

// ?string is equivalent to string | null, so the above is equivalent to:
mut s := ?string('bagaço')
// or
mut s := ?'bagaço'

s = ?string('sebo')
// or
s = ?'sebo'
// or
s = 'sebo'

s = ?string(null)
// or
s = null

// or
mut s := string_null('bagaço')
s = string_null('sebo')
s = string_null(null)
struct User {
  name string
  password string
}

mut u := (User | null){name: 'Cleosmar', password: 'easy'}

u = (User | null){name: 'Rodinei', password: 'hard'}
// or
u = User{name: 'Rodinei', password: 'hard'}

u = (User | null){null}
// or
u = null

// ?User is equivalent to User | null, so the above is equivalent to:
mut u := ?User{name: 'Cleosmar', password: 'easy'}

u = ?User{name: 'Rodinei', password: 'hard'}
// or
u = User{name: 'Rodinei', password: 'hard'}

u = ?User{null}
// or
u = null

The most sound solution would be:

noreturn / Void : has no possible value
none / Unit : has 1 possible value