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

How can I concat double types and string types? #1893

Closed
josephkang619 opened this issue Jul 29, 2019 · 9 comments
Closed

How can I concat double types and string types? #1893

josephkang619 opened this issue Jul 29, 2019 · 9 comments
Assignees
Labels

Comments

@josephkang619
Copy link

Hi. I tried to make SQL as follows.

public static function getQuery(var value)
{
    var query;

    var min, max;

    let min = value / 100 * 25;
    let max = value / 100 * 50;

    let query .= "SELECT *
                    FROM TEST
                   WHERE value <= ".max;

    let query .= "   AND value >= ".min;

    return query;
}

However, a compilation error occurred.

[ERROR] Variable type: double cannot be used in concat operation in...

So I tried to fix it.

public static function getQuery(var value)
{
    var query;

    var min, max;

    let min = value / 100 * 25;
    let max = value / 100 * 50;

    let query .= "SELECT *
                    FROM TEST
                   WHERE value <= ".(string) max;

    let query .= "   AND value >= ".(string) min;

    return query;
}

The results are as follows.

[ERROR] Internal extension compilation failed. Check compile-errors.log for more information.

Part of the compile-errors.log file.

/home/kini/zephir_work/foodhelper/ext/utinfra/helper/foodsuggesthelper.zep.c:96:2: warning: passing argument 1 of 'zval_get_type' from incompatible pointer type [enabled by default]
zephir_get_strval(&_0, &max);
^
In file included from /usr/include/php/Zend/zend.h:29:0,
from /usr/include/php/main/php.h:35,
from /home/kini/zephir_work/foodhelper/ext/utinfra/helper/foodsuggesthelper.zep.c:6:
/usr/include/php/Zend/zend_types.h:389:38: note: expected 'const struct zval ' but argument is of type 'double '
static zend_always_inline zend_uchar zval_get_type(const zval
pz) {
^
/usr/include/php/Zend/zend_types.h:402:36: error: request for member 'u1' in something not a structure or union
#define Z_TYPE_FLAGS(zval) (zval).u1.v.type_flags
^
/usr/include/php/Zend/zend_types.h:532:32: note: in expansion of macro 'Z_TYPE_FLAGS'
#define Z_REFCOUNTED(zval) ((Z_TYPE_FLAGS(zval) & IS_TYPE_REFCOUNTED) != 0)
^
/usr/include/php/Zend/zend_types.h:533:33: note: in expansion of macro 'Z_REFCOUNTED'
#define Z_REFCOUNTED_P(zval_p) Z_REFCOUNTED(
(zval_p))
^
/usr/include/php/Zend/zend_types.h:866:6: note: in expansion of macro 'Z_REFCOUNTED_P'
if (Z_REFCOUNTED_P((pz))) {
^
./kernel/memory.h:97:2: note: in expansion of macro 'Z_TRY_ADDREF_P'
Z_TRY_ADDREF_P(v);
^
./kernel/operators.h:210:4: note: in expansion of macro 'ZEPHIR_CPY_WRT'
ZEPHIR_CPY_WRT(left, right);
^
/home/kini/zephir_work/foodhelper/ext/utinfra/helper/foodsuggesthelper.zep.c:96:2: note: in expansion of macro 'zephir_get_strval'
zephir_get_strval(&_0, &max);

I've tried a lot of other things, but I can not find a solution and I'm asking for help.

Thanks for reading.

@sergeyklay
Copy link
Contributor

Thank you for the report. I'll take a look ASAP.

@juan-morales
Copy link

I got same result.

Zephir 0.12.1 by Andres Gutierrez and Serghei Iakovlev (25cf1fd)

Linux Mint:
Linux fachamix 4.10.0-38-generic #42~16.04.1-Ubuntu SMP Tue Oct 10 16:32:20 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux

@juan-morales
Copy link

juan-morales commented Jul 31, 2019

Just in case ... no tests were created for this scenario neither.
Concatenation with INT works perfect by the way.

@juan-morales
Copy link

juan-morales commented Jul 31, 2019

Eventhough it is an issue .... If its really urgent ... please check this workaround.

namespace Test;

class Test
{
    public static function saludar()
    {
        var number, mystring, myconcat;
        let number = 12.5;
        let mystring = "TOTAL IS ";
        let myconcat = sprintf("%s%g", mystring, number);
        echo myconcat;
    }
}

@juan-morales
Copy link

another workaround (in case of urgency)

namespace Test;

class Test
{
    public static function saludar()
    {
        var number, mystring, myconcat;
        let number = 12.5;
        let number .= "";
        let mystring = "TOTAL IS ";
        let myconcat = mystring . number;
        echo myconcat;
    }
}

@danhunsaker
Copy link
Contributor

I'd probably go with this simpler variant:

namespace Test;

class Test
{
    public static function saludar()
    {
        var number;
        let number = 12.5;
        echo sprintf("TOTAL IS %g", number);
    }
}

Use whatever variables you need to have, of course, but avoid creating ones you don't. And while we should certainly support concatenating multiple types, sprintf() is certainly the recommended approach in the meantime, or when your concatenation needs some precision in presentation.

@josephkang619
Copy link
Author

Thank you for all your answers. I solved the problem of juancarmo's writing.

@juan-morales
Copy link

Any updates on this?

@sergeyklay
Copy link
Contributor

Fixed in the development branch. Feel free to open a new issue if the problem appears again. Thank you for contributing.

dreamsxin pushed a commit to dreamsxin/zephir that referenced this issue Nov 6, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants