UDT construction as a proper way to Cast, not mentioned

Forum for discussion about the documentation project.
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: UDT construction as a proper way to Cast, not mentioned

Post by fxm »

When the Cast operator returns by reference, conversion can be bidirectional (but not implicit in the reverse direction):

Code: Select all

Type UDT
  Dim As Integer I
  Declare Operator Cast () Byref As Integer
End Type
Operator UDT.Cast () Byref As Integer
  Return This.I
End Operator

Dim As UDT u1 = (12)
Dim As Integer J
J = u1
Print J

Dim As UDT u2
J = 34
'u2 = J             '' error 180: Invalid assignment/conversion in 'u2 = J'
'u2 = Cast(UDT, J)  '' error 20: Type mismatch, before ')' in 'u2 = Cast(UDT, J)'
Cast(Integer, u2) = J
Print u2.I

Sleep
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: UDT construction as a proper way to Cast, not mentioned

Post by Tourist Trap »

fxm wrote:When the Cast operator returns by reference, conversion can be bidirectional (but not implicit in the reverse direction)
Unfortunately, if this is not documented, I dont know anyone even in the advanced users that would think of writting:
Cast(Integer, u2) = J
I myself wont be able to recall something that I don't find readable enough, or really hardly. If a macro could do:
InverseCast(u2, Integer)
based on Cast_byref, it would be a serious helper.

But I guess you are too much advanced to find interesting to mention the simplest facts that we meet first. But look again to this:

Code: Select all

type UDT
   as integer i
end type

dim as UDT u

u = Cast(UDT, 99)
The error is not "missing explicit operator cast...", it's "type mismatch". That doesn't help. If anyway the common beginner find the page on operator cast, he will get stuck immediately:

Code: Select all

