Recent

Author Topic: Extending write and str  (Read 4740 times)

BeniBela

  • Hero Member
  • *****
  • Posts: 906
    • homepage
Extending write and str
« on: February 09, 2015, 09:28:07 pm »
Is it possible to extend write or str with new types?

So you can write:

Code: [Select]
  var x: TMyType;
  writeln(x:1:3);

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Extending write and str
« Reply #1 on: February 10, 2015, 04:26:23 am »
Is it possible to extend write or str with new types?

So you can write:

Code: [Select]
  var x: TMyType;
  writeln(x:1:3);
No. Note that Write[Ln] is mapped to compilerproc for the type of every given expression (argument). So, for:
Code: [Select]
WriteLn(1,'str');
it will be translated to:
Code: [Select]
fpc_writetext_shortint(1);
fpc_writetext_shortstr('str');
fpc_writeln_end;
real name might differ, but the concept is the same. Use fpc -al to know the actual translation.

BeniBela

  • Hero Member
  • *****
  • Posts: 906
    • homepage
Re: Extending write and str
« Reply #2 on: February 10, 2015, 01:23:40 pm »

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11453
  • FPC developer.
Re: Extending write and str
« Reply #3 on: February 10, 2015, 01:25:31 pm »
Maybe classes can be made writeln()able due to their Tobject.Tostring method. I don't know if that is implemented though (in both FPC and Delphi)

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Extending write and str
« Reply #4 on: February 10, 2015, 04:47:47 pm »
Maybe classes can be made writeln()able due to their Tobject.Tostring method. I don't know if that is implemented though (in both FPC and Delphi)
Now that's something that FPC could consider. It's been the concept in Java, C# and some other functional / pure oo languages. I prefer something that can be used on non-class types as well, though.

A.S.

  • Jr. Member
  • **
  • Posts: 76
Re: Extending write and str
« Reply #5 on: February 10, 2015, 05:08:03 pm »
Isn't it possible to declare an implicit type cast to string? Maybe record/type helper?

BeniBela

  • Hero Member
  • *****
  • Posts: 906
    • homepage
Re: Extending write and str
« Reply #6 on: February 10, 2015, 06:25:36 pm »
Even if toString would work, it cannot know the decimal formatting arguments

 

TinyPortal © 2005-2018