Skip to content

Commit

Permalink
introduce type defences (#1306)
Browse files Browse the repository at this point in the history
  • Loading branch information
konnov authored Dec 13, 2023
1 parent 42d0174 commit aaa3b71
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
26 changes: 15 additions & 11 deletions quint/src/runtime/impl/runtimeValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -488,59 +488,63 @@ abstract class RuntimeValueBase implements RuntimeValue {
}

toSet(): Set<RuntimeValue> {
// the default transformation to a set is done via iteration
let set = Set.of<RuntimeValue>()
for (const e of this) {
set = set.add(e.normalForm())
if (this.isSetLike) {
// the default transformation to a set is done via iteration
let set = Set.of<RuntimeValue>()
for (const e of this) {
set = set.add(e.normalForm())
}
return set
} else {
throw new Error('Expected a set-like value')
}
return set
}

toList(): List<RuntimeValue> {
if (this instanceof RuntimeValueTupleOrList) {
return this.list
} else {
return List()
throw new Error('Expected a list value')
}
}

toOrderedMap(): OrderedMap<string, RuntimeValue> {
if (this instanceof RuntimeValueRecord) {
return this.map
} else {
return OrderedMap()
throw new Error('Expected a record value')
}
}

toMap(): Map<RuntimeValue, RuntimeValue> {
if (this instanceof RuntimeValueMap) {
return this.map
} else {
return Map()
throw new Error('Expected a map value')
}
}

toBool(): boolean {
if (this instanceof RuntimeValueBool) {
return (this as RuntimeValueBool).value
} else {
return false
throw new Error('Expected a Boolean value')
}
}

toInt(): bigint {
if (this instanceof RuntimeValueInt) {
return (this as RuntimeValueInt).value
} else {
return 0n
throw new Error('Expected an integer value')
}
}

toStr(): string {
if (this instanceof RuntimeValueStr) {
return (this as RuntimeValueStr).value
} else {
return ''
throw new Error('Expected a string value')
}
}

Expand Down
6 changes: 5 additions & 1 deletion quint/test/repl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,11 @@ describe('repl ok', () => {
|^^^^^^^^^
|
|
|1
|runtime error: error: [QNT501] Expected an integer value
|1 + false
|^^^^^^^^^
|
|<undefined value>
|>>> `
)
await assertRepl(input, output)
Expand Down

0 comments on commit aaa3b71

Please sign in to comment.