c3-web icon indicating copy to clipboard operation
c3-web copied to clipboard

Missing/nice to have info about Macros

Open tomaskallup opened this issue 1 year ago • 4 comments

I would like to slowly compose a list of stuff that I bump into while playing around with macros, so we can later on improve the docs.

Working with $typeof

  • I found it a bit difficult to find what is available on the result of $typeof, currently it's listed on top of the reflection page. The documentation for $typeof should ideally mention that the attributes are at the top.
  • nameof missing from the attributes

Working with $membersof

  • .get should be mentioned in the docs

tomaskallup avatar Oct 03 '24 12:10 tomaskallup

Add an example for working with macros and generics

macro @test_list(a)
{
  var $Type = $typeof(a);
  $if $defined(a[0]) &&& $Type.typeid == List(<$typeof(a[0])>).typeid:
    $echo("LIIIIST");
  $else
    $echo("NOT LIST");
  $endif
}

joshring avatar Oct 03 '24 19:10 joshring

macro @example(t_slice)
{
	var $type = $typeof(t_slice);
}

Error: A type must be followed by either (...) or '.'.

This is not an error

macro @example(t_slice)
{
	var $Type = $typeof(t_slice);
}

from: https://github.com/c3lang/c3c/issues/1553

joshring avatar Oct 12 '24 21:10 joshring

#76

sa-tasche avatar Oct 15 '24 13:10 sa-tasche

Another thing I ran into:

macro @escape_sequence(String seq)
{
  return "\u001b" +++ seq;
}

const TEST = @escape_sequence("[H");

Fails to compile and places the error at the "\u001b" literal, complaining it's not compile time constant. The fix is to simply prefix seq with $:

macro @escape_sequence(String $seq)
{
  return "\u001b" +++ $seq;
}

const TEST = @escape_sequence("[H");

The difference between the different types of macro parameters should probably be mentioned:

  • no prefix param
  • dollar sign prefix $param
  • hash prefix #param

Edit:

Nevermind, it's in there, but half a page down. Looking at it, maybe we should split macros into the "differences between C" and the actual macro system info.

tomaskallup avatar Oct 18 '24 20:10 tomaskallup