Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Instanceof feature for protocol's #128

Closed
griffith1deady opened this issue Feb 26, 2024 · 2 comments · Fixed by #129
Closed

Instanceof feature for protocol's #128

griffith1deady opened this issue Feb 26, 2024 · 2 comments · Fixed by #129

Comments

@griffith1deady
Copy link

griffith1deady commented Feb 26, 2024

how do I know that my object can be converted to my interface via toProtocol, as languages usually implement instanceof?
and nice library, thanks for your hard work!

import oolib

type 
    TileEntity {.inheritable, protocoled.} = tuple
     sout: proc(data: string)

class MyTileEntity impl TileEntity:
    proc sout(data: string) = echo(data)

let myTile = MyTileEntity.new()
echo myTile is TileEntity #false
@griffith1deady
Copy link
Author

griffith1deady commented Feb 26, 2024

as my workaround about it:

import oolib

type 
    TileEntity {.inheritable, protocoled.} = tuple
     sout: proc(data: string)

class MyTileEntity impl TileEntity:
    proc sout(data: string) = echo(data)

proc `is`[T](val: T, dest: typedesc): bool =
    result = when compiles(val.toProtocol): getTypeInfo(typeof(val.toProtocol).default) == getTypeInfo(dest.default) else: false

let myTile = MyTileEntity.new()
echo myTile is TileEntity #true
echo myTile is string #false

@glassesneo
Copy link
Owner

There is no procedures like instanceof for now, but your idea is so good I will start to implement it soon. Also, thank you for using my library:D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants