Ambiguous error for setting a value in a CChar array


(Adam Campbell) #1

Discovered this when trying to answer a stackoverflow question about
setting character values in a CCString array.

The following code produces an ambiguous error:
var buff = [CChar](count: 5, repeatedValue: 0)
buff[0] = "A".utf8.first!.value // Error - Cannot assign value of type
'Int8' to type 'CChar' (aka 'Int8')

I can easily get around this by changing the second line to:
buff[0].value = "A".utf8.first!.value

Can someone explain what is happening here? As well as the significance of
Int8.value?

Thanks in advance,
Adam


(Dmitri Gribenko) #2

The 'var value' property used to be a property of type Builtin.Int8,
which maps directly to LLVM's i8. This is a part of the integers
implementation, and it was renamed to 'var _value' a while ago.
Please don't use this property. The code that you posted (that
accesses '.value') does not compile anymore.

Dmitri

···

On Thu, Mar 3, 2016 at 7:54 PM, Adam Campbell via swift-users <swift-users@swift.org> wrote:

Discovered this when trying to answer a stackoverflow question about setting
character values in a CCString array.

The following code produces an ambiguous error:
var buff = [CChar](count: 5, repeatedValue: 0)
buff[0] = "A".utf8.first!.value // Error - Cannot assign value of type
'Int8' to type 'CChar' (aka 'Int8')

I can easily get around this by changing the second line to:
buff[0].value = "A".utf8.first!.value

Can someone explain what is happening here? As well as the significance of
Int8.value?

--
main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if
(j){printf("%d\n",i);}}} /*Dmitri Gribenko <gribozavr@gmail.com>*/


(Adam Campbell) #3

Thanks Dmitri,

Does the first statements I posted without .value compile at this time?
Or give a better error than I was receiving?

Adam

···

On Fri, Mar 4, 2016 at 3:46 PM Dmitri Gribenko <gribozavr@gmail.com> wrote:

On Thu, Mar 3, 2016 at 7:54 PM, Adam Campbell via swift-users > <swift-users@swift.org> wrote:
> Discovered this when trying to answer a stackoverflow question about
setting
> character values in a CCString array.
>
> The following code produces an ambiguous error:
> var buff = [CChar](count: 5, repeatedValue: 0)
> buff[0] = "A".utf8.first!.value // Error - Cannot assign value of type
> 'Int8' to type 'CChar' (aka 'Int8')
>
> I can easily get around this by changing the second line to:
> buff[0].value = "A".utf8.first!.value
>
> Can someone explain what is happening here? As well as the significance
of
> Int8.value?

The 'var value' property used to be a property of type Builtin.Int8,
which maps directly to LLVM's i8. This is a part of the integers
implementation, and it was renamed to 'var _value' a while ago.
Please don't use this property. The code that you posted (that
accesses '.value') does not compile anymore.

Dmitri

--
main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if
(j){printf("%d\n",i);}}} /*Dmitri Gribenko <gribozavr@gmail.com>*/


(Dmitri Gribenko) #4

This compiles:

var buff = [UInt8](repeating: 0, count: 5)
buff[0] = "A".utf8.first!

Dmitri

···

On Thu, Mar 3, 2016 at 8:50 PM, Adam Campbell <adam.92.ac@gmail.com> wrote:

Thanks Dmitri,

Does the first statements I posted without .value compile at this time?
Or give a better error than I was receiving?

--
main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if
(j){printf("%d\n",i);}}} /*Dmitri Gribenko <gribozavr@gmail.com>*/