question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Nested structs and functions within structs

See original GitHub issue

Goal

Allow for nested structs and functions within structs.

Overview

Depends on (#28)

Closures are cool, but to be useful you’d want to return an object/struct with a closure or another struct as a field.

Basically, this syntax should be possible

type lambda Closure = () => i32;
type Struct = {
   increment: Closure,
   decrement: Closure
};
function getActions(): Struct {
  let x: i32 = 0;
  const result: Struct = 0;
  result = {
     increment: (): i32 => { 
        x += 1;
        return x;
     },
    decrement: (): i32 => {
        x -= 1;
        return x;
    },
    getX: (): i32 => { return x; }
  };
  return result;
}

export function test() : i32 {
  const actions: Struct = getActions();
  actions.increment();
  actions.increment();
  actions.decrement();
  // should be 1
  return actions.getX();
}

This should make the syntax much more expressive and allow for a wide range of applications.

Acceptance Criteria

  • Allow nested struct types within other structs
  • Allow closure types within structs
  • Test everything

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:5
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
Chamberlain91commented, Jul 24, 2018

Might I also suggest replacing the ‘type’ keyword for ‘lambda’. For example 'lambda Func = () => i32’

Note: I’m on my phone and I’m unsure how the syntax style formatting works.

2reactions
ballercatcommented, Feb 8, 2018

Cool, so are you suggesting that we should have a built-in type for Lambda instead of a keyword? That could work I think with something like this

-type lambda Closure = () => i32;
+type Closure = Lambda<() => i32>;

or

-type lambda Closure = () => i32;
+type FunctionType = () => i32;
+type Closure = Lambda<FunctionType>

I personally have not seen this particular use of generics before, but this could work. Would need a type parser change/PR but I like it 👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

Nested Structure in C with Examples - GeeksforGeeks
A nested structure in C is a structure within structure. One structure can be declared inside another structure in the same way structure...
Read more >
Nested Structure in C - Scaler Topics
Nested structures can be passed to functions directly by passing the structure as an argument to the function. ... Here the structure variable...
Read more >
Nested Structure in C | C Program | Fresh2refresh
Nested structure in C is nothing but structure within structure. ... This program explains how to use structure within structure in C using...
Read more >
What is the purpose of using nested struct's in c programming ...
Grouping information in meaningful structures is useful to write functions that deal with the group independently of whether it is a stand ...
Read more >
Nested Structures in C - C Programming Tutorial - OverIQ.com
A structure can be nested inside another structure. In other words, the members of a structure can be of any other type including...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found