declare operator Cast(as integer) as UDT 'doesn't exist and ignored
declare operator Cast() as ??     'nothing of this form will work for fixing the present case
declare operator Cast() byref as integer  'if could work, I'm not even sure despite example above, should be anyway very very well explained in detail , syntax is quite uncommon
So from the documentation point of view. First thing to complete seems to me to say clearly 2 things:
▪ at cast page, that there are a world of features dedicated to udts. So at present day this page is quite omitting a crucial information for no reason (the page is very small and wouldn't be flooded with a tiny remark about datatypes)
▪ at operator cast page. Should be also said clearly that Cast(UDT, var) requires a special treatment. As the example above showed, the need of operator cast starts even if the user doesn't have to use a complex udt. So for the user that is just interested on cast, he should find if possible the answer (return byref?) or be directed without delay to more stuff. For the moment, a beginner wont find the solution to the simple problem exposed above.

Maybe the fact that the documentation is in english is also why I dont really like to read more than the header with usage and syntax. That's why I would prefer that any question about datatype parameter of Cast would be mentioned there.

Maybe some feedback from other users would help however. For I'm no more able to read the doc page as a pure beginner when I read the explanations from the forum simultaneously. But before I posted, I was in a real life user situation. Question on syntax due to error, jump to doc. Answer not found so posting here. When I find the informations I generally won't ask for more (for the simple reason that I don't think of more, like advanced usage).
Cast page
By using a member Operator Cast, Cast can be overloaded for a user defined type expression.
I just have seen this more recent addition. Just a problem. What if the user don't identify its problem in term of overloading an operator of an udt (or he would already be reading operator cast). This could be clearer if saying that the datatypes divide in famillies, built-in and udt. The key problem is in the datatype limitation.
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: UDT construction as a proper way to Cast, not mentioned

Post by Tourist Trap »

fxm wrote:Example above added in documentation:
ProPgDataConversion → fxm [Added example of explicit/implicit conversions using UDT constructor and two operators]
(with links from TYPE, UNION, CONSTRUCTOR, OPERATOR, CAST, Operator Let (Assign), Operator =[>] (Assign), Operator CAST)
Just recall of the links in case of update.
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: UDT construction as a proper way to Cast, not mentioned

Post by fxm »

Tourist Trap wrote:
fxm wrote:When the Cast operator returns by reference, conversion can be bidirectional (but not implicit in the reverse direction)
Unfortunately, if this is not documented, I dont know anyone even in the advanced users that would think of writting:
Cast(Integer, u2) = J
This is only the general capability of a function that returns by reference (see BYREF (function results)), applied to the Cast operator which is a member function.
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: UDT construction as a proper way to Cast, not mentioned

Post by Tourist Trap »

fxm wrote: This is only the general capability of a function that returns by reference (see BYREF (function results)), applied to the Cast operator which is a member function.
The syntax of Cast is very special, so here inverting the assignment is breaking something. Is there a macro alternative possible? I can't pass a type as parameter. Is there a way?
Cast page
Useful to be used in macros when datatype is unknown and also when converting to Type Alias
[/quote]
Is it possible to illustrate by an example in the page?
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: UDT construction as a proper way to Cast, not mentioned

Post by fxm »

See the last update of the two pages:
CAST
Operator CAST
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: UDT construction as a proper way to Cast, not mentioned

Post by Tourist Trap »

fxm wrote:See the last update of the two pages:
CAST
Operator CAST
This is a nice improvement and big piece of work for the examples covering now most of cases (if not all - even the quite surprising return byref). Thank you.
The example for macro at cast page is difficult but at least it removes the mystery about how doing that. Macros are in general always a little cumbersome, so each time a working example is given, this is good.

I see nothing more to add from my side. Just maybe a question. I've seen some wikis that can fold/unfold definitions at clicking. Apparently this one can't, otherwise why not defining built-in types with a link to the whole list?
Built-in types list
Integer range ..... etc
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: UDT construction as a proper way to Cast, not mentioned

Post by fxm »

See also the last update of the page:
Coercion and Conversion
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: UDT construction as a proper way to Cast, not mentioned

Post by fxm »

Tourist Trap wrote:The example for macro at cast page is difficult but at least it removes the mystery about how doing that.
I could not propose a too obvious example of macro (which uses the keyword Cast) as following:

Code: Select all

#define casting(datatype, expression) cast(datatype, expression)
!!!
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: UDT construction as a proper way to Cast, not mentioned

Post by Tourist Trap »

fxm wrote:
Tourist Trap wrote:The example for macro at cast page is difficult but at least it removes the mystery about how doing that.
I could not propose a too obvious example of macro (which uses the keyword Cast) as following:

Code: Select all

#define casting(datatype, expression) cast(datatype, expression)
!!!
Yes. It's naturally difficult, that's why it was necessary to add it. Because before that the announcement at the top of the doc was really not giving any info.
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: UDT construction as a proper way to Cast, not mentioned

Post by Tourist Trap »

fxm wrote:See also the last update of the page:
Coercion and Conversion
Thats clear and useful .
there is a link to Standard data types. Those types are the built-in data types also. Is there an entry in the index as Built-in data types? This term is used in many pages so the user may search for this expression rather than "standard" in general.
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: UDT construction as a proper way to Cast, not mentioned

Post by fxm »

Seen that you are interested now in case the Cast result is returned by reference:
- you can write '@Cast(datatype, expression)'
- Cast can be used on the left side of an assignment expression

There are some cases where the implicit Cast returns also its result by reference:
- when converting any integer type between its signed version and its unsigned version
- between 'Integer' and 'Long' (for 32-bit) or 'Longint' (for 64-bit)
- in case of inheritance for converting a derived instance into a base instance

In an inheritance structure, the upcasting is implicit and it is sufficient to write:
'base_instance = derived_instance'
but you can use explicitly the Cast syntax:
'base_instance = Cast(base_type, derived_instance)'

In addition, this implicit Cast operator returns its result by reference, but not in an implicit way.
You cannot write:
'derived_instance = base_instance'
but it is mandatory to be explicit:
'Cast(base_type, derived_instance) = base_instance'

If now you define your own Cast operator in the derived type by overloading the implicit Cast, you lose the above last capability if your Cast operator returns its result only by value.
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Cast a generic

Post by Tourist Trap »

Woots!

@fxm, What a big work you've done here! It's really noticiable !

I've to add a new remark about something really limited but probably desserving a little room here to complete the picture.

I've discovered that we can do @UDT (where UDT is the generic type name not an instance). So I tried *@UDT associated to a... STATIC OPERATOR CAST.

Sadly enough it's impossible. It's not too much said directly that cast won't be static, but it's ok the documentation gives some hint by evocating non-static member access and the THIS parameter.

So the question is would it be any way to customize the value that we would get from *@UDT?
You know it could be casted to a string with a comment inside about the type usage, or more general information like the creation date or version... or whatever one can think of.
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Cast a generic

Post by fxm »

Tourist Trap wrote:I've discovered that we can do @UDT (where UDT is the generic type name not an instance).
Surprising!

Can you post a code example of using that '@UDT' which is not an instance?

From my point of view, '@UDT' is allowed only when the UDT has a default constructor, and it is a shortcut of '@UDT()' which represents the address of the temporary instance 'UDT()':

Code: Select all

Type UDT
  Dim As Integer I = 123
End Type

Print @UDT,        '' shortcut for @UDT()
Print (UDT).I

Print @Type<UDT>,  '' shortcut for @Type<UDT>()
Print Type<UDT>.I
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: Cast a generic

Post by Tourist Trap »

(mistyping)
Last edited by Tourist Trap on Nov 11, 2016 15:34, edited 1 time in total.
Post Reply