Forum > Suggestions

Custom type for Nil

<< < (9/10) > >>

VisualLab:

--- Quote from: AlexTP on March 15, 2022, 03:36:01 pm ---
--- Quote ---useful in Object Pascal to be able to define generics in such a way that it would allow to restrict them with the use of simple types as well.
--- End quote ---

Suggestion for a possible syntax here:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---  generic TMy<T: (integer or dword or qword or word)> = ...   generic TMy<T: (integer / dword / qword / word)> = ... 
--- End quote ---

I was thinking about something like:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---  generic TMy<T: number)> = class... // any numbers: integer or real   generic TMy<T: integer> = class... // any integer numbers: Byte, ShortInt, etc.   generic TMy<T: real> = class... // any real numbers: Single, Double, etc.   generic TMy<T: enumeration> = class... // any enumerated types 
But the example with a list of allowed types has the advantage of allowing the programmer to only allow specific types. In my example, an entire set of types would be acceptable. Which can sometimes be a disadvantage. Either way, even if it is possible in a compiler, it is imperative to think carefully about the notation of this constraint.

Warfley:

--- Quote from: VisualLab on March 15, 2022, 04:08:45 pm ---This is probably a bad example. It is the TList class (and many other containers) that uses arrays internally. Have there been any attempts to implement tuples in ObjectPascal? I mean class (container).

--- End quote ---
The thing about tuples is that every element can potentially be of a different type, so a simple container doesn't help. I ended up building this: https://github.com/Warfley/Recutils/blob/master/tuple/tuple.pas which provides tuples from 2-5 elements, which is enough for me. Still, a language based solution would of course be much cleaner


--- Quote from: VisualLab on March 15, 2022, 04:08:45 pm ---And the reason to create a translator is not a coincidence that building a decent compiler from scratch is a lot of work to be done. It seems to me that this is probably why the "Haskel -> C" translator was created. Anyway, there are quite a few differences between Haskell (practically purely functional) and C (old-date imperative). In any case, the general concepts seem to be significantly different in the two languages.

--- End quote ---
Yes it is of course very different and you cannot take concepts in a one to one fashion, but you can combine those, most modern languages have some functional elements. Java has lambdas, pattern matching and the stream library, which emulates functional list behavior. Python has lambdas, list comprehension, pattern matching, lazy evaluation (through generators) allowing for functional style lists and expression caching (as opt-in on a function specific level), C++ has lambdas and provides also utilities to handle containers like functional style lists. Tuples are also originally from functional languages as far as I know

You cannot take any functional element verbatim due to the inherent differences (e.g. functional functions are always stateless, meaning no such things as global or static variables, class members, etc) but you can take functional ideas and bring them to imperative language, but you of course need to change them a little bit or restrict them. But it can be done, and is done in pretty much all languages over the past few years. Even Delphi got with it's (horrible) lambda functions a piece of the functional cake

SymbolicFrank:
If you want the ultimate freedom in making classes, do it the JavaScript way: make a copy of the source code of an instance (that includes everything from the superclasses as well, unless overridden). That way, you can parse it yourself. I guess that is more or less what C++ does as well, for each class (not all together at the same time), during compile time.

But if you want to do that, please do the rest of us a favor and fork your language. :)

PascalDragon:

--- Quote from: VisualLab on March 15, 2022, 02:48:47 pm ---After a while, ObjectPascal will turn into a monster that even the most hardened Pascal followers will not want to use. It will be bloated by an absurd number of features that hardly anyone will be able to grasp. We will get a language worse than PL/I with C++ combined.

--- End quote ---

That's why we're very conservative with new features.


--- Quote from: Warfley on March 15, 2022, 02:44:40 pm ---I personally think that it would be really nice if you could just write:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---list.Sort(lambda A, B: A.ID - B.ID);and save having to declare an extra function for this one liner

PS: I think the delphi lambda expressions are terrible, but due to compatibility it is probably what will be implemented, so the syntax will not be so nice as shown above

--- End quote ---

In my opinion lambda A, B as A.ID - B.ID is more Pascal (and that is a syntax I had already thought about adding once anonymous functions are ready, but other devs don't agree here). However the problem is that this requires type inference and that is a concept that as such does not exist in Pascal or the compiler, thus making this a rather complex functionality.


--- Quote from: Warfley on March 15, 2022, 03:14:50 pm ---
--- Quote from: VisualLab on March 15, 2022, 02:48:47 pm ---4. and 5. I do not think it is appropriate to follow the fashion (sheep rush) of stuffing the features of functional languages into imperative languages. Most of these features are so exotic that they will only create a mess. Won't get anything better. It's like giving a fish a bicycle.
--- End quote ---
Using operators as functions is not necessarily a functional feature. Operators are functions already, they are declared as functions, are handled by the compiler as functions and in delphi they even have names. Using them as functions (note it must not be the syntax haskell introduces) is just natural.
About fixing operators, Pascal already does that with methods. This fixes the first parameter to be a certain object. So it is not even a new concept
--- End quote ---

Please be clear here: operator overloads are functions. Operators themselves are not. Assuming you would assign a builtin operator the compiler would need to create a function around it.

Also I don't know whether this would be some code I'd like to read:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---TList<Integer>.Combine(list1, list2, +);// orspecialize TList<Integer>.Combine(list1, list2, @+);

--- Quote from: VisualLab on March 15, 2022, 03:32:30 pm ---I generally agree that new features should be added to the language. For example, for me it would be useful in Object Pascal to be able to define generics in such a way that it would allow to restrict them with the use of simple types as well. For example, I create a class, record, or functions that accept only numbers (integers and real) or only integers. It would be good for my private library of numerical and statistical calculations (it's in its infancy for now). And if this type constraint is feasible, it is probably only with "compiler magic".

--- End quote ---

Extending generic constraints is something we can talk about and I personally am open about (cause I had thought about this as well). Though please, discuss this in a new thread.

Warfley:

--- Quote from: PascalDragon on March 16, 2022, 08:33:30 am ---In my opinion lambda A, B as A.ID - B.ID is more Pascal (and that is a syntax I had already thought about adding once anonymous functions are ready, but other devs don't agree here). However the problem is that this requires type inference and that is a concept that as such does not exist in Pascal or the compiler, thus making this a rather complex functionality.

--- End quote ---
Already thought that this might pose a problem, but such a feature might also be useful for generics, to deduce generic types:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---generic function foo<T>(val: T);... foo<>(24); // instead of foo<Integer>(24)

--- Quote from: PascalDragon on March 16, 2022, 08:33:30 am ---Also I don't know whether this would be some code I'd like to read:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---TList<Integer>.Combine(list1, list2, +);// orspecialize TList<Integer>.Combine(list1, list2, @+);
--- End quote ---
I thought more about something like

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---TList<Integer>.Combine(list1, list2, operator Integer.Add);// orTList<Integer>.Combine(list1, list2, operator Add);Or something similar. I actually like wordy syntax more than just throwing symbols there, and at least for mode delphi the operators already have names (this would also make disambigouagtion between binary and unary operator - easier)

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version