php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #80697 Return type declaration int allows floats to be returned
Submitted: 2021-02-02 12:38 UTC Modified: 2021-02-02 18:15 UTC
From: david dot m dot wakelin at gmail dot com Assigned:
Status: Not a bug Package: *Programming Data Structures
PHP Version: 8.0.1 OS: Ubuntu 18.04.5 LTS
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: david dot m dot wakelin at gmail dot com
New email:
PHP Version: OS:

 

 [2021-02-02 12:38 UTC] david dot m dot wakelin at gmail dot com
Description:
------------
When specifying int as a return type declaration, a type of float is allowed to be returned, which gets cast to an int.

All other type coercions preserve the meaning of the returned data, whereas coercing float to int will lose information after the decimal, therefore not returning the true meaning.

This is not a good feature in my opinion and has been the cause of a couple of uncaught bugs for me.

I suggest that a float to int should be non-coercible in return types and should raise a TypeError as standard and not just when using strict_types.

Test script:
---------------
class Test
{
    public static function getInt(): int
    {
        return 5.67;
    }
}

Test::getInt(); // 5 but would like to see a TypeError

Expected result:
----------------
getInt returns a 5, but I think it should raise a TypeError


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-02-02 12:45 UTC] rtrtrtrtrt at dfdfdfdf dot dfd35
> and not just when using strict_types

nosense - that's exactly what strict_types is for
without it#s implicit casting as desigend and documented
 [2021-02-02 13:04 UTC] sdsdsdsdsd at dfgfgfgfg dot net
in other words:

a) if you want strict_types hebavior enable strict_types
b) if you can't enable it because of code-mess fix your codebase

in weak mode your code literally means and that is how it's supposed to be and your code is in fact written

public static function getInt(): int
{
 return (int)5.67;
}
 [2021-02-02 17:18 UTC] david dot m dot wakelin at gmail dot com
-Status: Open +Status: Closed
 [2021-02-02 17:18 UTC] david dot m dot wakelin at gmail dot com
I'm well aware of how this works, I know it's fully documented and I know why it works that way.

I'm not asking for help here. I'm starting a discussion on the possibility of changing the current behavior as I believe it can function in a more robust manner.

However, after some more thought, I don't think this is viable, so I am closing this report.
 [2021-02-02 17:25 UTC] rtrtrtrtrt at dfdfdfdf dot dfd35
> believe it can function in a more robust manner

consistency matters!

adjust behavior here and there because it may make sense for badly written code leads to a total mess
 [2021-02-02 18:15 UTC] requinix@php.net
-Status: Closed +Status: Not a bug
 [2021-02-02 18:15 UTC] requinix@php.net
> I'm starting a discussion on the possibility of changing the current behavior
The current behavior was heavily discussed. As mentioned, what you're looking for is strict_types.
https://wiki.php.net/rfc/scalar_type_hints_v5
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Dec 05 23:01:30 2024 UTC