Hexadecimal floating-point literals


(Toni Suter) #1

Hi,

I have a question regarding hexadecimal floating-point literals. According to the Lexical Structure (https://developer.apple.com/library/prerelease/content/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html)
it is not possible to have a hex floating-point literal without the exponent. At first I thought this makes sense.
How else would the lexer / parser know if 0x123.beef is a hex floating-point literal or a hex integer literal with a property 'beef'?
However, if I define such a property on Int, it doesn’t work:

extension Int {
    var beef: Int {
        return 42
    }
}

print(12.beef) // works
print(0b1001.beef) // works
print(0o77.beef) // works
print(0xabc.beef) // error: hexadecimal floating point literal must end with an exponent

Is this just to avoid confusion for the programmer? Or is there some other reason?

Thanks and best regards,
Toni


(Zhao Xin) #2

I think it avoids the confusion. You can use print((0xabc).beef) instead.

Zhaoxin

···

On Sun, Jun 26, 2016 at 3:50 PM, Toni Suter via swift-users < swift-users@swift.org> wrote:

Hi,

I have a question regarding hexadecimal floating-point literals. According
to the Lexical Structure (
https://developer.apple.com/library/prerelease/content/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html
)
it is not possible to have a hex floating-point literal without the
exponent. At first I thought this makes sense.
How else would the lexer / parser know if 0x123.beef is a hex
floating-point literal or a hex integer literal with a property 'beef'?
However, if I define such a property on Int, it doesn’t work:

extension Int {
    var beef: Int {
        return 42
    }
}

print(12.beef) // works
print(0b1001.beef) // works
print(0o77.beef) // works
print(0xabc.beef) // error: hexadecimal floating point literal must end
with an exponent

Is this just to avoid confusion for the programmer? Or is there some other
reason?

Thanks and best regards,
Toni

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Joe Groff) #3

The lexer failed to backtrack if there wasn't a valid significand and exponent, but that was recently fixed, so this should work now. See https://github.com/apple/swift/pull/3124 .

-Joe

···

On Jun 26, 2016, at 12:50 AM, Toni Suter via swift-users <swift-users@swift.org> wrote:

Hi,

I have a question regarding hexadecimal floating-point literals. According to the Lexical Structure (https://developer.apple.com/library/prerelease/content/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html)
it is not possible to have a hex floating-point literal without the exponent. At first I thought this makes sense.
How else would the lexer / parser know if 0x123.beef is a hex floating-point literal or a hex integer literal with a property 'beef'?
However, if I define such a property on Int, it doesn’t work:

extension Int {
    var beef: Int {
        return 42
    }
}

print(12.beef) // works
print(0b1001.beef) // works
print(0o77.beef) // works
print(0xabc.beef) // error: hexadecimal floating point literal must end with an exponent

Is this just to avoid confusion for the programmer? Or is there some other reason?


(Toni Suter) #4

Ok, thanks for the hint!

Toni

···

On 26 Jun 2016, at 11:49, zh ao <owenzx@gmail.com> wrote:

I think it avoids the confusion. You can use print((0xabc).beef) instead.

Zhaoxin

On Sun, Jun 26, 2016 at 3:50 PM, Toni Suter via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:
Hi,

I have a question regarding hexadecimal floating-point literals. According to the Lexical Structure (https://developer.apple.com/library/prerelease/content/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html)
it is not possible to have a hex floating-point literal without the exponent. At first I thought this makes sense.
How else would the lexer / parser know if 0x123.beef is a hex floating-point literal or a hex integer literal with a property 'beef'?
However, if I define such a property on Int, it doesn’t work:

extension Int {
    var beef: Int {
        return 42
    }
}

print(12.beef) // works
print(0b1001.beef) // works
print(0o77.beef) // works
print(0xabc.beef) // error: hexadecimal floating point literal must end with an exponent

Is this just to avoid confusion for the programmer? Or is there some other reason?

Thanks and best regards,
Toni

_______________________________________________
swift-users mailing list
swift-users@swift.org <mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users