Hi there.
Next code involving C bindings for Hunspell library.
@[Link("hunspell")]
lib LibHunspell
type Hunhandle = Void*
fun create = Hunspell_create(affpath : LibC::Char*, dpath : LibC::Char*) : Hunhandle
fun destroy = Hunspell_destroy(p_hunspell : Hunhandle)
fun spell = Hunspell_spell(p_hunspell : Hunhandle, x1 : LibC::Char*) : LibC::Int
end
class Hunspell
@handle : LibHunspell::Hunhandle
def initialize(affpath : String, dpath : String)
@closed = false
@handle = LibHunspell.create(affpath, dpath) # => Pointer(Void)
end
def spellcheck(word : String) : Bool
LibHunspell.spell(@handle, word) != 0
end
def finalize
puts "__finalize__"
close
end
def close
LibHunspell.destroy(@handle) unless @closed
@closed = true
end
end
loop do
hunspell = Hunspell.new("/usr/share/hunspell/en_US.aff", "/usr/share/hunspell/en_US.dic")
hunspell.spellcheck("d0g")
# hunspell.close
end
You can notice finalize
method is never called.
Of course, I can call the close
method manually so this is not an issue at all for me.
I’m just curious does Crystal have a natural way to do this automatically?
Thanks for any advice.