Custom types

Share your advanced PureBasic knowledge/code with the community.
Joubarbe
Enthusiast
Enthusiast
Posts: 710
Joined: Wed Sep 18, 2013 11:54 am
Location: France

Custom types

Post by Joubarbe »

So you might not know that, but you can name your enumerations:

Code: Select all

Enumeration CUSTOM_TYPE
  #CUSTOM_TYPE_1
  #CUSTOM_TYPE_2
EndEnumeration
But unlike some other languages, you cannot use that as a custom type, ie. you cannot force values to be either #CUSTOM_TYPE_1 or #CUSTOM_TYPE_2. However, you can use macros to do that:

Code: Select all

Enumeration CUSTOM_TYPE
  #CUSTOM_TYPE_1
  #CUSTOM_TYPE_2
EndEnumeration
Macro CUSTOM_TYPE : i : EndMacro

Structure _Struc
  int.i
  custom.CUSTOM_TYPE
EndStructure

Procedure WriteCustom(custom.CUSTOM_TYPE)
  ; Something.
EndProcedure
Which I find pretty cool. When you have a big program with a ton of constants, you can easily identify what constants you need to use when you call a function or when you assign a value to a structured variable.
User avatar
idle
Always Here
Always Here
Posts: 5889
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Custom types

Post by idle »

thanks it's a good tip and is useful when importing large c libs
infratec
Always Here
Always Here
Posts: 7613
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Custom types

Post by infratec »

:mrgreen: :mrgreen: :mrgreen:

I use this since ... a long time ago,
for C imports:

Code: Select all

Enumeration pjsip_inv_state
  #PJSIP_INV_STATE_NULL           ; Before INVITE is sent Or received
  #PJSIP_INV_STATE_CALLING        ; After INVITE is sent
  #PJSIP_INV_STATE_INCOMING       ; After INVITE is received.
  #PJSIP_INV_STATE_EARLY          ; After response With To tag.
  #PJSIP_INV_STATE_CONNECTING     ; After 2xx is sent/received.
  #PJSIP_INV_STATE_CONFIRMED      ; After ACK is sent/received.
  #PJSIP_INV_STATE_DISCONNECTED   ; Session is terminated.
EndEnumeration

Macro pjsip_inv_state
  l
EndMacro
:wink:
ebs
Enthusiast
Enthusiast
Posts: 561
Joined: Fri Apr 25, 2003 11:08 pm

Re: Custom types

Post by ebs »

Thank you - this is really clever!

As you said, it doesn't enforce the use of the correct type, but it makes your code much more self-documenting.
It makes the C programmer in me very happy! :D
Axolotl
Addict
Addict
Posts: 832
Joined: Wed Dec 31, 2008 3:36 pm

Re: Custom types

Post by Axolotl »

Thanks for sharing.
Indeed Marcos are very helpful as workaround solutions.
Fun Fact: I recently made a wish. Enums as a Type
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
Axolotl
Addict
Addict
Posts: 832
Joined: Wed Dec 31, 2008 3:36 pm

Re: Custom types

Post by Axolotl »

infratec wrote: Thu Jul 31, 2025 6:53 am ...
I use this since ... a long time ago,
for C imports:

Code: Select all

Enumeration pjsip_inv_state
....
EndEnumeration
@infratec, is your code published in the forum?
The search function is a mess these days.
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
infratec
Always Here
Always Here
Posts: 7613
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Custom types

Post by infratec »

No, this code is not public.
And I'm definately not allowed to make this public.
Post Reply