2022-05-31

DefaultDict in twinBASIC crashes compiler

I'm trying to make a defaultdict in twinBASIC - this is one where if a key does not exist in a dictionary then it is populated by a default value according to some type. This is what I've tried:

Private Class DefaultDict(Of T)
    Implements Scripting.Dictionary Via dict
    Private dict As Dictionary
    Private defaultValue As T

    Public Property Get Item(ByVal key As Variant) As Variant Implements Scripting.Dictionary.Item
        If Not dict.Exists(key) Then
            dict(key) = defaultValue
        End If
        Return dict(key)
    End Property
End Class

Called like:

Dim dict As New DefaultDict(Of Long)
dict("foo") += 1 'foo key defaults to 0
Debug.Print dict("foo") 'should be 1

However this just crashes the compiler. What's the proper approach here?



No comments:

Post a Comment