1

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?

Greedo
  • 4,967
  • 2
  • 30
  • 78

0 Answers0