The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Clownfish::Type - A variable's type.

METHODS

new

    my $type = MyType->new(
        specifier   => 'char',    # default undef
        indirection => undef,     # default 0
        const       => 1,         # default undef
        parcel      => undef,     # default undef
        c_string    => undef,     # default undef
    );

Generic constructor.

  • specifier - The C name for the type, not including any indirection or array subscripts.

  • indirection - integer indicating level of indirection. Example: the C type "float**" has a specifier of "float" and indirection 2.

  • const - should be true if the type is const.

  • parcel - A Clownfish::Parcel or a parcel name.

  • c_string - The C representation of the type.

new_integer

    my $type = Clownfish::Type->new_integer(
        const     => 1,       # default: undef
        specifier => 'char',  # required
    );

Return a Type representing an integer primitive.

Support is limited to a subset of the standard C integer types:

    int8_t
    int16_t
    int32_t
    int64_t
    uint8_t
    uint16_t
    uint32_t
    uint64_t
    char
    short
    int
    long
    size_t

Many others are not supported: "signed" or "unsigned" anything, "long long", "ptrdiff_t", "off_t", etc.

The following Charmonizer typedefs are supported:

    bool_t
  • const - Should be true if the type is const.

  • specifier - Must match one of the supported types.

new_float

    my $type = Clownfish::Type->new_float(
        const     => 1,           # default: undef
        specifier => 'double',    # required
    );

Return a Type representing a floating point primitive.

Two specifiers are supported:

    float
    double
  • const - Should be true if the type is const.

  • specifier - Must match one of the supported types.

new_composite

    my $type = Clownfish::Type->new_composite(
        child       => $char_type,    # required
        indirection => undef,         # default 0
        array       => '[]',          # default undef,
        const       => 1,             # default undef
    );

Constructor for a composite type which is made up of repetitions of a single, uniform subtype.

  • child - The Type which the composite is comprised of.

  • indirection - integer indicating level of indirection. Example: the C type "float**" has indirection 2.

  • array - A string describing an array postfix.

  • const - should be 1 if the type is const.

new_object

    my $type = Clownfish::Type->new_object(
        specifier   => "Lobster",       # required
        parcel      => "Crustacean",    # default: the default Parcel.
        const       => undef,           # default undef
        indirection => 1,               # default 1
        incremented => 1,               # default 0
        decremented => 0,               # default 0
        nullable    => 1,               # default 0
    );

Create a Type representing an object. The Type's specifier must match the last component of the class name -- i.e. for the class "Crustacean::Lobster" it must be "Lobster".

  • specifier - Required. Must follow the rules for Clownfish::Class class name components.

  • parcel - A Clownfish::Parcel or a parcel name.

  • const - Should be true if the Type is const. Note that this refers to the object itself and not the pointer.

  • indirection - Level of indirection. Must be 1 if supplied.

  • incremented - Indicate whether the caller must take responsibility for an added refcount.

  • decremented - Indicate whether the caller must account for for a refcount decrement.

  • nullable - Indicate whether the object specified by this type may be NULL.

The Parcel's prefix will be prepended to the specifier by new_object().

new_void

    my $type = Clownfish::Type->new_void(
        specifier => 'void',    # default: void
        const     => 1,         # default: undef
    );

Return a Type representing a the 'void' keyword in C. It can be used either for a void return type, or in conjuction with with new_composite() to support the void* opaque pointer type.

  • specifier - Must be "void" if supplied.

  • const - Should be true if the type is const. (Useful in the context of const void*).

new_va_list

    my $type = Clownfish::Type->new_va_list(
        specifier => 'va_list',    # default: va_list
    );

Create a Type representing C's va_list, from stdarg.h.

  • specifier. Must be "va_list" if supplied.

new_arbitrary

    my $type = Clownfish::Type->new_arbitrary(
        specifier => 'floatint_t',    # required
        parcel    => 'Crustacean',    # default: undef
    );

"Arbitrary" types are a hack that spares us from having to support C types with complex declaration syntaxes -- such as unions, structs, enums, or function pointers -- from within Clownfish itself.

The only constraint is that the specifier must end in "_t". This allows us to create complex types in a C header file...

    typedef union { float f; int i; } floatint_t;

... pound-include the C header, then use the resulting typedef in a Clownfish header file and have it parse as an "arbitrary" type.

    floatint_t floatint;
  • specifier - The name of the type, which must end in "_t".

  • parcel - A Clownfish::Parcel or a parcel name.

If parcel is supplied and specifier begins with a capital letter, the Parcel's prefix will be prepended to the specifier:

    foo_t         -> foo_t                # no prefix prepending
    Lobster_foo_t -> crust_Lobster_foo_t  # prefix prepended

equals

    do_stuff() if $type->equals($other);

Returns true if two Clownfish::Type objects are equivalent.

similar

    do_stuff() if $type->similar($other_type);

Weak checking of type which allows for covariant return types. Calling this method on anything other than an object type is an error.

to_c

    # Declare variable "foo".
    print $type->to_c . " foo;\n";

Return the C representation of the type.

set_c_string

Set the C representation of the type.

get_specifier get_parcel get_indirection get_array const nullable set_specifier set_nullable

Accessors.

is_object is_primitive is_integer is_floating is_composite is_void

    do_stuff() if $type->is_object;

Identify the flavor of Type, which is determined by the constructor which was used to create it.

  • is_object: Clownfish::Type->new_object

  • is_primitive: Either Clownfish::Type->new_integer or Clownfish::Type->new_float

  • is_integer: Clownfish::Type->new_integer

  • is_floating: Clownfish::Type->new_float

  • is_void: Clownfish::Type->new_void

  • is_composite: Clownfish::Type->new_composite

is_string_type

Returns true if $type represents a Clownfish type which holds unicode strings.

incremented

Returns true if the Type is incremented. Only applicable to object Types.

decremented

Returns true if the Type is decremented. Only applicable to object Types.