GithubHelp home page GithubHelp logo

Comments (7)

flosch avatar flosch commented on May 16, 2024 1

You're welcome! Yes, a better documentation is on my TODO list (#36). I will definitely work on better docs. :-)

from pongo2.

flosch avatar flosch commented on May 16, 2024

It should work (and it works in the tests at least, for example here: https://github.com/flosch/pongo2/blob/master/pongo2_template_test.go#L44 and https://github.com/flosch/pongo2/blob/master/template_tests/complex.tpl#L22). Can you please provide more code (how you're creating the object and how you pass it to the template etc.) or - even better - a working minimal example where this does not work for you so it's possible for me to reproduce it? Thanks!

Are you sure you're passing a pointer to a Flash object to your context?

from pongo2.

ahall avatar ahall commented on May 16, 2024

The flash object is not a pointer, but I still expected it to be able to call the method and be implicitly converted.

from pongo2.

flosch avatar flosch commented on May 16, 2024

Method with pointer receivers are only part of the method set of *T (not T). If you have a method with a pointer receiver the object must be of type *Flash in order to access this method.

See https://golang.org/ref/spec#Method_sets:

A type may have a method set associated with it. The method set of an interface type is its interface. The method set of any other type T consists of all methods declared with receiver type T. The method set of the corresponding pointer type *T is the set of all methods declared with receiver *T or T (that is, it also contains the method set of T). Further rules apply to structs containing anonymous fields, as described in the section on struct types. Any other type has an empty method set. In a method set, each method must have a unique non-blank method name.

from pongo2.

ahall avatar ahall commented on May 16, 2024

I would have expected this not to work then:

package main

import (
    "fmt"
)

type Blah struct {
    Name string
}

func (this *Blah) Test() string {
    return this.Name
}

func main() {
    b := Blah{Name: "Something"}
    fmt.Printf("Al: %s\n", b.Test())
}

Here b is not a pointer, yet it can call the Test() function. I would have the same expectation with the code in this issue.

from pongo2.

flosch avatar flosch commented on May 16, 2024

The reason for that is https://golang.org/ref/spec#Calls:

A method call x.m() is valid if the method set of (the type of) x contains m and the argument list can be assigned to the parameter list of m. If x is addressable and &x's method set contains m, x.m() is shorthand for (&x).m()

pongo2 uses the reflect package do get the method set and it follows the before-mentioned definition of a method set. So yes, it works in Go, but it does not work in pongo2 which follows the strict method set definition. Your solution is to pass a pointer to an object (which is usually a good idea anyway).

from pongo2.

ahall avatar ahall commented on May 16, 2024

Good to know and thanks for the clarification :). Just thought some of pongo2 users may get into the same issue, so perhaps we could document this.

from pongo2.

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.