Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2020-11

Generics in Objective Pascal?

4 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Zoë Peterson

I'd like to have a generic objcclass that would add some extra functionality for multiple base types.  I don't have a lot of experience with generics, but this compiles:
------
generic TMyControl<T: TWinControl> = class(T)
  private
    FField: string;
procedure Foo;
  end;
  TMyEdit = specialize TMyControl<TEdit>;
------

and this fails with the error 'Class or interface type expected, but got "NSView"':
------
  generic NSMyView<T: NSView> = objcclass(T)
  private
    FField: string;
    procedure Foo; message 'Foo:';
  end;
------

The documentation doesn't mention it, so I'm assuming it's just not implemented.  Is that something that could be supported?  Would it be difficult to do so?  Are there any existing Objective Pascal specific tricks I could use to avoid duplicating all of the fields/code? Thanks!
Zoë Peterson
Scooter Software

Reply by email

Ryan Joseph
to Zoë Peterson
On Nov 1, 2020, at 8:45 AM, Zoë Peterson via fpc-pascal wrote: I'd like to have a generic objcclass that would add some extra functionality for multiple base types. I don't have a lot of experience with generics, but this compiles:

Waiting for Jonas to reply but I'm pretty certain this is a feature never implemented. I've been working with generics in the compiler recently and I don't see any reason why they would be hard to implement but maybe Jonas knows the answer.

What are you trying to do exactly? There are categories in ObjC but those don't allow fields (they're like helpers in Object Pascal). If you need to share data between a bunch of different NSView subclasses then all you can do is make a common subclass or make the shared data a class of its own and pass that into all your subclasses. Nothing ObjC related about that however but I'm not sure what you're trying to do.

Regards,
Ryan Joseph

Reply by email

Zoë Peterson
to Ryan Joseph
Ryan Joseph wrote
What are you trying to do exactly? There are categories in ObjC but those don't allow fields (they're like helpers in Object Pascal). If you need to share data between a bunch of different NSView subclasses then all you can do is make a common subclass or make the shared data a class of its own and pass that into all your subclasses. Nothing ObjC related about that however but I'm not sure what you're trying to do.

I have an NSView subclass with a bunch of fields and methods, and I wanted to be able to optionally swap that out with a parallel version derived from NSVisualEffectView without duplicating all of the code. I was hoping that adding generics to objcclass would be relatively trivial and just hadn't been asked for, but I know how assuming that usually goes.

More specifically, under LCL/Cocoa, TCustomControl's Handle uses a TCocoaManualScrollView(NSView) (in
lcl/interfaces/cocoa/cocoascrollers.pas) that has a bunch of code to interact with the LCL objects. When running recent versions of macOS in dark mode, custom controls like TTreeView actually use a behind-the-window blur rather than a plain clWindow FillRect. Since a lot of the TCocoaManualScrollView interaction is just general NSView messages or LCL protocol/category extensions, I figured I could keep the changes fairly small if I was able to just swap in a TCocoaManualScrollView<NSVisualEffectView>, which would avoid introducing another view in the hierarchy, keep the code cleaner, and be easier to disable if MACOS_MINIMUM_DEPLOYMENT_TARGET < 10.10.

I should be able to make it work with composition and re-parenting things when necessary, but there will be a lot more management code, so I thought I'd ask.

Zoë Peterson Scooter Software

Reply by email

Ryan Joseph
to Zoë Peterson
On Nov 2, 2020, at 1:30 PM, Zoë Peterson via fpc-pascal wrote: I have an NSView subclass with a bunch of fields and methods, and I wanted to be able to optionally swap that out with a parallel version derived from NSVisualEffectView without duplicating all of the code. I was hoping that adding generics to objcclass would be relatively trivial and just hadn't been asked for, but I know how assuming that usually goes.

Hmmm It sounds like once again Pascal needs "traits" or some other way to do composition? I'm extremely keep to develop such feature if the team wouuld decide on some syntax and principles (I had a thread about this some months ago if you search).

In mean time I guess you could try include files and ifdefs in some clever way. Not pretty but maybe it's better then keeping lots of duplicated code or doing big refactors.

Regards,
Ryan Joseph

Reply by email