Class Reference Type Parameter With Default Value lazarus / 62-General ---------------------------------------------------------------------- simone · 2020-02-10 23:27 I'm surely missing some basic thing… I apologize in advance… Let suppose we want to use a procedure with a class reference type parameter having a default value, as in the following example: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program Project1;{$mode ObjFpc}type  C=class  end;   C1=class(C)  end;   C2=class(C1)  end;   Cl = class of C;   procedure Proc(aCl : Cl=C1); //<-- illegal expression  begin  end; beginend.  Why does the compiler says that there is an illegal expression in the definition of Proc? Thanks. ---------------------------------------------------------------------- PascalDragon · 2020-02-11 08:23 You can't. Only those values that are also allowed for untyped constants (ordinals, floating point values, strings, sets) are allowed as default values. You need to solve this using an overload: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure Proc(aCl: Cl);beginend; procedure Proc; inline;begin  Proc(C1);end; If you place the implementation of the overload without the parameter after the one with the parameter and the former has the inline modifier (like in the example above; for a unit you need to add the modifier to the declaration in the interface section) then you'll essentially have the same as only one call will be involved then. ---------------------------------------------------------------------- simone · 2020-02-11 08:31 Thanks PascalDragon.  Waiting for the forum reply I had used this workaround (but without the intelligent use of the inline modifier).  I didn't know this limitation of parameters with default values.  Thanks for the explanation. ---------------------------------------------------------------------- simone · 2020-02-11 12:40 Quote from: PascalDragon on February 11, 2020, 09:23:47 amOnly those values that are also allowed for untyped constants (ordinals, floating point values, strings, sets) are allowed as default values. As a side note:  It's possible to set a class reference type parameter with NIL as default value: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program Project1;{$mode ObjFpc}type  C=class  end;   C1=class(C)  end;   C2=class(C1)  end;   Cl = class of C;   procedure Proc(aCl : Cl=nil); //<-- OK  begin  end; beginend. ---------------------------------------------------------------------- jamie · 2020-02-11 21:40 Your first attempt failed because you tried to change the type to another type, even if it is the same. You can only define a default value not a default type.. C1 here is a type not a variable. The second attempted work because NIL is a value not a type which equals a zero pointer. ---------------------------------------------------------------------- simone · 2020-02-11 23:28 Thanks Jamie. Indeed the concept of class reference in Object Pascal is similar, but different, respect to the notion of metaclass (i.e. class whose instances are classes) available in other OOP languages. I was misled by this analogy... ---------------------------------------------------------------------- PascalDragon · 2020-02-12 08:24 Quote from: jamie on February 11, 2020, 10:40:32 pmYour first attempt failed because you tried to change the type to another type, even if it is the same. You can only define a default value not a default type.. C1 here is a type not a variable. The second attempted work because NIL is a value not a type which equals a zero pointer. Nonsense. For class types a class type is a valid value: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type  TMyObject = class  end;   TMyClass = class of TMyObject;   TMySubObject = class(TMyObject); var  MyClass: TMyClass = TMySubObject; This is one of the outstanding features of Object Pascal. It's only that default parameters have further restrictions that simone's code does not work. ---------------------------------------------------------------------- jamie · 2020-02-12 21:49 Of course you can do that but he was doing it within the parameter case call. different state all together.. ---------------------------------------------------------------------- PascalDragon · 2020-02-13 08:23 That's what I already said: that default parameters have further restrictions. But what you wrote was more general: Quote from: jamie on February 11, 2020, 10:40:32 pmYou can only define a default value not a default type.. To someone skimming over the posts it might not be clear that you're talking strictly about default parameters, thus my clarification.