zephir icon indicating copy to clipboard operation
zephir copied to clipboard

Add abiilty to check for types using the fetch operator

Open dschissler opened this issue 6 years ago • 4 comments

Currently we can do the following:

if fetch blah, options["blah"] {
}

I'm thinking that it would be great to be able to type the fetch like this:

if fetch string blah, options["blah"] {
}
if fetch <Blah> blah, options["blah"] {
}

If more fine grained control was needed then just don't use the types. Sometimes I won't want to check the existence first to throw an exception, then if it doesn't exist and then another check with exception if the type doesn't match. However sometimes I don't care that much and would like to do it with less code (less information).

dschissler avatar Feb 24 '19 00:02 dschissler

Like this?

if fetch blah, options["blah"] {
    if typeof blah == 'string' {
       // ...
    }
}

dreamsxin avatar Feb 24 '19 01:02 dreamsxin

Of course it can be done without new language features.

dschissler avatar Feb 24 '19 01:02 dschissler

It more looks like a pattern matching

sergeyklay avatar Feb 24 '19 11:02 sergeyklay

Already mentioned in discord - isbool isstring etc macros also can be added.

if isset X && typeof X === T currently using a lot of in param check.

ViltusVilks avatar Feb 24 '19 19:02 ViltusVilks