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 Offline
    M Offline
    Marc Clifton
    wrote on last edited by
    #1

    ...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

    M P R P P 17 Replies 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

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

      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.

      M J PJ ArendsP R F 6 Replies 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

        M Offline
        M Offline
        Maximilien
        wrote on last edited by
        #3

        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 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

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

          Well, you can do things analagous to this in Smalltalk.

          M 1 Reply Last reply
          0
          • P Pete OHanlon

            Well, you can do things analagous to this in Smalltalk.

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

            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 1 Reply Last reply
            0
            • R Ron Beyer

              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.

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

              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 1 Reply Last reply
              0
              • R Ron Beyer

                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.

                J Offline
                J Offline
                Johnny J
                wrote on last edited by
                #7

                Ron Beyer wrote:

                AgeInYears oldAge = 50;

                You know that you're offending quite a lot of the CP users here, don't you? ;P

                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

                R 1 Reply Last reply
                0
                • J Johnny J

                  Ron Beyer wrote:

                  AgeInYears oldAge = 50;

                  You know that you're offending quite a lot of the CP users here, don't you? ;P

                  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

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

                  :D I'm creeping up there myself, so old age certainly isn't far off for me.

                  J 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

                    P Offline
                    P Offline
                    Pablo Aliskevicius
                    wrote on last edited by
                    #9

                    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 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

                      P Offline
                      P Offline
                      Phil Martin
                      wrote on last edited by
                      #10

                      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 1 Reply Last reply
                      0
                      • R Ron Beyer

                        :D I'm creeping up there myself, so old age certainly isn't far off for me.

                        J Offline
                        J Offline
                        Johnny J
                        wrote on last edited by
                        #11

                        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

                        1 Reply Last reply
                        0
                        • R Ron Beyer

                          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.

                          PJ ArendsP Offline
                          PJ ArendsP Offline
                          PJ Arends
                          wrote on last edited by
                          #12

                          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!

                          Within you lies the power for good; Use it!

                          R 1 Reply Last reply
                          0
                          • 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
                                          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