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

int to bool conversion #117

Closed
psynetic opened this issue Nov 30, 2022 · 3 comments
Closed

int to bool conversion #117

psynetic opened this issue Nov 30, 2022 · 3 comments

Comments

@psynetic
Copy link

I get an conversion exception when using functions which are returning int in conjunction with 'and' or 'or'.

From C++11:
When any scalar value is converted to bool, the result is 0 if the value compares equal to 0; otherwise, the result is 1.

I could solve the problem by disabling CheckType for Value::GetBool().

bool Value::GetBool() const
{
//CheckType('b');
return m_val.real() == 1;
}

or

bool Value::GetBool() const
{
if (m_cType != 'b')
{
return ((bool)m_val.real()) == 1;
}

CheckType('b');
return m_val.real() == 1;

}

@beltoforion
Copy link
Owner

beltoforion commented Dec 2, 2022

I'm not striving to match C++ behavior. The test fails because the types are different. The implicit cast from int to bool and vice versa is inherently dangerous and allows for some syntax I do not want to support. Like abusing it for if-then else assignments:

(a<b) * 123 + (a>=b) *456

Your solution will work but for the time being i do not want to allow it.

@psynetic
Copy link
Author

psynetic commented Dec 3, 2022

Thanks for clarification.

But in type-strict environment func f1 which returns type mup::bool_type and the value false should throw also something like 'incomparable types: boolean and int'

with the expression:

f1()==0

In that case the return value (0) for m_Value (Value) is wrong.

@beltoforion
Copy link
Owner

The test will return false if the types differ. This may not be expected and is indeed inconsistent. I will fix it.

beltoforion pushed a commit that referenced this issue Dec 4, 2022
beltoforion pushed a commit that referenced this issue Dec 4, 2022
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

No branches or pull requests

2 participants