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

    ...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
                      • M Marc Clifton

                        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 Offline
                        P Offline
                        Phil Martin
                        wrote on last edited by
                        #33

                        Yeah, it is unfortunate. In the engineering applications I write, I've created a ScalaryQuantity and a VectorQuantity class. They are just the usual numeric structures which support all the normal arithmetic, but supports keeping track of units, and converting units when necessary. There's a big run time overhead involved, but for me it is worth it because it has helped me catch many errors far earlier in the process of developing new calculations.

                        1 Reply Last reply
                        0
                        • G Gary Wheeler

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

                          Gary Wheeler wrote:

                          The Ada programming language provided a semblance of semantic typing.

                          Ah, it does indeed. I've been reading the Ada type stuff - very slick. It's a pity these constructs aren't in other languages. I wonder why not - it seems like it would really help bullet proof code. Then again, like anything else, I bet it can be horribly abused as well. Marc

                          R G 2 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

                            H Offline
                            H Offline
                            HaBiX
                            wrote on last edited by
                            #35

                            "which completely loses the concept that 51 is an age (in years)." so write int ageInYears = 51; There are many ways to do it in c# (pass the int value in constructor, make an implicit cast operator, ...), but imho the best way is sticking with plain int, as that what "age in years" exactly is.

                            1 Reply Last reply
                            0
                            • R Ron Beyer

                              If you only knew how my mind worked :)

                              S Offline
                              S Offline
                              Simon ORiordan from UK
                              wrote on last edited by
                              #36

                              My mind doesn't work. So I would use Python. :zzz:

                              1 Reply Last reply
                              0
                              • G Gary Wheeler

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

                                Software Zen: delete this;

                                S Offline
                                S Offline
                                SortaCore
                                wrote on last edited by
                                #37

                                Cue "who let the dogs out" playing for an unreasonably long time in my head...

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

                                  R Offline
                                  R Offline
                                  Rob Grainger
                                  wrote on last edited by
                                  #38

                                  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; // OOPS!

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

                                  1 Reply Last reply
                                  0
                                  • M Marc Clifton

                                    Gary Wheeler wrote:

                                    The Ada programming language provided a semblance of semantic typing.

                                    Ah, it does indeed. I've been reading the Ada type stuff - very slick. It's a pity these constructs aren't in other languages. I wonder why not - it seems like it would really help bullet proof code. Then again, like anything else, I bet it can be horribly abused as well. Marc

                                    R Offline
                                    R Offline
                                    Rob Grainger
                                    wrote on last edited by
                                    #39

                                    Stroustrup has written somewhere on implementing SI measures in C++ using user-defined literals and simple classes. That would work well, but should really be part of the standard library.

                                    "If you don't fail at least 90 percent of the time, you're not aiming high enough." Alan Kay.

                                    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

                                      K Offline
                                      K Offline
                                      kalberts
                                      wrote on last edited by
                                      #40

                                      CHILL (CCITT HIgh Level Language - CCITT was the old name of ITU-T) probably has the best developed strict type system of any industrial language. One of the features was the distinction between a SYNMODE and NEWMODE definitions (MODE is the CHILL term for type/class): A SYNMODE defines restrictions (like value subranges) or aggregates (like arrays) of existing types, but (within the restrictions) fully compatible with the base mode. A NEWMODE is similar, but defines an incompatible mode. So if you make new integer modes AppleCount and OrangeCount using SYNMODE, you can add apples and oranges. If you define dem using NEWMODE, the compiler won't allow you to add apples and oranges without an explicit cast. CHILL was developed to be the ITU standard for programming telephone switches, but the language design is just as general as, say, c or java. It never made any success in non-telephone environments (and even there it never took more than about half of the market), which is a pity: CHILL is one of the most thoroughly well-designed languages there is. But then again: The marketplace isn't known for always selecting the best designs... (I won't give c as an exapmple of that, that could hurt some people's feelings).

                                      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

                                        R Offline
                                        R Offline
                                        Rob Grainger
                                        wrote on last edited by
                                        #41

                                        Marc has already pointed to Smalltalk, someone else pointed out C++, I'd like to add Haskell to the mix. Getting type safety would be easy:

                                        newtype meter m = meter m deriving (Show, Num, Eq, Ord)
                                        newtype foot f = foot f deriving (Show, Num, Eq, Ord)

                                        -- Usage (ghci> is an iteractive prompt)
                                        ghci> let m1 = meter 5
                                        ghci> let m2 = meter 10
                                        ghci> let m3 = m1 + m2
                                        ghci> m3
                                        meter 15
                                        ghci> let f1 = foot 3
                                        ghci> let f2 = foot 4
                                        ghci> let f3 = f1 + f2
                                        ghci> f3
                                        foot 7
                                        ghci> let e1 = f1 + m1 -- Won't Work - produces error of types mismatching

                                        With more work, it can be extended to add support for conversions, magnitudes (nm, mm, m, km, ...). Indeed someone has already done all this work for us: Dimensional[^] and Dimensional using type families[^]. All available at the standard repository Hackage[^].

                                        "If you don't fail at least 90 percent of the time, you're not aiming high enough." Alan Kay.

                                        M 1 Reply Last reply
                                        0
                                        • M Marc Clifton

                                          Gary Wheeler wrote:

                                          The Ada programming language provided a semblance of semantic typing.

                                          Ah, it does indeed. I've been reading the Ada type stuff - very slick. It's a pity these constructs aren't in other languages. I wonder why not - it seems like it would really help bullet proof code. Then again, like anything else, I bet it can be horribly abused as well. Marc

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

                                          The central goal behind Ada was to make it difficult to make common programming mistakes, and to have the compiler enforce as many conditions for correctness as possible. The language design succeeded in a lot of respects, but it made it difficult to write code in the language. It was very frustrating learning how to appease the compiler. In my case, we were beta testers for one of the first VAX/VMS Ada compilers. It was difficult to tell the difference between genuine flaws in our source and possible compiler bugs.

                                          Software Zen: delete this;

                                          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