Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. Is there a programming language...

Is there a programming language...

Scheduled Pinned Locked Moved The Lounge
questioncsharpdatabasecomtesting
72 Posts 26 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Marc Clifton

    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

    Day 1: Spider Database Navigator Unit Testing Succinctly

    R Offline
    R Offline
    Ron Beyer
    wrote on last edited by
    #13

    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.

    M P B 4 Replies Last reply
    0
    • PJ ArendsP PJ Arends

      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!

      R Offline
      R Offline
      Ron Beyer
      wrote on last edited by
      #14

      My point exactly, typedef would be a legitimate use of it, #define works because of precompiler even if it makes the programmers head hurt.

      1 Reply Last reply
      0
      • M Marc Clifton

        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

        Day 1: Spider Database Navigator Unit Testing Succinctly

        P Offline
        P Offline
        Pete OHanlon
        wrote on last edited by
        #15

        Great minds and all of that.

        1 Reply Last reply
        0
        • M Maximilien

          In C++11, there is the notion of user defined literals. Have a look at this : http://akrzemi1.wordpress.com/2012/08/12/user-defined-literals-part-i/[^]

          I'd rather be phishing!

          M Offline
          M Offline
          Marc Clifton
          wrote on last edited by
          #16

          Maximilien wrote:

          In C++11, there is the notion of user defined literals.

          Wow, that was a fascinating read - thanks for the link. It's been many years since I looked at C++! Marc

          Day 1: Spider Database Navigator Unit Testing Succinctly

          1 Reply Last reply
          0
          • P Pablo Aliskevicius

            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); // m/(s*s)
            Mass m = new mass(25); // kg
            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).

            M Offline
            M Offline
            Marc Clifton
            wrote on last edited by
            #17

            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

            Day 1: Spider Database Navigator Unit Testing Succinctly

            1 Reply Last reply
            0
            • P Phil Martin

              Yes. F# supports units of measurement[^]. For example, you can do this:

              [] type years

              let myAge = 32

              The downside this is strictly language support, and not runtime support. Units are lost at runtime, but it's still pretty handy.

              M Offline
              M Offline
              Marc Clifton
              wrote on last edited by
              #18

              Phil Martin wrote:

              Units are lost at runtime

              Which is unfortunate because I'd possibly like to be able to reflect on the unit of measure. But it's an interesting avenue to explore. Thanks! Marc

              Day 1: Spider Database Navigator Unit Testing Succinctly

              P 1 Reply Last reply
              0
              • R Ron Beyer

                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.

                M Offline
                M Offline
                Marc Clifton
                wrote on last edited by
                #19

                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

                Day 1: Spider Database Navigator Unit Testing Succinctly

                G 1 Reply Last reply
                0
                • M Marc Clifton

                  ...that works "easily" with semantic types? For example, I may have: int age = 51; which completely loses the concept that 51 is an age (in years). What I want is something like: AgeInYears myAge = 51; and yet still be able to specify that I can perform, say, arithmetic operations on "myAge". For example, in C#, I could write:

                  class AgeInYears
                  {
                  public int Value {get;set;}
                  }

                  ... implement operators on AgeInYears

                  But that gets messy real fast - every "semantic type" needs these operators, etc. Furthermore, the unit of measurement is still not handled very elegantly. So, as the question states, are there programming languages out there that are more expressive of semantic types? Marc

                  Day 1: Spider Database Navigator Unit Testing Succinctly

                  OriginalGriffO Offline
                  OriginalGriffO Offline
                  OriginalGriff
                  wrote on last edited by
                  #20

                  Annoyingly, int is a sealed type in C#, or all you would have to do is provide the implicit cast operators:

                  class AgeInYears : int
                  {
                  public static implicit operator AgeInYears(int i)
                  {
                  return (AgeInYears)i;
                  }
                  public static implicit operator int(AgeInYears a)
                  {
                  return (int)a;
                  }
                  }

                  But...what is an age plus an age? It's not really anything useful if you think about it. What you should be thinking of here is an Age plus a Timespan equals a DateTime, but then an Age can't really be assigned an integer value unless it already has a Datetime component - perhaps it is relative to the time at which the Age object is instantiated? And don't forget that an Age is not a constant value: it will vary as the application runs... :laugh:

                  "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                  "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                  M G 2 Replies Last reply
                  0
                  • OriginalGriffO OriginalGriff

                    Annoyingly, int is a sealed type in C#, or all you would have to do is provide the implicit cast operators:

                    class AgeInYears : int
                    {
                    public static implicit operator AgeInYears(int i)
                    {
                    return (AgeInYears)i;
                    }
                    public static implicit operator int(AgeInYears a)
                    {
                    return (int)a;
                    }
                    }

                    But...what is an age plus an age? It's not really anything useful if you think about it. What you should be thinking of here is an Age plus a Timespan equals a DateTime, but then an Age can't really be assigned an integer value unless it already has a Datetime component - perhaps it is relative to the time at which the Age object is instantiated? And don't forget that an Age is not a constant value: it will vary as the application runs... :laugh:

                    M Offline
                    M Offline
                    Marc Clifton
                    wrote on last edited by
                    #21

                    OriginalGriff wrote:

                    Annoyingly, int is a sealed type in C#, or all you would have to do is provide the implicit cast operators:

                    Exactly!

                    OriginalGriff wrote:

                    And don't forget that an Age is not a constant value: it will vary as the application runs...

                    I know. :) It was a contrived example.

                    OriginalGriff wrote:

                    But...what is an age plus an age?

                    Yeah, this stuff gets one to really think about the meaning of things. :) Marc

                    Day 1: Spider Database Navigator Unit Testing Succinctly

                    1 Reply Last reply
                    0
                    • R Ron Beyer

                      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.

                      P Offline
                      P Offline
                      PIEBALDconsult
                      wrote on last edited by
                      #22

                      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 .

                      1 Reply Last reply
                      0
                      • M Marc Clifton

                        ...that works "easily" with semantic types? For example, I may have: int age = 51; which completely loses the concept that 51 is an age (in years). What I want is something like: AgeInYears myAge = 51; and yet still be able to specify that I can perform, say, arithmetic operations on "myAge". For example, in C#, I could write:

                        class AgeInYears
                        {
                        public int Value {get;set;}
                        }

                        ... implement operators on AgeInYears

                        But that gets messy real fast - every "semantic type" needs these operators, etc. Furthermore, the unit of measurement is still not handled very elegantly. So, as the question states, are there programming languages out there that are more expressive of semantic types? Marc

                        Day 1: Spider Database Navigator Unit Testing Succinctly

                        G Offline
                        G Offline
                        Gary Wheeler
                        wrote on last edited by
                        #23

                        A long time ago, at a defense contractor far, far defunct... The Ada programming language provided a semblance of semantic typing. You could create an 'Age' type that was a subtype of integer. I'm sure the computer scientists would scoff at Ada's limitations, but it does somewhat fit the bill. I don't know the modern language definition (I used it back in the 80's), so it might be more capable now.

                        Software Zen: delete this;

                        M 1 Reply Last reply
                        0
                        • M Marc Clifton

                          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

                          Day 1: Spider Database Navigator Unit Testing Succinctly

                          G Offline
                          G Offline
                          Gary Wheeler
                          wrote on last edited by
                          #24

                          var has its place. Too many programmers let it escape that place, unfortunately.

                          Software Zen: delete this;

                          S 1 Reply Last reply
                          0
                          • OriginalGriffO OriginalGriff

                            Annoyingly, int is a sealed type in C#, or all you would have to do is provide the implicit cast operators:

                            class AgeInYears : int
                            {
                            public static implicit operator AgeInYears(int i)
                            {
                            return (AgeInYears)i;
                            }
                            public static implicit operator int(AgeInYears a)
                            {
                            return (int)a;
                            }
                            }

                            But...what is an age plus an age? It's not really anything useful if you think about it. What you should be thinking of here is an Age plus a Timespan equals a DateTime, but then an Age can't really be assigned an integer value unless it already has a Datetime component - perhaps it is relative to the time at which the Age object is instantiated? And don't forget that an Age is not a constant value: it will vary as the application runs... :laugh:

                            G Offline
                            G Offline
                            Gary Wheeler
                            wrote on last edited by
                            #25

                            You could argue that AgeInYears should be implemented something like this:

                            class AgeInYears
                            {
                            public AgeInYears(DateTime birthdate)
                            {
                            _Birthdate = birthdate;
                            }
                            private DateTime _Birthdate;
                            public int Years
                            {
                            get
                            {
                            return (DataTime.Now - _Birthdate).Days / 365; // yes, I know 365 isn't right; it's just an example, for gosh sakes
                            }
                            }
                            }

                            Software Zen: delete this;

                            OriginalGriffO 1 Reply Last reply
                            0
                            • G Gary Wheeler

                              You could argue that AgeInYears should be implemented something like this:

                              class AgeInYears
                              {
                              public AgeInYears(DateTime birthdate)
                              {
                              _Birthdate = birthdate;
                              }
                              private DateTime _Birthdate;
                              public int Years
                              {
                              get
                              {
                              return (DataTime.Now - _Birthdate).Days / 365; // yes, I know 365 isn't right; it's just an example, for gosh sakes
                              }
                              }
                              }

                              Software Zen: delete this;

                              OriginalGriffO Offline
                              OriginalGriffO Offline
                              OriginalGriff
                              wrote on last edited by
                              #26

                              *cough* :-O I did... Working with Age: it's not the same as a TimeSpan![^]

                              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                              "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                              G 1 Reply Last reply
                              0
                              • OriginalGriffO OriginalGriff

                                *cough* :-O I did... Working with Age: it's not the same as a TimeSpan![^]

                                G Offline
                                G Offline
                                Gary Wheeler
                                wrote on last edited by
                                #27

                                Yeah, this reeked of prior art but I couldn't be arsed to go looking just to comment on a casual question. :-D

                                Software Zen: delete this;

                                1 Reply Last reply
                                0
                                • M Marc Clifton

                                  ...that works "easily" with semantic types? For example, I may have: int age = 51; which completely loses the concept that 51 is an age (in years). What I want is something like: AgeInYears myAge = 51; and yet still be able to specify that I can perform, say, arithmetic operations on "myAge". For example, in C#, I could write:

                                  class AgeInYears
                                  {
                                  public int Value {get;set;}
                                  }

                                  ... implement operators on AgeInYears

                                  But that gets messy real fast - every "semantic type" needs these operators, etc. Furthermore, the unit of measurement is still not handled very elegantly. So, as the question states, are there programming languages out there that are more expressive of semantic types? Marc

                                  Day 1: Spider Database Navigator Unit Testing Succinctly

                                  G Offline
                                  G Offline
                                  greydmar
                                  wrote on last edited by
                                  #28

                                  Well This c++ 11 trick is very powerfull, really! I prefer to do all the hard work from scratch (ok, have R #). I believe that this threshold "semantic" is outside the domain of a programming language (commonly, it is a "system domain" concept), because it is difficult to predict the particularities of a user-defined (conversion, comparison, integrity, serialization, etc.).

                                  M 1 Reply Last reply
                                  0
                                  • R Ron Beyer

                                    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.

                                    B Offline
                                    B Offline
                                    BillWoodruff
                                    wrote on last edited by
                                    #29

                                    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

                                    R 1 Reply Last reply
                                    0
                                    • R Ron Beyer

                                      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.

                                      B Offline
                                      B Offline
                                      BillWoodruff
                                      wrote on last edited by
                                      #30

                                      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

                                      R 1 Reply Last reply
                                      0
                                      • B BillWoodruff

                                        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

                                        R Offline
                                        R Offline
                                        Ron Beyer
                                        wrote on last edited by
                                        #31

                                        If you only knew how my mind worked :)

                                        S 1 Reply Last reply
                                        0
                                        • B BillWoodruff

                                          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

                                          R Offline
                                          R Offline
                                          Ron Beyer
                                          wrote on last edited by
                                          #32

                                          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.

                                          B 1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups