March 29, 2013 Re: bearophile can say "i told you so" (re uint->int implicit conv) | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On Thursday, 28 March 2013 at 21:58:05 UTC, bearophile wrote:
> I didn't write a Bugzilla request to remove the implicit uint==>int assignment. (I think the signed-unsigned comparisons are more dangerous than those signed-unsigned assignments. But maybe too is a problem with no solution).
Oh maybe I got it mixed up, but I definitely remember talking about signed/unsigned something with you before!
|
March 29, 2013 Re: bearophile can say "i told you so" (re uint->int implicit conv) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | On Friday, March 29, 2013 02:19:49 Adam D. Ruppe wrote:
> On Friday, 29 March 2013 at 01:18:03 UTC, Jonathan M Davis wrote:
> > It's not terribly pretty, but you can always do this
>
> We could also do more C++ looking:
>
> unsigned_cast!foo or IFTI or whatever;
It would be pretty trivial to add a wrapper function to make it cleaner. I was just pointing out that we already provided a way to cast to an unsigned type of the same size without needing to add anything to the language.
- Jonathan M Davis
|
March 29, 2013 Re: bearophile can say "i told you so" (re uint->int implicit conv) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Timon Gehr | On Thursday, 28 March 2013 at 22:04:36 UTC, Timon Gehr wrote:
> - Copypasta & edit instead of string mixins / static foreach.
Part of why I did it this way was the annoyance that I can't do a variadic template in an interface. I'd REALLY prefer to do it that way so there wouldn't be a list of types at all - just plain to!string(foo).
The actual line in the program is a little longer too more like this:
if(arg == typeid(string) || arg == typeid(immutable(string)) || arg == typeid(const(string)))
It annoyed me that there's so many different typeids even though it really doesn't matter for me here. But oh well, I got this code to a point where it works (with a few practices I keep in mind) and now I generally don't think about it anymore.
|
March 29, 2013 Re: bearophile can say "i told you so" (re uint->int implicit conv) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan M Davis | On Friday, 29 March 2013 at 01:18:03 UTC, Jonathan M Davis wrote:
> On Thursday, March 28, 2013 15:11:02 H. S. Teoh wrote:
>> Maybe it's time to introduce cast(signed) or cast(unsigned) to the
>> language, as bearophile suggests?
>
> It's not terribly pretty, but you can always do this
>
> auto foo = cast(Unsigned!(typeof(var))var;
>
> or
>
> auto bar = to!(Unsigned!(typeof(var)))(var);
>
> - Jonathan M Davis
short signed(ushort n){ return cast(short)n; }
int signed(uint n){ return cast(int)n; }
long signed(ulong n){ return cast(long)n; }
int n = va_arg!uint(_argptr).signed;
|
March 29, 2013 Re: bearophile can say "i told you so" (re uint->int implicit conv) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan M Davis | On 3/28/2013 6:17 PM, Jonathan M Davis wrote: > On Thursday, March 28, 2013 15:11:02 H. S. Teoh wrote: >> Maybe it's time to introduce cast(signed) or cast(unsigned) to the >> language, as bearophile suggests? > > It's not terribly pretty, but you can always do this: http://dlang.org/phobos/std_traits.html#.Unsigned http://dlang.org/phobos/std_traits.html#.Signed |
March 29, 2013 Re: bearophile can say "i told you so" (re uint->int implicit conv) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | Am 29.03.2013 02:28, schrieb Adam D. Ruppe: > Part of why I did it this way was the annoyance that I can't do a > variadic template in an interface. I'd REALLY prefer to do it that way > so there wouldn't be a list of types at all - just plain to!string(foo). > Who says you can't ? In fact you can using the NVI idiom: http://dpaste.dzfl.pl/d3b6dc77 Kind Regards Benjamin Thaut |
March 29, 2013 Re: bearophile can say "i told you so" (re uint->int implicit conv) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Benjamin Thaut | On Friday, 29 March 2013 at 09:26:33 UTC, Benjamin Thaut wrote:
> Who says you can't ? In fact you can using the NVI idiom:
Is that fairly new in D? I'm almost certain I tried it and it didn't work when I originally wrote this code (which was a couple years ago).
But it'd be worth redoing it now. The other place I use runtime varargs is:
// vararg hack so property assignment works right, even with null
string opDispatch(string field, string file = __FILE__, size_t line = __LINE__)(...)
I think there's a better way to do that now too. I'll have to spend some weekend gime on this.
|
March 29, 2013 Re: bearophile can say "i told you so" (re uint->int implicit conv) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | On Friday, March 29, 2013 13:10:02 Adam D. Ruppe wrote:
> On Friday, 29 March 2013 at 09:26:33 UTC, Benjamin Thaut wrote:
> > Who says you can't ? In fact you can using the NVI idiom:
> Is that fairly new in D? I'm almost certain I tried it and it didn't work when I originally wrote this code (which was a couple years ago).
It'll work with classes and protected. It's _supposed_ to work with interfaces and private according to TDPL, but AFAIK, that hasn't been implemented yet (though it might be; I don't know).
- Jonathan M Davis
|
March 29, 2013 Re: bearophile can say "i told you so" (re uint->int implicit conv) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan M Davis | Consider: uint u = ...; int x = u; Wouldn't a warning be enough? |
March 29, 2013 Re: bearophile can say "i told you so" (re uint->int implicit conv) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | Am 29.03.2013 13:10, schrieb Adam D. Ruppe: > > Is that fairly new in D? I'm almost certain I tried it and it didn't > work when I originally wrote this code (which was a couple years ago). > Yes it's fairly new. I think dmd 2.060 or something along that line. I tend to use it a lot because its so awesome ^^ For example my streaming interface for binary streams: interface IInputStream { public: final size_t read(T)(ref T data) if(!thBase.traits.isArray!T) { static assert(!is(T == const) && !is(T == immutable), "can not read into const / immutable value"); return readImpl((cast(void*)&data)[0..T.sizeof]); } final size_t read(T)(T data) if(thBase.traits.isArray!T) { static assert(!is(typeof(data[0]) == const) && !is(typeof(data[0]) == immutable), "can not read into const / immutable array"); return readImpl((cast(void*)data.ptr)[0..(arrayType!T.sizeof * data.length)]); } size_t skip(size_t bytes); protected: size_t readImpl(void[] buffer); } Kind Regards Benjamin Thaut |
Copyright © 1999-2021 by the D Language Foundation