[RESOLVED] Why am I required to implement set_XX when the attribute is writeonly

Started by Joacim Andersson, Sat 23/11/2024 05:34:33

Previous topic - Next topic

Joacim Andersson

EDIT: I'm being stupid, read my own reply below.

I've been writing some test code to learn how AGS implements OOP. So my very first struct in AGS looks like this:
Code: ags
struct List
{
  protected int arrSize;
  protected String list[];
  protected int count;
  import writeprotected attribute int Count;
  import attribute String Items[];
  import int Add(String value);
  import function Remove(int idx);
  import int get_Count();
  protected import function set_Count();
  import String geti_Items(int idx);
  import function seti_Items(int idx, String value);
  protected import function ExpandArray();
};
Obviously, it's a try to implement a Collection object in AGS that is a bit more dynamic than to work with arrays (the code works fine BTW).

My question is why I'm required to implement the set_Count() function for a writeonly attribute, I understand that within the struct I can use this.Count but in my case I only use the count (with lowercase letters) to change the Count property. In my opinion, the set_Count() function should be voluntary to implement, AGS should be smart enough to just throw a runtime error if I ever use this.Count = someValue in my code if I haven't implemented the set_Count() function.

Another question related to my implementation above: This List struct only accepts strings. Is there any generic type in AGS that can accept any value, either a number or a String, such as an object (and I'm talking about the type here, not Room Object)?

Joacim Andersson

I'm soooo sorry, I'm being stupid, AGS also have a readonly, that's obviously the one I should use.

My second question remains though, do AGS have a generic data type?

Crimson Wizard

Quote from: Joacim Andersson on Sat 23/11/2024 05:44:13My second question remains though, do AGS have a generic data type?

No, that's one of the big limitations of this script.
It has no generic data type, no C#-like generic functions, nor C++ like templates.

It does support extending a struct, and up-casting from child to parent struct pointer (if these structs are declared as "managed"), but does not support down-casting at the same time (latter may be added in AGS 4 in theory, where we implemented some form of runtime type detection).

Joacim Andersson

EDIT: Duh, I can't even read today, you said there were no generic data type, sorry. Please ignore this.

I wasn't referring to generics as in C# or C++ templates, I meant more like a generic or variant type, like System.Object in .Net

SMF spam blocked by CleanTalk