Giter Site home page Giter Site logo

Inline if with nil pointer about gomponents HOT 7 OPEN

maragudk avatar maragudk commented on June 19, 2024
Inline if with nil pointer

from gomponents.

Comments (7)

markuswustenberg avatar markuswustenberg commented on June 19, 2024 1

Hmm. Yeah, I get your point. I still think If has value, though, because it's so easy to inline components conditionally that would otherwise need to be branched elsewhere. Alternatively, one can use the IIFE pattern from Javascript-land (https://en.wikipedia.org/wiki/Immediately_invoked_function_expression).

from gomponents.

markuswustenberg avatar markuswustenberg commented on June 19, 2024 1

@egeozcan @onordgren if you like and have the time, I'd be happy if you had a look at my proposed lazy evaluation solution at #156. I think I've finally arrived at an API I like.

from gomponents.

markuswustenberg avatar markuswustenberg commented on June 19, 2024

Hi @onordgren , thank you for taking the time to file an issue! My answer time is a bit longer than usual, I've got the flu. 🤧

I agree that this is surprising behaviour, but actually a consequence of the design of gomponents itself. As you probably know, a component is basically (most often) a function. The functions are called inline in your example, and the component given to the g.If is always evaluated, but the result may be discarded. That's the reason for your nil pointer error.

I've explored possible solutions for this in #99 and #101, but haven't found a good one yet. Maybe I should use this opportunity to revisit the problem. Feedback and input appreciated. 😊

from gomponents.

markuswustenberg avatar markuswustenberg commented on June 19, 2024

I tried an implementation for a solution, but I'm not entirely satisfied. If anyone wants to have a look and post their thoughts, they are very welcome!

from gomponents.

egeozcan avatar egeozcan commented on June 19, 2024

Hi, a new user of the library here!

My humble suggestion is deprecating g.If and suggesting creating a function with an early nil return if the parameter does not satisfy. The language simply does not have the extensibility to enable any more elegant solution. But of course, I may be wrong and any pointers to alternative approaches would help a lot.

from gomponents.

markuswustenberg avatar markuswustenberg commented on June 19, 2024

Hi @egeozcan , thanks for chiming in.

What you're proposing is essentially what the current implementation does:

func If(condition bool, n Node) Node {
	if condition {
		return n
	}
	return nil
}

The problem lies not in what the function does, but when parameters are evaluated when using it. Because gomponents is all just nested functions, the innermost function and its parameters is evaluated before we even reach the If, and that's where the nil panics occur. The only way around that is to guard against nil in a custom inline function (which returns a Node), or check for nils earlier in the component and behave appropriately.

Or am I misunderstanding your suggestion?

from gomponents.

egeozcan avatar egeozcan commented on June 19, 2024

hi @markuswustenberg, well I meant not using a generic function, and using a custom function to do this. the g.node is super easy to compose so I humbly suggested that, that should be encouraged instead of offering abstractions that can't cover some cases such as the one discussed and may cause confusion. thanks for the detailed reply 😊

from gomponents.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.