Complicating code in C#
Moderators: hgm, Rebel, chrisw
-
- Posts: 7216
- Joined: Mon May 27, 2013 10:31 am
Re: Complicating code in C#
I see no better solution than replace ICoord (coordinate of a square) by an int otherwise an instance of it would become an object with extra overhead. If you would use predefined coord objects then you have to lock them when they are created. Might be a problem when there are many. Maybe you will implement three or multi dimensional chess in the future. Who knows.
-
- Posts: 7216
- Joined: Mon May 27, 2013 10:31 am
Re: Complicating code in C#
https://stackoverflow.com/questions/161 ... in-c-sharp
You can do something with 'using' statement in C#
Doubt if that will be practical.
You can do something with 'using' statement in C#
Code: Select all
using BitCoord64 = Int;
-
- Posts: 334
- Joined: Sat Feb 25, 2012 10:42 pm
- Location: Stockholm
Re: Complicating code in C#
You can use structs. See documentation with an example with coords see https://docs.microsoft.com/en-us/dotne ... pes/structHenk wrote: ↑Thu Dec 10, 2020 2:54 pm https://stackoverflow.com/questions/161 ... in-c-sharp
You can do something with 'using' statement in C#
Doubt if that will be practical.Code: Select all
using BitCoord64 = Int;
-
- Posts: 7216
- Joined: Mon May 27, 2013 10:31 am
Re: Complicating code in C#
In the example for X and Y they use double. I could use int.Pio wrote: ↑Thu Dec 10, 2020 3:43 pmYou can use structs. See documentation with an example with coords see https://docs.microsoft.com/en-us/dotne ... pes/structHenk wrote: ↑Thu Dec 10, 2020 2:54 pm https://stackoverflow.com/questions/161 ... in-c-sharp
You can do something with 'using' statement in C#
Doubt if that will be practical.Code: Select all
using BitCoord64 = Int;
I like to have an equivalent to
say typedef BitCoord64 = int range [0 ..63]
So EnPassantSquare() would return BitCoord64 and I would be sure its range is limited to 0 .. 63. If it returns an int it might return 99 or something. There is no implicit validation. Or i have to add extra code in EnPassantSquare method or property or whatever.
Last edited by Henk on Thu Dec 10, 2020 4:10 pm, edited 1 time in total.
-
- Posts: 334
- Joined: Sat Feb 25, 2012 10:42 pm
- Location: Stockholm
Re: Complicating code in C#
Just check for the range in the constructor and throw an exception if out of rangeHenk wrote: ↑Thu Dec 10, 2020 4:03 pmIn the example for X and Y they use double. I could use int.Pio wrote: ↑Thu Dec 10, 2020 3:43 pmYou can use structs. See documentation with an example with coords see https://docs.microsoft.com/en-us/dotne ... pes/structHenk wrote: ↑Thu Dec 10, 2020 2:54 pm https://stackoverflow.com/questions/161 ... in-c-sharp
You can do something with 'using' statement in C#
Doubt if that will be practical.Code: Select all
using BitCoord64 = Int;
I like to have an equivalent
say typedef BitCoord64 = int range [0 ..63]
So EnPassantSquare() would return BitCoord64 and I would be sure its range is limited to 0 .. 63. If it returns an int it might return 99 or something. There is no implicit validation. Or i have to add extra code in EnPassantSquare method or property or whatever.
-
- Posts: 334
- Joined: Sat Feb 25, 2012 10:42 pm
- Location: Stockholm
Re: Complicating code in C#
It might be better to use debug.Assert in your case since it might be in a time critical part of the engine and debug.Assert is only included while running in debug mode so it won’t affect speed in release modePio wrote: ↑Thu Dec 10, 2020 4:07 pmJust check for the range in the constructor and throw an exception if out of rangeHenk wrote: ↑Thu Dec 10, 2020 4:03 pmIn the example for X and Y they use double. I could use int.Pio wrote: ↑Thu Dec 10, 2020 3:43 pmYou can use structs. See documentation with an example with coords see https://docs.microsoft.com/en-us/dotne ... pes/structHenk wrote: ↑Thu Dec 10, 2020 2:54 pm https://stackoverflow.com/questions/161 ... in-c-sharp
You can do something with 'using' statement in C#
Doubt if that will be practical.Code: Select all
using BitCoord64 = Int;
I like to have an equivalent
say typedef BitCoord64 = int range [0 ..63]
So EnPassantSquare() would return BitCoord64 and I would be sure its range is limited to 0 .. 63. If it returns an int it might return 99 or something. There is no implicit validation. Or i have to add extra code in EnPassantSquare method or property or whatever.
-
- Posts: 7216
- Joined: Mon May 27, 2013 10:31 am
Re: Complicating code in C#
It is still using a struct. A one dimensional coord would be more efficient. Thought that new operations should be avoided or only used in factory methods.Pio wrote: ↑Thu Dec 10, 2020 4:07 pmJust check for the range in the constructor and throw an exception if out of rangeHenk wrote: ↑Thu Dec 10, 2020 4:03 pmIn the example for X and Y they use double. I could use int.Pio wrote: ↑Thu Dec 10, 2020 3:43 pmYou can use structs. See documentation with an example with coords see https://docs.microsoft.com/en-us/dotne ... pes/structHenk wrote: ↑Thu Dec 10, 2020 2:54 pm https://stackoverflow.com/questions/161 ... in-c-sharp
You can do something with 'using' statement in C#
Doubt if that will be practical.Code: Select all
using BitCoord64 = Int;
I like to have an equivalent
say typedef BitCoord64 = int range [0 ..63]
So EnPassantSquare() would return BitCoord64 and I would be sure its range is limited to 0 .. 63. If it returns an int it might return 99 or something. There is no implicit validation. Or i have to add extra code in EnPassantSquare method or property or whatever.
Or maybe a struct with one property Value. Still not efficient.
-
- Posts: 334
- Joined: Sat Feb 25, 2012 10:42 pm
- Location: Stockholm
Re: Complicating code in C#
A struct is a value type just like an int. it has no reference. What are you talking about?Henk wrote: ↑Thu Dec 10, 2020 4:19 pmIt is still using a struct. A one dimensional coord would be more efficient. Thought that new operations should be avoided or only used in factory methods.Pio wrote: ↑Thu Dec 10, 2020 4:07 pmJust check for the range in the constructor and throw an exception if out of rangeHenk wrote: ↑Thu Dec 10, 2020 4:03 pmIn the example for X and Y they use double. I could use int.Pio wrote: ↑Thu Dec 10, 2020 3:43 pmYou can use structs. See documentation with an example with coords see https://docs.microsoft.com/en-us/dotne ... pes/structHenk wrote: ↑Thu Dec 10, 2020 2:54 pm https://stackoverflow.com/questions/161 ... in-c-sharp
You can do something with 'using' statement in C#
Doubt if that will be practical.Code: Select all
using BitCoord64 = Int;
I like to have an equivalent
say typedef BitCoord64 = int range [0 ..63]
So EnPassantSquare() would return BitCoord64 and I would be sure its range is limited to 0 .. 63. If it returns an int it might return 99 or something. There is no implicit validation. Or i have to add extra code in EnPassantSquare method or property or whatever.
Or maybe a struct with one property Value. Still not efficient.
-
- Posts: 7216
- Joined: Mon May 27, 2013 10:31 am
Re: Complicating code in C#
Doubt ifPio wrote: ↑Thu Dec 10, 2020 4:25 pmA struct is a value type just like an int. it has no reference. What are you talking about?Henk wrote: ↑Thu Dec 10, 2020 4:19 pmIt is still using a struct. A one dimensional coord would be more efficient. Thought that new operations should be avoided or only used in factory methods.Pio wrote: ↑Thu Dec 10, 2020 4:07 pmJust check for the range in the constructor and throw an exception if out of rangeHenk wrote: ↑Thu Dec 10, 2020 4:03 pmIn the example for X and Y they use double. I could use int.Pio wrote: ↑Thu Dec 10, 2020 3:43 pmYou can use structs. See documentation with an example with coords see https://docs.microsoft.com/en-us/dotne ... pes/structHenk wrote: ↑Thu Dec 10, 2020 2:54 pm https://stackoverflow.com/questions/161 ... in-c-sharp
You can do something with 'using' statement in C#
Doubt if that will be practical.Code: Select all
using BitCoord64 = Int;
I like to have an equivalent
say typedef BitCoord64 = int range [0 ..63]
So EnPassantSquare() would return BitCoord64 and I would be sure its range is limited to 0 .. 63. If it returns an int it might return 99 or something. There is no implicit validation. Or i have to add extra code in EnPassantSquare method or property or whatever.
Or maybe a struct with one property Value. Still not efficient.
struct Coord
{
int Value;
}
is similar to int Value. You have to use coord.Value to access it.
c# does not support subrange.
Also want an interface for its implementation might change.
So I get
interface ICoord
{
int Value
}
That should be implemented by a struct?
I think I just use an int Coord; for now.
Unit tests should capture the errors. I don't know.
Just use int instead of an ICoord and see what happens. Check its subrange in constructor where Coord is part of.
O wait it is not the constructor everwhere where I use Coord in interface I have to validate it.
So
interface ICoord
{
int Value
}
would be more correct if it would not be slow and inconvenient. (coord.Value)
-
- Posts: 334
- Joined: Sat Feb 25, 2012 10:42 pm
- Location: Stockholm
Re: Complicating code in C#
You can use implicit conversion operators to achieve what you want. See https://stackoverflow.com/questions/15 ... r-a-struct for how to write the conversions.Henk wrote: ↑Thu Dec 10, 2020 4:34 pmDoubt ifPio wrote: ↑Thu Dec 10, 2020 4:25 pmA struct is a value type just like an int. it has no reference. What are you talking about?Henk wrote: ↑Thu Dec 10, 2020 4:19 pmIt is still using a struct. A one dimensional coord would be more efficient. Thought that new operations should be avoided or only used in factory methods.Pio wrote: ↑Thu Dec 10, 2020 4:07 pmJust check for the range in the constructor and throw an exception if out of rangeHenk wrote: ↑Thu Dec 10, 2020 4:03 pmIn the example for X and Y they use double. I could use int.Pio wrote: ↑Thu Dec 10, 2020 3:43 pmYou can use structs. See documentation with an example with coords see https://docs.microsoft.com/en-us/dotne ... pes/structHenk wrote: ↑Thu Dec 10, 2020 2:54 pm https://stackoverflow.com/questions/161 ... in-c-sharp
You can do something with 'using' statement in C#
Doubt if that will be practical.Code: Select all
using BitCoord64 = Int;
I like to have an equivalent
say typedef BitCoord64 = int range [0 ..63]
So EnPassantSquare() would return BitCoord64 and I would be sure its range is limited to 0 .. 63. If it returns an int it might return 99 or something. There is no implicit validation. Or i have to add extra code in EnPassantSquare method or property or whatever.
Or maybe a struct with one property Value. Still not efficient.
struct Coord
{
int Value;
}
is similar to int Value. You have to use coord.Value to access it.
c# does not support subrange.
Also want an interface for its implementation might change.
So I get
interface ICoord
{
int Value
}
That should be implemented by a struct?
I think I just use an int Coord; for now.
Unit tests should capture the errors. I don't know.
Just use int instead of an ICoord and see what happens. Check its subrange in constructor where Coord is part of.
O wait it is not the constructor everwhere where I use Coord in interface I have to validate it.
So
interface ICoord
{
int Value
}
would be more correct if it would not be slow and inconvenient. (coord.Value)
You completely misunderstand when to use interfaces. As long as you don’t have two or more classes there is no benefit of using interfaces it just makes the code a lot worse. Interfaces should only be used with behaviour so properties are really really bad to use in interfaces since those expose the data not behaviour. The only place where an interface might be used in an engine is if you use it for supporting different protocols or maybe if you want to use different search algorithms but probably you won’t need an interface in that case either you could just use a function declaration