News for hackers. Stuff that matters.
Re:Giving Ada a Chance by UncleOxidant on Tuesday March 02, 2021 @03:04PM (#26319843)
I really like Ada's ranged types (VHDL has them also - it inherited them from Ada). You can say:

    type OperatingTemp is range 33 .. 90;
And then declare variables of that type and they will be range checked - an exception will be thrown if the variable goes out of that range. Wish more languages had this feature.
Re:Giving Ada a Chance by throwaway894345 on Tuesday March 02, 2021 @03:57PM (#26320610)
I agree. I learned Ada in university (my professor was on the Ada committee) and used it for an embedded development course. The language was really good (many fewer foot guns than C or C++; clearly Ada was holistically designed), but I recall having issues with the tooling and the ecosystem, and the community was super defensive and hostile. If something wasn't working for your use case and the community didn't have a solution, then your use case was invalid. If you politely asked for help, you were incompetent or too lazy to figure it out on your own. This was a decade ago, so maybe things have changed since, but I can honestly understand why the language is niche. The type system and other intra-language features are very interesting but ultimately not the most important aspects of a programming language.
Re:Giving Ada a Chance by daniellarusso on Tuesday March 02, 2021 @05:23PM (#26321881)
GWU?
Re:Giving Ada a Chance by throwaway894345 on Tuesday March 02, 2021 @05:35PM (#26322041)
Nope. And I'm not going to say since this is my top secret anon account.
Re:Giving Ada a Chance by drfuchs on Tuesday March 02, 2021 @03:40PM (#26320328)
Ranged types are directly from Pascal. You can also use them to specify the valid indexes for an array (so a given array might be zero-based, or one-based, or 1900-based, or even -32768-based). A very positive feature, I agree.
Re:Giving Ada a Chance by Jtsummers on Tuesday March 02, 2021 @03:54PM (#26320545)
And, even better, arrays in Ada (can't speak to Pascal) can be any discrete type so long as it is a range:

  type Character_Histogram is Array (range 'A'..'Z') of Integer;
  -- syntax may be off, not enough practice to do this cold right now
Re:Giving Ada a Chance by FpUser on Tuesday March 02, 2021 @04:08PM (#26320773)

  type
    TPerson = record
      name: string;
      age: integer;
    end;
    TThrteenPersons = array[10..22] of TPerson;

  procedure Check(const aPerson: TPerson);
  var
    vPerson: TThrteenPersons;
  begin
    vPerson[11] := aPerson; // this is ok
    vPerson[1] := aPerson; // this one gives compile error
  end;
alternatively if runtime range checking is on then vPerson[i] = aPerson will raise the exception if i is out of range
Re:Giving Ada a Chance by Jtsummers on Tuesday March 02, 2021 @04:12PM (#26320831)
Right. I know it works with numeric indexes in Pascal. My point was that (using my previous example) in Ada it works with any discrete type, in that case characters (which are not numbers in disguise in Ada) so I can do this:

  Histogram : Character_Histogram;
  ...
  Histogram['A'] := Histogram['A'] + 1;
(Ok, a bit more work because I didn't initialize the histogram to 0.)
Re:Giving Ada a Chance by kwhitefoot on Tuesday March 02, 2021 @05:26PM (#26321929)
It works with any ordinal type in Pascal, including characters, booleans and enums.
Re:Giving Ada a Chance by Lucretia9 on Tuesday March 02, 2021 @04:56PM (#26321512)
Square brackets are coming, so you'd have to use ('A')
Re:Giving Ada a Chance by Jtsummers on Tuesday March 02, 2021 @05:40PM (#26322111)
Hah, I've been in Go lately so my brain defaulted to the wrong thing in that example.
Re:Giving Ada a Chance by FpUser on Tuesday March 02, 2021 @05:25PM (#26321910)
Pascal (well Delphi/FreePascal incarnation) also works with chars and enums.

This modern pascal is very feature rich and has plenty of fancy bells and whistles.

Re:Giving Ada a Chance by drfuchs on Tuesday March 02, 2021 @05:39PM (#26322106)
Wirth's original, standard Pascal allowed for int/char/bool/enum, and ranges thereof, as array bounds; it wasn't a Delphi enhancement.
Re:Giving Ada a Chance by FpUser on Tuesday March 02, 2021 @05:48PM (#26322204)
I am sure it was. I am just not familiar with it hence specified the one I know.
Re:Giving Ada a Chance by kwhitefoot on Tuesday March 02, 2021 @05:24PM (#26321888)
In Pascal you can use any ordinal type as the type of the array index. You can create a new type from a range of ordinal values.

So you can have arrays where the index runs from -10 to +20 or from +7 to +26 for instance. Booleans are also ordinal types so you can have an array where the index is boolean.

Re:Giving Ada a Chance by Avshalom on Tuesday March 02, 2021 @03:59PM (#26320634)
I don't think it even needs to be a range, you can index using enums
Re:Giving Ada a Chance by Jtsummers on Tuesday March 02, 2021 @04:06PM (#26320732)
Well, "range" was not the correct term. What I meant is that you can't treat the source as sparse. You can't skip portions of it. So you can't make an array that only covers every other element of this enumeration:

  type Colors is (Red, Green, Blue, Black, Gray, White);
It has to be a consecutive group of them, taken in order, as the specification of even this enumeration has an ordering to it as far as Ada is concerned. So I can make an array using all of Colors as the index, or some subset but it has to be, say, Red..Blue and not Red|Blue skipping over Green.
Re:Giving Ada a Chance by Lucretia9 on Tuesday March 02, 2021 @04:55PM (#26321503)
Ada 2012 allows you to define gaps, this is my best example I have available https://github.com/Lucretia/sdlada/blob/master/src/sdl-video...
Re:Giving Ada a Chance by Jtsummers on Tuesday March 02, 2021 @05:43PM (#26322152)
That is great to read, and probably a sign that my learning materials should be updated. I'll try to remember this in the future.
Re:Giving Ada a Chance by the_only_law on Tuesday March 02, 2021 @05:28PM (#26321950)
I have little knowledge of HDL’s but I’m constantly hearing bad things about Verilog and good things about VHDL. Is it worth picking up?
Re:Giving Ada a Chance by UncleOxidant on Tuesday March 02, 2021 @06:47PM (#26322848)
I much prefer VHDL to Verilog. Mostly because you can define your own types in VHDL but can't in standard verilog (SystemVerilog is closer to VHDL in capability). Also because the whole reg thing in Verilog just doesn't seem very ergonomic to me - I have to stop and look things up when coding state machines or similar stateful things in Verilog. In VHDL you've got signals and variables. Signals can have state. Variables are only used inside of processes - much easier to remember for me, anyway (maybe my block has to do with learning VHDL first).

And finally VHDL is strongly typed. Verilog is pretty much C-typed meaning it's pretty weak. About a dozen years ago I worked at an EDA company and one of my tasks was to run our generated HDL code through a popular industry linting tool. There were hundreds linting problems with the Verilog code that needed to be fixed. In the VHDL code there were only a couple of things that needed to be addressed - this was mostly due to VHDL's strong type system preventing many of the problems that showed up on the Verilog side.

Re:Giving Ada a Chance by the_only_law on Tuesday March 02, 2021 @07:28PM (#26323190)
I’ve been told Verilog is more predominant in the US and VHDL in the EU. Any idea how that happened?
Re:Giving Ada a Chance by UncleOxidant on Tuesday March 02, 2021 @09:41PM (#26324062)
Yeah, it's very weird that a language developed by the DoD ended up popular in the EU but much less so in the US. I'm not sure why that happened. I did do a stint in Italy about 16 years ago and they preferred VHDL there at that time as well, so it's not exactly a new phenomenon. I did a VHDL project for a US company last year so it's not like it's completely unheard of here. Another way to look at it is that there are fewer people who know VHDL so when you do find places that are doing VHDL projects there's less competition.
Re:Giving Ada a Chance by chowells on Tuesday March 02, 2021 @09:40PM (#26324059)
https://en.wikipedia.org/wiki/Refinement_type

That idea was neatly generalized 30 years ago. So expect it to start showing up as the hot new thing in another 20.

Re:Giving Ada a Chance by hasperdi on Tuesday March 02, 2021 @03:37PM (#26320295)
In JavaScript it is possible to define getter and setter method of an object. So this behaviour can be emulated by adding a validation in the setter method.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...

In Java too, don’t know about other languages though...

Re:Giving Ada a Chance by Jtsummers on Tuesday March 02, 2021 @03:52PM (#26320513)
Right, it's emulated but not universal or built-in. Nothing in JavaScript will enforce using getters/setters, it's 100% up to programmer discipline or adding a linter (if any linter has this as an option) to ensure both the creation of the accessors and their use.

The benefit of this in Ada is that it is universal, and it requires the programmer to do nothing special later on except to not turn off the runtime checks. Using that above example, this would be a runtime error:

  Reading : Integer := Get_A_Reading(...); -- In some run Reading gets 0 for some reason
  Temperature : OperatingTemp;

  ...
  Temperature := OperatingTemp'Value(Reading); --  runtime error in Ada if Reading is invalid
And because OperatingTemp is a distinct type, you never even have the option to do:

  Temperature := Reading; -- doesn't compile
Also, in most OO languages where you'd make temperature an internal, private variable you can prevent external users of the class from interacting with it incorrectly, but you can't control internal users. Consider this Java-ish example:

  class Thermostat {
    private int temperature; // should only be in [33,90]
    public void SetTemperature(int value) { // check range and set }
    public void AnotherMethod(int value) {
      // good discipline would be:
      SetTemperature(f(value));
      // but it's not strictly required so this is also allowed
      temperature = f(value);
    }
  }
Re:Giving Ada a Chance by goatinaboat on Tuesday March 02, 2021 @04:47PM (#26321349)
In JavaScript it is possible to define getter and setter method of an object

If a civil engineer had access to concrete but he chose to build with mashed potato instead he would be considered insane. Yet programmers with access to Ada choose JavaScript and it's considered perfectly normal.

Re:Giving Ada a Chance by n30phyte on Tuesday March 02, 2021 @03:43PM (#26320371)
Basically any language with objects and methods can do this
Re:Giving Ada a Chance by nradov on Tuesday March 02, 2021 @03:53PM (#26320537)
Sure, although in languages like Java that lack operator overloading the syntax gets messy.
Re:Giving Ada a Chance by Symmetry on Tuesday March 02, 2021 @10:07PM (#26324244)
It's not obvious from the syntax but Nim takes a fair amount of inspiration from Ada including this.
Re:Giving Ada a Chance by edflsafoiewq on Tuesday March 02, 2021 @06:40PM (#26322782)
If I have two OperatingTemps, can I add them? What is the type of the sum?
Re:Giving Ada a Chance by Jtsummers on Tuesday March 02, 2021 @07:01PM (#26322986)
Yes and it would be the same type but there is a risk of overflow (and underflow with subtraction) which could result in a runtime exception (same as the risk with conventional integers). You’d want to include a guard that prevented those results.