On 20-02-18 14:58, Chris Angelico wrote: > On Wed, Feb 21, 2018 at 12:53 AM, Antoon Pardon <antoon.par...@vub.be> wrote: >> In C++ I can do something like: >> >> SomeClass MyVar; >> >> And after that the kind of possible assignments to MyVar are constraint. It >> makes the runtime throw an error when somewhere the program tries to assign >> something to MyVar that isn't allowed by SomeClass. >> >> You can't put such constraints on names in Python. >> >> In C++ I can do some like: >> Some_Class: MyVar; >> >> And after that, It will be impossible to assign a value to MyVar that >> doesn't meet the >> constraints imposed by the constructor/copy operator. You have put >> somekind of >> contract on the name MyVar, that limits the kind of things assignable to >> it. You >> can't put such constraints on a name in Python. > Okay. Now create a constraint on a name in C++ such that it can only > accept integers representing A.D. years which, on the Gregorian > calendar, are leap years. (Using a dedicated integer-like type is > permitted.) It must accept all multiples of four, except those which > are multiples of one hundred, unless they're also multiples of four > hundred. > > That's what Steve asked for. Can you do it? Or is the C++ type system > not flexible enough for that?
Steve had multiple contributions in this thread. I didn't react to the one where he asked for that. I reacted to his assertion that dynamic languages excell at [run time checks] as if static languages are somehow limited at run time checks. writing a Digit Class in a dynamic language that checks whether the number you are about to assign is a digit, doesn't strike me as that much different from just writing a function that does the same kind of checking. Plus, in a statically typed language you can put constraints on the variables that will ensure the compilor will implicitly call the needed functions to check (because they are part of the constructor/copy operator) which you can't do in a dynamically typed language. So IMO the statically typed languages are at the advantage here. -- Antoon. -- https://mail.python.org/mailman/listinfo/python-list