Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - lerno

Pages: 1 ... 15 16 [17]
241
General Discussion / Struct "inheritance"
« on: June 24, 2018, 12:13:33 AM »
Taking a page from JAI, why not allow "struct includes":

Code: [Select]
type MyData struct {
    const char* name;
    State status;
}

type MyDataExtended struct {
    int32 foo;
    MyData data @(inline);
    ExtendedStatus extendedStatus;
}

MyDataExtended foobar;

// initialize foobar here
...
// Init done


Code: [Select]
printf("Value: %s\n", foobar.name) // Due to the @(inline) attribute.

func void MyData.parse(MyData* data) {
    // ...
}

func void randomizeData(MyData* data) {
  // ...
}

foobar.parse() // valid, equivalent to foobar.data.parse()
randomizeData(foobar) // valid, equivalent to randomizeData(foobar.data)

All of this is resolved during compile time, so this would not work for example:

Code: [Select]
func void funky(void *data) {
   randomizeData(cast<MyData>(data));
}

funky(cast<void>(foobar)); // Will not work as expected.
However, the code WOULD behave nicely if MyDataExtended did not start with the i32 field...

242
General Discussion / Re: Complex numbers
« on: June 23, 2018, 11:49:34 PM »
Simple operations on complex numbers, quaternions and matrices is pretty much all that a sane programmer uses to operator overloading for.

Support for this out of the box would be very nice, but it's important to understand that people will want to write their own optimized libraries to plug in into the system.

243
Ideas / Re: What about initialization in conditions?
« on: June 23, 2018, 10:50:25 PM »
Code: [Select]
while (Height h = getHeight()) {
  ..
}

This code confused me totally. What this compiles to is apparently:

Code: [Select]
Height h = getHeight();
while (h) {
  ..
}

NOT what I would expect actually. I suggest that:

Code: [Select]
while (Height h = getHeight()) {
  ..
}

// => should not compile

while (Height h = getHeight(); h) {
  ..
}

// => compiles to

Height h = getHeight();
while (h) {
  ..
}

Finally we then can have have

Code: [Select]
while (Height h; h = getHeight()) {
  ..
}

// Compiles to
Height h;
while ((h = getHeight()) {
  ..
}

Since this is all new, it's possible to actually try different syntax from C:

Code: [Select]
// Separate by ':'
while (i32 y = 0 : y > 0) {
while (i32 y = 0; i32 z = 0 : y < 100 && z < 3) {

// Group multiple declarations in { }:
while ({i32 x = 0; i32 y = 100;}; x < 100 && y < 500) {

I'm a bit fond of the ":" version since it cleanly separates what only occurs before the while evaluation (left of ':'), and what is part of the repeated evaluation (right of ':').

244
There is a good reason why one would like to allow it though: it allows something similar to class extensions.

For example, let's day one module defines a "string" struct, which then is a char pointer + length.

The code has a String.find(char *) function, but is missing String.beginsWith and String.endsWith – which are really useful for some string handling.

Obviously you can define string_begins_with and string_ends_with, but it will be less nice to have:

Code: [Select]
if (string_begins_with(string, "foo")) {
  return string.toUpper();
}

When we could have:
Code: [Select]
if (string.beginsWith("foo")) {
  return string.toUpper();
}

At least of me it's common to build up a complementary library of additional functions on various types. In many languages we are forced to create stand-alone functions, because the class definition is closed. In languages like Objective-C – or in any language with UFCS, we have the option to let ourselves extend this definition (regardless wether we are struct-based or use OO).


245
General Discussion / Why the module restriction on struct functions?
« on: June 22, 2018, 10:35:57 PM »
From the docs:

"struct-functions are defined in the same module as the struct (not necessary the same file!)"

Why this restriction?

246
Ideas / What about initialization in conditions?
« on: June 22, 2018, 05:33:25 PM »
A typical while looks like this:

Code: [Select]
i32 a = 10;
while (a > 0) {
    a--;
}

Often it's not really necessary to have the variable outside the while scope. We could support this syntax:

Code: [Select]
while (i32 a = 10; a > 0) {
    a--;
}

It would be equivalent to:

Code: [Select]
{
   i32 a = 10;
   while (a > 0) {
      a--;
   }
}

The same could be used for other similar statements:

Code: [Select]
// if
if (i32 a = foo(); a > 0) {
   // a is available here
} else {
   // a is also available here
}
// case
switch (Height h = getHeight(); h) {
   case LOW:     
   case MEDIUM:
      // can access "h" here
      break;
   case HIGH:
      // can access "h" here
      break;
}

247
Ideas / Re: [Email suggestion No. 3] type casts in C2 + bugs
« on: June 22, 2018, 11:16:09 AM »
C/C++/Java style casts of the form (Bar)foo is notoriously hard to visually parse because the precedence rules are rarely clear. The cast<Bar>(foo) is easier to visually parse since the object of the cast is clear. Another way is to introduce a post-fix notation with an "AS" operator or similar, e.g. foo as Bar where as binds directly to the object to the left:

Code: [Select]
foo as Bar // == cast<Bar>(foo)
foo.baz as Buz // == cast<Buz>(foo.baz)
(foo as Bar).fooBar // == cast<Bar>(foo).fooBar
i as u8 // == cast<u8>(i)
cc = ((c << 2) & 0xFF) as u8 // == cast<u8>((c << 2) & 0xFF)

Pages: 1 ... 15 16 [17]