www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - ElementEncodingType and immutable

reply "bioinfornatics" <bioinfornatics fedoraproject.org> writes:
hi with this code: http://www.dpaste.dzfl.pl/2f830da1
I do not understand why alias Char is equal to immutable(char)

How to fix these issues?

Thanks
Nov 21 2013
parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
On Friday, November 22, 2013 01:01:52 bioinfornatics wrote:
 hi with this code: http://www.dpaste.dzfl.pl/2f830da1
 I do not understand why alias Char is equal to immutable(char)
 
 How to fix these issues?
I'm not quite sure which line you're refering to here, but ElementEncodingType is going to give the same constness as the elements - e.g. ElementEncodingType!string is going to be immutable(char), not char. Also, you're doing something with AAs, and all keys for AAs are immutable, even if you don't explicitly mark them as immutable, so if if the type that you're using ElementEncodingType was a key in an AA, that could be your problem. However, the first error that's popping up seems to relate to the fact that ranges treat strings as ranges of dchar, not char, so sequence gives you letters which are dchar, not char, and then you try and assign it to an AA which holds immutable(char) rather than dchar (and the fact that the AA holds immutable(char) rather than char might cause further problems with being able to reassign anything in the AA - and if it doesn't it's probably due to the AA doing casting internally when it shouldn't). - Jonathan M Davis
Nov 21 2013
next sibling parent reply "bioinfornatics" <bioinfornatics feforaproject.org> writes:
On Friday, 22 November 2013 at 07:54:45 UTC, Jonathan M Davis 
wrote:
 On Friday, November 22, 2013 01:01:52 bioinfornatics wrote:
 hi with this code: http://www.dpaste.dzfl.pl/2f830da1
 I do not understand why alias Char is equal to immutable(char)
 
 How to fix these issues?
I'm not quite sure which line you're refering to here, but ElementEncodingType is going to give the same constness as the elements - e.g. ElementEncodingType!string is going to be immutable(char), not char. Also, you're doing something with AAs, and all keys for AAs are immutable, even if you don't explicitly mark them as immutable, so if if the type that you're using ElementEncodingType was a key in an AA, that could be your problem. However, the first error that's popping up seems to relate to the fact that ranges treat strings as ranges of dchar, not char, so sequence gives you letters which are dchar, not char, and then you try and assign it to an AA which holds immutable(char) rather than dchar (and the fact that the AA holds immutable(char) rather than char might cause further problems with being able to reassign anything in the AA - and if it doesn't it's probably due to the AA doing casting internally when it shouldn't). - Jonathan M Davis
Thanks Jonathan I try to have a dynamic type If sequence is immutable(char)[] AA become char[ubyte] If sequence is immutable(dchar)[] AA become dchar[ushort] …
Nov 22 2013
parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
On Friday, November 22, 2013 10:17:40 bioinfornatics wrote:
 On Friday, 22 November 2013 at 07:54:45 UTC, Jonathan M Davis
 
 wrote:
 On Friday, November 22, 2013 01:01:52 bioinfornatics wrote:
 hi with this code: http://www.dpaste.dzfl.pl/2f830da1
 I do not understand why alias Char is equal to immutable(char)
 
 How to fix these issues?
I'm not quite sure which line you're refering to here, but ElementEncodingType is going to give the same constness as the elements - e.g. ElementEncodingType!string is going to be immutable(char), not char. Also, you're doing something with AAs, and all keys for AAs are immutable, even if you don't explicitly mark them as immutable, so if if the type that you're using ElementEncodingType was a key in an AA, that could be your problem. However, the first error that's popping up seems to relate to the fact that ranges treat strings as ranges of dchar, not char, so sequence gives you letters which are dchar, not char, and then you try and assign it to an AA which holds immutable(char) rather than dchar (and the fact that the AA holds immutable(char) rather than char might cause further problems with being able to reassign anything in the AA - and if it doesn't it's probably due to the AA doing casting internally when it shouldn't). - Jonathan M Davis
Thanks Jonathan I try to have a dynamic type If sequence is immutable(char)[] AA become char[ubyte] If sequence is immutable(dchar)[] AA become dchar[ushort] …
Then you can't put sequence in a range and get its value out from there. immutable(char)[]will be treated as a range of dchar, so you'll get dchars, not chars. If you want to operate on a string as chars, then don't use it as a range. The closest that you could get to operating on it as a range of chars would be to use std.string.representation and operate on it as immutable(ubyte)[]. - Jonathan M Davis
Nov 22 2013
parent reply "bioinfornatics" <bioinfornatics feforaproject.org> writes:
On Friday, 22 November 2013 at 10:07:12 UTC, Jonathan M Davis 
wrote:
 On Friday, November 22, 2013 10:17:40 bioinfornatics wrote:
 On Friday, 22 November 2013 at 07:54:45 UTC, Jonathan M Davis
 
 wrote:
 On Friday, November 22, 2013 01:01:52 bioinfornatics wrote:
 hi with this code: http://www.dpaste.dzfl.pl/2f830da1
 I do not understand why alias Char is equal to 
 immutable(char)
 
 How to fix these issues?
I'm not quite sure which line you're refering to here, but ElementEncodingType is going to give the same constness as the elements - e.g. ElementEncodingType!string is going to be immutable(char), not char. Also, you're doing something with AAs, and all keys for AAs are immutable, even if you don't explicitly mark them as immutable, so if if the type that you're using ElementEncodingType was a key in an AA, that could be your problem. However, the first error that's popping up seems to relate to the fact that ranges treat strings as ranges of dchar, not char, so sequence gives you letters which are dchar, not char, and then you try and assign it to an AA which holds immutable(char) rather than dchar (and the fact that the AA holds immutable(char) rather than char might cause further problems with being able to reassign anything in the AA - and if it doesn't it's probably due to the AA doing casting internally when it shouldn't). - Jonathan M Davis
Thanks Jonathan I try to have a dynamic type If sequence is immutable(char)[] AA become char[ubyte] If sequence is immutable(dchar)[] AA become dchar[ushort] …
Then you can't put sequence in a range and get its value out from there. immutable(char)[]will be treated as a range of dchar, so you'll get dchars, not chars. If you want to operate on a string as chars, then don't use it as a range. The closest that you could get to operating on it as a range of chars would be to use std.string.representation and operate on it as immutable(ubyte)[]. - Jonathan M Davis
std.string.representation is great but return a numeric array instead of numeric type. Ex ubyte[] For ascii but me i try to get ubyte.
Nov 22 2013
parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Friday, November 22, 2013 12:21:23 bioinfornatics wrote:
 std.string.representation is great but return a numeric array
 instead of numeric type. Ex ubyte[]
   For ascii but me i try to get ubyte.
Yes. My point is that you have to operate on sequence as immutable(ubyte)[] instead of immutable(char)[] if you don't want range-based functions such as zip to treat it as a range of dchar. As soon as you pass a string to any range-based function that returns a new range type (rather than being able to return a portion of the original range like a funtion such as find does), then you're going to get a type which is explicitly a range of dchar and does not provide access to the underlying chars at all. It will decode them all as dchars and return those. So, when you do foreach( letter, i; zip(sequence, values) ) letter is going to be dchar. If instead you did foreach( letter, i; zip(sequence.representation, values) ) then it would be ubyte, which you could then cast to char if you wanted to, but if you pass sequence directly to zip, then you're going to be iterating over dchars and not chars. Normally, that's what you want, as that's more correct with regards to Unicode, but if you really want to operate on the individual chars as you seem to, then either you can't use any range-based functions, or you're going to need to use std.string.representation to convert the string to an array of the integral type that's the same size as the character type and then iterate over that instead of char or wchar. All range-based functions are just going to treat strings as ranges of dchar and give you dchar when you iterate over them. - Jonathan M Davis
Nov 22 2013
prev sibling parent "bioinfornatics" <bioinfornatics feforaproject.org> writes:
On Friday, 22 November 2013 at 07:54:45 UTC, Jonathan M Davis
wrote:
 On Friday, November 22, 2013 01:01:52 bioinfornatics wrote:
 hi with this code: http://www.dpaste.dzfl.pl/2f830da1
 I do not understand why alias Char is equal to immutable(char)
 
 How to fix these issues?
I'm not quite sure which line you're refering to here, but ElementEncodingType is going to give the same constness as the elements - e.g. ElementEncodingType!string is going to be immutable(char), not char. Also, you're doing something with AAs, and all keys for AAs are immutable, even if you don't explicitly mark them as immutable, so if if the type that you're using ElementEncodingType was a key in an AA, that could be your problem. However, the first error that's popping up seems to relate to the fact that ranges treat strings as ranges of dchar, not char, so sequence gives you letters which are dchar, not char, and then you try and assign it to an AA which holds immutable(char) rather than dchar (and the fact that the AA holds immutable(char) rather than char might cause further problems with being able to reassign anything in the AA - and if it doesn't it's probably due to the AA doing casting internally when it shouldn't). - Jonathan M Davis
Thanks Jonathan I try to have a dynamic type If sequence is immutable(char)[] AA become char[ubyte] If sequence is immutable(dchar)[] AA become dchar[ushort] …
Nov 22 2013