|
Well, you can do things analagous to this in Smalltalk.
|
|
|
|
|
Pete O'Hanlon wrote: Well, you can do things analagous to this in Smalltalk.
Funny you mention that, I was looking at smalltalk a couple minutes ago!
Marc
|
|
|
|
|
Great minds and all of that.
|
|
|
|
|
I hate myself for typing this:
namespace TestApp1
{
using AgeInYears = System.Int32;
class Program
{
static void Main(string[] args)
{
AgeInYears myAge = 10;
AgeInYears oldAge = 50;
AgeInYears timeUntilOldAge = oldAge - myAge;
}
}
}
Yes, that's perfectly legal C# code. Its technically an int, works the same way that #define does in c++ to replace types. It only works in single code files though.
|
|
|
|
|
Ron Beyer wrote: AgeInYears oldAge = 50; You know that you're offending quite a lot of the CP users here, don't you?
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
|
|
|
|
|
I'm creeping up there myself, so old age certainly isn't far off for me.
|
|
|
|
|
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
|
|
|
|