I'm working on a project where i use a
lot of sbyte-type variables. I notice
that most (all?) operators tend to cast
the result into int type.
While i can easily recast e.g.
sbyte something = else % 10;
into
sbyte something = (sbyte)(else % 10);
it make the code look less readable
and is a bit tedious. I've been thaugt
that overloading is a no-no, but
perhaps it's a good idea to do it here.
1. Can i overload e.g. modulo operator
using some sort of extension method,
in such manner so it looks like "%"
still but returns sbyte type?
2. Should i do so?
3. How (if not easily googleable and/or
connected to evil gotchas)?
--
Regards
Konrad Viltersten
--------------------------------
sleep - a substitute for coffee for the poor
ambition - lack of sense to be lazy
Jon Skeet [C# MVP] - 19 Mar 2008 11:16 GMT
> I'm working on a project where i use a
> lot of sbyte-type variables. I notice
[quoted text clipped - 23 lines]
> 3. How (if not easily googleable and/or
> connected to evil gotchas)?
Do all your sbyte variables actually have the same logical type? If so,
why not encapsulate that in your own type, instead of using sbyte? (It
would probably be *backed* by an sbyte).
Then you can define your own operators for your type, which will
probably all return values of the same type.

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
K Viltersten - 19 Mar 2008 19:54 GMT
> While i can easily recast e.g.
>
[quoted text clipped - 8 lines]
> that overloading is a no-no, but
> perhaps it's a good idea to do it here.
Hrmp... I Don't think this code would
compile, due to the name of the variable.
I can't believe nobody noticed it! I just
saw it myself! Shame on you, Mr. Skeet,
for missing that! It was an open goal... :)
--
Regards
Konrad Viltersten
--------------------------------
sleep - a substitute for coffee for the poor
ambition - lack of sense to be lazy
Jon Skeet [C# MVP] - 19 Mar 2008 20:49 GMT
> > While i can easily recast e.g.
> >
[quoted text clipped - 11 lines]
> Hrmp... I Don't think this code would
> compile, due to the name of the variable.
I'd kind of assumed it was pseudo-code... You could always make it:
sbyte something = (sbyte)(@else % 10);
Best not to though.
> I can't believe nobody noticed it! I just
> saw it myself! Shame on you, Mr. Skeet,
> for missing that! It was an open goal... :)
;)

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Mythran - 20 Mar 2008 17:27 GMT
> it make the code look less readable
> and is a bit tedious. I've been thaugt
> that overloading is a no-no, but perhaps it's a good idea to do it here.
Wha?!? Who told you that overloading is a no-no? Operating overloading is
there for a reason, and it works great! Why would someone not want you
using operating overloading (if used appropriately and correctly)?
Mythran
K Viltersten - 20 Mar 2008 20:20 GMT
>> it make the code look less readable
>> and is a bit tedious. I've been thaugt
[quoted text clipped - 7 lines]
> operating overloading (if used
> appropriately and correctly)?
One of my supervisors during my master's
thesis. I'm not claiming him to be right
nor wrong (and perhaps it was a different
type of project).
Anyhow, i'm taking your advice gladly.
--
Regards
Konrad Viltersten
--------------------------------
sleep - a substitute for coffee for the poor
ambition - lack of sense to be lazy
Jon Skeet [C# MVP] - 20 Mar 2008 23:10 GMT
> >> it make the code look less readable
> >> and is a bit tedious. I've been thaugt
[quoted text clipped - 14 lines]
>
> Anyhow, i'm taking your advice gladly.
I would say that operator overloading should be used *very* cautiously.
I can't remember the last time I felt the need to do it (aside from
demo Complex structs). When it's useful, it's very useful. When it's
abused, it's a readability nightmare.

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Mythran - 21 Mar 2008 16:54 GMT
>> >> it make the code look less readable
>> >> and is a bit tedious. I've been thaugt
[quoted text clipped - 19 lines]
> demo Complex structs). When it's useful, it's very useful. When it's
> abused, it's a readability nightmare.
Aye, Jon, and as you probably noticed, I did add at the end of my previous
post "if used appropriately and correctly". But I probably could have used
a more precise term or statement that made it clearer.
--- BELOW IS AN OFF TOPIC REMARK ---
Jon, I was just gonna email ya before you posted this reply....
Read the following link:
http://www.evga.com/articles/395.asp
Now buy it for me.
Ok? Whew, glad I convinced ya to send it to me.
Thanks,
Mythran