[swift-evolution] [Pitch] Improve String Literals
Tony Allevato
tony.allevato at gmail.com
Tue May 16 16:11:40 CDT 2017
On Tue, May 16, 2017 at 1:47 PM Ben Rimmington <me at benrimmington.com> wrote:
>
> > On 16 May 2017, at 21:27, Tony Allevato <tony.allevato at gmail.com> wrote:
> >
> >> On Tue, May 16, 2017 at 1:25 PM David Hart <david at hartbit.com> wrote:
> >> The problem I see is that + is an operator of the Standard Library and
> not part of the core language. I wouldn’t want + to sometimes to be a
> runtime operation and other times a compile-time operation. No, I really
> think we need strong language support here.
> >
> > Why not? Compile-time concatenation of string literals delimited by `+`
> sounds like a perfectly reasonable compiler optimization.
> >
> > Your argument would also state that the compiler should not do constant
> folding like turning `5 + 7` into `12`, would it not? What makes that
> situation different?
>
> When does the compiler do constant folding?
>
> Welcome to Apple Swift version 3.1 (swiftlang-802.0.53
> clang-802.0.42). Type :help for assistance.
> 1> import Foundation
> 2> let x: NSNumber = 12
> x: __NSCFNumber = Int64(12)
> 3> let y: NSNumber = 5 + 7
> error: repl.swift:3:21: error: cannot convert value of type 'Int'
> to specified type 'NSNumber'
> let y: NSNumber = 5 + 7
> ~~^~~
> NSNumber( )
>
> -- Ben
>
$ swiftc -emit-sil - <<EOF
let x = 5 + 7
EOF
// main
sil @main : $@convention(c) (Int32,
UnsafeMutablePointer<Optional<UnsafeMutablePointer<Int8>>>) -> Int32 {
bb0(%0 : $Int32, %1 :
$UnsafeMutablePointer<Optional<UnsafeMutablePointer<Int8>>>):
alloc_global @_Tv4main1xSi, loc "<stdin>":1:5, scope 1 // id: %2
%3 = global_addr @_Tv4main1xSi : $*Int, loc "<stdin>":1:5, scope 1 //
user: %6
* %4 = integer_literal $Builtin.Int64, 12, loc "<stdin>":1:11, scope 1 //
user: %5*
%5 = struct $Int (%4 : $Builtin.Int64), loc "<stdin>":1:11, scope 1 //
user: %6
store %5 to %3 : $*Int, loc "<stdin>":1:11, scope 1 // id: %6
%7 = integer_literal $Builtin.Int32, 0, scope 1 // user: %8
%8 = struct $Int32 (%7 : $Builtin.Int32), scope 1 // user: %9
return %8 : $Int32, scope 1 // id: %9
} // end sil function 'main'
The resulting SIL is the run-time vs. compile-time distinction that I was
addressing; if someone is concerned about `+` being different for strings,
then that SIL shouldn't be allowed either.
The results you show for NSNumber are honestly surprising to me, but the
intricacies of Obj-C bridging are definitely one of the areas of Swift that
I don't understand deeply.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170516/e60116f0/attachment.html>
More information about the swift-evolution
mailing list