Re: Issue with template function

2015-02-07 Thread Nicholas Wilson via Digitalmars-d-learn
The original code I was using was written in Java, and only had a method for strings. This is closer to what I wanted. My unit tests were just going back and forth with readString function, so I was completely missing this for other types. Nice catch! There were a couple issues with your cod

Re: Issue with template function

2015-02-07 Thread Charles via Digitalmars-d-learn
On Saturday, 7 February 2015 at 12:04:12 UTC, Nicholas Wilson wrote: Are you wanting to to convert each element in arr to a byte thus truncating and losing data (when T.sizeof != 1)? as in toBytes([1,2,3, 42, 500 /*this will be truncated to 244 */]);// T == int here or are you wanting to c

Re: Issue with template function

2015-02-07 Thread Nicholas Wilson via Digitalmars-d-learn
On Friday, 6 February 2015 at 17:09:29 UTC, Charles wrote: I'm trying to create a template function that can take in any type of array and convert it to a ubyte array. I'm not concerned with endianness at the moment, but I ran into a roadblock when trying to do this with strings. It already wor

Re: Issue with template function

2015-02-07 Thread Charles via Digitalmars-d-learn
On Friday, 6 February 2015 at 17:40:31 UTC, ketmar wrote: On Fri, 06 Feb 2015 17:09:28 +, Charles wrote: readString(toBytes!string("test"),0,4).writeln; if you'll take a look into druntime sources, you'll find that string is just an alias to `immutable(char)[]`. so you actually

Re: Issue with template function

2015-02-06 Thread ketmar via Digitalmars-d-learn
On Fri, 06 Feb 2015 17:09:28 +, Charles wrote: > readString(toBytes!string("test"),0,4).writeln; if you'll take a look into druntime sources, you'll find that string is just an alias to `immutable(char)[]`. so you actually doing thing: readString(toBytes!(immutable(char)[])("test

Re: Issue with template function

2015-02-06 Thread FG via Digitalmars-d-learn
On 2015-02-06 at 18:09, Charles wrote: readString(toBytes!char(['t','e','s','t']),0,4).writeln; readString(toBytes!string("test"),0,4).writeln;// This is line 39 That second line makes no sense (you didn't provide an array of strings). Why toBytes!string("test") and not to

Re: Issue with template function

2015-02-06 Thread Charles via Digitalmars-d-learn
Can I not do this cast because it's immutable?

Issue with template function

2015-02-06 Thread Charles via Digitalmars-d-learn
I'm trying to create a template function that can take in any type of array and convert it to a ubyte array. I'm not concerned with endianness at the moment, but I ran into a roadblock when trying to do this with strings. It already works with ints, chars, etc. Here's the relevant test code: