|
Well, if only you don't add this:
if (myAge >= oldAge)
thisGuyIsDead = true;
or
if (myAge >= oldAge)
BookSpaceInRetirementHome(this);
Anything that is unrelated to elephants is irrelephant Anonymous ----- The problem with quotes on the internet is that you can never tell if they're genuine Winston Churchill, 1944 ----- I'd just like a chance to prove that money can't make me happy. Me, all the time
|
|
|
|
|
Ron Beyer wrote: Yes, that's perfectly legal C# code.
Fascinating. I'm glad there are some things of which I'm still ignorant.
Marc
|
|
|
|
|
I wouldn't get too excited about it though, its really one of the more horrible C# "features". Try the little program out once, then type out a function that has int's as parameters, Intellisense replaces any occurrence of the type with AgeInYears. And while you can define more than one alias for the same type, Intellisense will pick the last defined one to replace in the preview window.
Its also a really good way of making code impossible to follow.
|
|
|
|
|
Ron Beyer wrote: Its also a really good way of making code impossible to follow.
No worse than using "var" implicit types, I suspect.
Marc
|
|
|
|
|
var has its place. Too many programmers let it escape that place, unfortunately.
Software Zen: delete this;
|
|
|
|
|
Cue "who let the dogs out" playing for an unreasonably long time in my head...
|
|
|
|
|
Software Zen: delete this;
|
|
|
|
|
That's a problem with Intellisense then, not the language.
Defining aliases is the one best use for the using directive, but I limit it to complex types like Dictionary-of-Dictionary-of-List kinds of things.
Or, you can make a more general alias for a particular type, like using Connection=System.Data.SqlClient.SqlConnection .
|
|
|
|
|
Hi Ron, fyi: Visual Studio 2013 IntelliSense does not suggest, or replace, an int Type with an alias defined in a Using statement.
If you seek to aid everyone that suffers in the galaxy, you will only weaken yourself … and weaken them. It is the internal struggles, when fought and won on their own, that yield the strongest rewards… If you care for others, then dispense with pity and sacrifice and recognize the value in letting them fight their own battles." Darth Traya
|
|
|
|
|
Odd, mine does, VS2013 is what I tried that in. When I typed String.Compare( then hit the down arrow to select one of the overloads that had an int, it showed AgeInYears instead of int.
|
|
|
|
|
Interesting; a little investigation shows that a ReSharper trial version (Build 8.1.23.546) is what is making the difference in what shows up in the Intellisense pop-up, where ints are expected.
bill
If you seek to aid everyone that suffers in the galaxy, you will only weaken yourself … and weaken them. It is the internal struggles, when fought and won on their own, that yield the strongest rewards… If you care for others, then dispense with pity and sacrifice and recognize the value in letting them fight their own battles." Darth Traya
|
|
|
|
|
Upvoted ! Before I read your response, I had opened VS 2013, and typed:
using AgeInYears = System.Int32;
Any time my mind works like yours, I feel better
bill
If you seek to aid everyone that suffers in the galaxy, you will only weaken yourself … and weaken them. It is the internal struggles, when fought and won on their own, that yield the strongest rewards… If you care for others, then dispense with pity and sacrifice and recognize the value in letting them fight their own battles." Darth Traya
|
|
|
|
|
If you only knew how my mind worked
|
|
|
|
|
My mind doesn't work.
So I would use Python.
|
|
|
|
|
Ron Beyer wrote: works the same way that #define does in c++ to replace types.
In C++ one would use typedef for this purpose. Using #define is just wrong
Within you lies the power for good - Use it!
|
|
|
|
|
My point exactly, typedef would be a legitimate use of it, #define works because of precompiler even if it makes the programmers head hurt.
|
|
|
|
|
It fails totally though for type safety.
using AgeInYears = System.Int32;
using AgeInDays = System.Int32;
...
AgeInYears yearAge = 10;
AgeInDays dayAge = 3650;
var myAge = yearAge + dayAge;
(A better example may involve standard units of measure: inches, miles, meters, temperatures, etc.)
The system should at least prevent naive attempts to assign to incorrect types.
Ideally, the system should be able to perform conversions where possible.
Smalltalk and C++ both offer enough flexibility. In C#, a struct could be declared, but I think you'd struggle to make it semantically sound.
"If you don't fail at least 90 percent of the time, you're not aiming high enough."
Alan Kay.
|
|
|
|
|
You should rewrite it that way.
namespace TestApp1
{
using AgeInYears = System.Int32;
class Program
{
static void Main(string[] args)
{
AgeInYears myAge = 10;
AgeInYears timeUntilOldAge = 10;
AgeInYears oldAge = myAge + timeUntilOldAge;
}
}
}
So you are forever young.
The good thing about pessimism is, that you are always either right or pleasently surprised.
|
|
|
|
|
isn't that akin to a typedef?
David
|
|
|
|
|
Kinda, as I said in a different reply, typedef has valid uses (defining a BOOL for example) that actually creates a new type and can be universal, whereas "using" alias is more like a #define replacement in a single file.
|
|
|
|
|
I suppose typedefs get type checked at compile time and #defines do not. Is that correct? so maybe the typedef equivalent is the safer way to go?
David
|
|
|
|
|
#define will be checked at compile time just like typedefs because the preprocessor will run through and replace all instances of the #define name with the value before compiling.
If typedef had an equivalent c# construct that would certainly be the better way to go, but it doesn't. So the only real way to redefine types as a different name is to use the "using" or to create your own value type that derives from the equivalent type.
|
|
|
|
|
Obviously, you don't mean just time: otherwise, timespan and datetime in c# would do the trick.
For phisical entities (mass, distance, acceleration, ....) there is a C++ library in BOOST:
http://www.boost.org/doc/libs/1_41_0/doc/html/boost_units/Dimensional_Analysis.html[^]
I remember reading an article (which I can't find) that used this to implement classes that allow you to do the following:
Acceleration g = new Acceleration(9.88); Mass m = new mass(25); Force f = m * g;
Is that what you're looking for?
Update: Found it. http://www.boostpro.com/mplbook/metafunctions.html[^].
Pablo.
"Accident: An inevitable occurrence due to the action of immutable natural laws." (Ambrose Bierce, circa 1899).
"You are to act in the light of experience as guided by intelligence" (Rex Stout, "In the Best Families", 1950).
modified 18-Dec-13 9:45am.
|
|
|
|
|
Pablo Aliskevicius wrote: I remember reading an article (which I can't find) that used this to implement classes that allow you to do the following:
Yes, I was just reading about that. There's a CP article that also does some pre-processing for C# that allows units of measure to be specified, very similar to F#.
And yes, that's one piece of the puzzle I'm working on.
Marc
|
|
|
|
|
Yes. F# supports units of measurement[^].
For example, you can do this:
[<Measure>] type years
let myAge = 32<years>
The downside this is strictly language support, and not runtime support. Units are lost at runtime, but it's still pretty handy.
|
|
|
|