Sign in Join

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

Mailing lists / fpc-pascal / 2012-11

Is it possible to redefine FreePascal types?

8 messages · 7 people · –

Reply by email Subscribe to this list Your subscription

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

Frank Church

Lets say I am using some string variables, but I want to ensure that there are no typing errors in some code I am using.
so I define newString as a new string type and declare a variable as ANewString.

That way if I create a function such as functionUsingNewString(a: newString) and I pass a plain string as a parameter to functionNewString the compiler declares an error.

If c is string and b is newString and I do b := c the compiler should
generate an error unless I do b := newString(c);

Is this possible?

Frank Church ======================= http://devblog.brahmancreations.com

Reply by email

Ewald
to Frank Church

Once upon a time, on 11/21/2012 08:37 PM to be precise, Frank Church said:

Show quoted text 13 lines
Lets say I am using some string variables, but I want to ensure that there are no typing errors in some code I am using. so I define newString as a new string type and declare a variable as ANewString. That way if I create a function such as functionUsingNewString(a: newString) and I pass a plain string as a parameter to functionNewString the compiler declares an error. If c is string and b is newString and I do b := c the compiler should generate an error unless I do b := newString(c); Is this possible?

If you declare something like

Type
    NewString = Record
        Data: Array of Char;
    End;
And a function:
    Function ToNewString(a: String): NewString;
    Begin
        SetLength(Result.Data, Length(a));
        Move(a[1], Result.Data[0], Length(a) * SizeOf(Char));
    End;

; The compiler should generate an error when you try to typecast a String to a NewString.

Ewald

Reply by email

Kenneth Cochran
to Frank Church: Lets say I am using some string variables, but I want to ensure that there are no typing…

Yes.

type newString = type string;

Any function that accepts newString as a parameter will cause a compilation error if you try to pass a normal string. You'll have to cast it to newString first.

Frank Church wrote 24 lines
Lets say I am using some string variables, but I want to ensure that there are no typing errors in some code I am using. so I define newString as a new string type and declare a variable as ANewString. That way if I create a function such as functionUsingNewString(a: newString) and I pass a plain string as a parameter to functionNewString the compiler declares an error. If c is string and b is newString and I do b := c the compiler should generate an error unless I do b := newString(c); Is this possible? -- Frank Church ======================= http://devblog.brahmancreations.com _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply by email

Howard Page-Clark
to Kenneth Cochran
Kenneth Cochran wrote 7 lines
Yes. type newString = type string; Any function that accepts newString as a parameter will cause a compilation error if you try to pass a normal string. You'll have to cast it to newString first.

This is not the case, as the following program demonstrates. The compiler does not even blink.

program Project1;
{$mode objfpc}{$H+}
type TnewString = type string;
function NewStringFn(ns: TnewString): integer;
var c: Char;
begin
   Result := 0;
   for c in ns do
    if (c#0) then inc(Result);
end;
var s:string = 'ansistring';
     ns:TnewString = 'new string';
begin
   writeln('The new string length is ',NewStringFn(ns));
   writeln('The old string length is ',NewStringFn(s));
end.

Reply by email

Graeme Geldenhuys
to Kenneth Cochran: Yes. type newString = type string; Any function that accepts newString as a parameter…
Kenneth Cochran wrote
Any function that accepts newString as a parameter will cause a compilation error if you try to pass a normal string. You'll have to cast it to newString first.

That is an incorrect assumptions. The compiler will happily accept both types without needing typecasting.

Regards,
- Graeme -

fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://fpgui.sourceforge.net/

Reply by email

Kenneth Cochran
to Graeme Geldenhuys
Graeme Geldenhuys wrote
That is an incorrect assumptions. The compiler will happily accept both types without needing typecasting.

I stand corrected. I assumed it behaved the same as Delphi in this respect. It doesn't, not even when Delphi mode is enabled.

Reply by email

Jonas Maebe
to Kenneth Cochran
Kenneth Cochran wrote 10 lines
On Wed, Nov 21, 2012 at 5:34 PM, Graeme Geldenhuys wrote: > That is an incorrect assumptions. The compiler will happily accept > both > types without needing typecasting. I stand corrected. I assumed it behaved the same as Delphi in this respect. It doesn't, not even when Delphi mode is enabled.

Delphi behaves like FPC, at least Kylix 3 does. Afaik the only use for "type newstring = type string;" is so that you can declare overloaded routines that are differentiated based on those two types.

Jonas

Reply by email

Mattias Gaertner
to Jonas Maebe

Jonas Maebe hat am 22. November 2012 um 10:03
geschrieben:

Show quoted text 17 lines
On 22 Nov 2012, at 02:00, Kenneth Cochran wrote: > On Wed, Nov 21, 2012 at 5:34 PM, Graeme Geldenhuys > wrote: > >> That is an incorrect assumptions. The compiler will happily accept >> both >> types without needing typecasting. > > I stand corrected. I assumed it behaved the same as Delphi in this > respect. > It doesn't, not even when Delphi mode is enabled. Delphi behaves like FPC, at least Kylix 3 does. Afaik the only use for "type newstring = type string;" is so that you can declare overloaded routines that are differentiated based on those two types.

Yes, for example you can ask the type via RTTI. For instance the LCL uses type TTranslateString = string for all string properties that should be auto translated.

Mattias

Reply by email