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

function to check type #95

Closed
stdweird opened this issue Aug 25, 2015 · 12 comments
Closed

function to check type #95

stdweird opened this issue Aug 25, 2015 · 12 comments
Assignees

Comments

@stdweird
Copy link
Member

currently we have a bunch of code that does things like

function is_type_x = {
    if(something(ARGV[0]){ 
       return(false)
   }
   true;
}

type type_x = string with is_type_x(SELF);

this is typically used to make new types out of sub-types like

type type_xyz = string with {
    if(is_type_x(SELF)) return(true);
    ....
}

it would make a lot of sense to be able to test the type directly like

type type_xyz = string with {
    if(is_type(type_x, SELF)) return(true);
    ....
}
@stdweird
Copy link
Member Author

@loomis forgot to ask: do you this is feasible?

@loomis
Copy link

loomis commented Mar 22, 2016

Yes, it is feasible. Probably fairly straightforward to implement.

@stdweird
Copy link
Member Author

to follow the internal code, and not cause any confustion, we settled on the function name validate to test a variable or value using the type validation code.

(is_type might suggest that you test if a variable (or path) is of a certain non-trivial type. this makes sense for the base types, but those already have dedicated functions like is_string. but not for variables, as you cannot bind variables to a type).

@ned21
Copy link
Contributor

ned21 commented Jul 26, 2016

The problem with validate() is discovery -- how does someone know a particular function exists? If the only reason not to use is_type() is because someone might try to apply it to a variable then surely that is best handled in the documentation?

@stdweird
Copy link
Member Author

@ned21 is_type is confusing because it does not check the type. maybe i should rename this issue to function to test value/variable using type validation? (if this makes no sense, then i didn't understand your question 😄 )

@ned21
Copy link
Contributor

ned21 commented Jul 26, 2016

Yep, I think I am confused as to what this does. Can you provide a real code example instead of a generic one? Then I might understand it better. :(

@stdweird
Copy link
Member Author

i see 2 main use cases:

type type_ip = string with validate(type_ipv4, SELF) || validate(type_ipv6, SELF);

this would get rid of a bunch of functions that are only used because you currently cannot easily combine types (without refactoring or factroing out the validation code).

  • other use case would be to use existing types in control structures
    e.g. assume type exists
type configured_nic = string with exists("/system/network/interfaces/" + SELF);

and then you can do things like

"/software/component/magic/ignore" = {
    foreach(nic;data;value("/harwdare/cards/nic")) {
       if(! validate(configured_nic, nic)) {
           append(nic);
       };
    };
};

and yes, you can define test functions that only return true/false for this and make more type x = string with test_function_x(SELF), but why not define the type and reuse the validation code?

@ned21
Copy link
Contributor

ned21 commented Jul 26, 2016

The examples in the docs for the PR help explain this better. I can see why is_type is wrong since you are comparing (, ), to make sure validates as that type.

Validate remains a vague and overly general term which is very close to the existing valid statement. How about valid_type(type, value) ? I suppose that makes it sounds like checking type exists. Or apply_type(type, value) ? If we think of type as a function-like thing, that is effectively what is occurring here.

@stdweird
Copy link
Member Author

@ned21 the pan book has the following wrt bind "If any of the content does not conform to the specified types, then an error will occur during the compilation.". maybe conform as function name? or is_conform, or conforms?
i'm bad with names, not proposing anything more 😄

@iliclaey feel free to make even more suggestions

@ned21
Copy link
Contributor

ned21 commented Jul 26, 2016

+1 for accuracy of conform(type, element). It's not very discoverable if someone is grepping the book for type related functions but it ticks all the other boxes.

@jouvin
Copy link
Contributor

jouvin commented Jul 26, 2016

What about is_valid(element, type)? At the end, this is to assess if a particular value is valid for a given type, if I followed correctly.

@iliclaey
Copy link
Contributor

I think is_valid most clearly conveys what this function does

@jrha jrha closed this as completed in #123 Nov 24, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

6 participants