Skip to content

Conversation

@jayrm
Copy link
Member

@jayrm jayrm commented Jun 2, 2019

Allow a user defined type (UDT) to inherit properties and behaviours of a zstring or wstring by extending existing syntax. Purpose is to allow users to create custom string types (i.e. dynamic memory management) that can integrate well in to existing fbc compiler built ins.

New Syntax:

type UDT extends zstring|wstring [, base-type]
end type

In places where fbc compiler would have normally rejected a UDT, this declaration of a UDT will instruct compiler to convert the UDT to a z|wstring through a suitable CAST operator.

zstring|wstring behaviour can be inherited directly from zstring|wstring, or indirectly and singly from the base type:

type B extends wstring
	'' member data
	'' inherits from wstring
end type

type D extends B
	'' D inherits from B and wstring
end type

Or zstring|wstring behaviours can also be inherited in a UDT with a kind of pseudo multiple-inheritance:

type B
	'' member data
end type

type D extends wstring, B
	'' D inherits from B and wstring
end type

Changed:

  • SADD/STRPTR(wstring) returns WSTRING PTR

Added:

  • 'TYPE udt EXTENDS Z|WSTRING' allowed to specify that UDT is a kind of Z|WSTRING
  • LTRIM/RTRIM/TRIM will accept UDT as Z|WSTRING
  • LCASE/UCASE will accept UDT as Z|WSTRING
  • Cxxx() conversion functions will accept UDT as Z|WSTRING
  • INSTR/INSTRREV will accept UDT as Z|WSTRING
  • MID function will accept UDT as Z|WSTRING
  • SADD/STRPTR will accept UDT as Z|WSTRING to return Z|WSTRING ptr
  • LSET/RSET statements will accept UDT as Z|WSTRING
  • MID statement will accept UDT as Z|WSTRING
  • ASC function will accept UDT as Z|WSTRING
  • STR/WSTR function will accept UDT as Z|WSTRING to return a Z|WSTRING

The only change to existing behaviour is SADD/STRPTR(wstring) now returns WSTRING PTR. This will break any user code where STRPTR(wstring) is expected to return a ZSTRING PTR

New implicit conversions and behaviours are activated only if a UDT extends zstring or wstring.

In future expect separate pull requests for:

  • LEFT, RIGHT to accept UDT extends z|wstring without having to explicitly overload LEFT, RIGHT. This involves fixing, in my opinion multiple string related bugs on sf.net
  • IIF to allow handling UDT's that inherit zstring|wstring
  • SELECT CASE to allow handling UDT's that inherit zstring|wstring
  • SWAP which maybe would be handled best with an overloaded member function rather than implicit conversion to zstring|wstring

This pull request introduces the initial feature and handles nearly all the quirk keywords. I'd like to merge this change in before working on the remaining features in separate pull requests.

Forum discussion at Extending Wstring and Zstring with UDTs

@jayrm jayrm mentioned this pull request Jun 2, 2019
jayrm added 12 commits June 9, 2019 11:55
- intent is that a UDT can extend the built in zstring and wstring types
- add tests/udt-wstring directory
- add tests/udt-zstring directory
- add astTryConvertUdtToWstring() to cast UDT to z|wstring if the UDT has a cast to z|wstring operator
- add symbGetIsUdtZstring() and symbGetIsUdtWstring() to detect if UDT extends z|wstring
- Cxxx() conversion functions will accept UDT as Z|WSTRING
- SADD/STRPTR will accept UDT as Z|WSTRING to return Z|WSTRING ptr
- SADD/STRPTR(wstring) returns WSTRING PTR
- type can extend Z|WSTRING and another user defined type (pseudo multiple inheritance for z|wstring only)
- extended types inherit Z|WSTRING if the base type also extended Z|WSTRING
@jayrm
Copy link
Member Author

jayrm commented Jun 16, 2019

Force push to rebase on current fbc/master

@jayrm
Copy link
Member Author

jayrm commented Jun 16, 2019

This pull request now also includes additions for SWAP, IIF, and SELECT. to accept a UDT extends Z|WSTRING.

Existing user code is not affected by this pull request, and I plan to continue support for the feature in future. On this basis I believe it acceptable to merge in to master without a review.

@jayrm jayrm merged commit 5dcb9e2 into freebasic:master Jun 16, 2019
@dkl
Copy link
Member

dkl commented Jun 17, 2019

Sounds good.
Since you're currently pretty much the only person working on the project, reviews don't make much sense (no point waiting for reviews if nobody is doing it). I try to stay up-to-date here or there, but then again, for me it was usually "whoever makes the change gets to decide" and so I've been trying to not comment too much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants