Skip to content
This repository was archived by the owner on Jan 28, 2021. It is now read-only.
This repository was archived by the owner on Jan 28, 2021. It is now read-only.

Proposal: Specify function arity instead of using reflection #37

@erizocosmico

Description

@erizocosmico

Current state

Right now, we pass a interface{} as the UDF, which then is inspected and called using reflect.

Proposal

Instead, I propose to have fixed arity specified by the user.

Benefits of this:

  • does not use reflection, so more maintainable code
  • just looking at the registry you can see the arity of the function
  • does not allow random signatures, so it's more type-safe

The implementation would look like the following:

type Function interface {
        Arity() int
}

type Function1 func(sql.Expression) sql.Expression
type Function2 func(sql.Expression, sql.Expression) sql.Expression)
// ...
type FunctionN func(sql.Expression, sql.Expression, sql.Expression, sql.Expression) sql.Expression)

func (Function1) Arity() int { return 1 }
func (Function2) Arity() int { return 2 }
// ...
func (FunctionN) Arity() int { return n }

// Call could be converted to Call(...sql.Expression) 
func Call(fn Function, exprs ...sql.Expression) (sql.Expression, error) {
        if fn.Arity() != len(exprs) { /* throw error */ }
        switch fn := fn.(type) {
        case Function1: fn(exprs[0])
        // ...
        }
}

Then in the registry:

var Functions = map[string]Function{
        "func": Function1(NewMyFunc),
}

Metadata

Metadata

Assignees

Labels

enhancementNew feature or requestproposalproposal for new additions or changes

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

    pFad - Phonifier reborn

    Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

    Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


    Alternative Proxies:

    Alternative Proxy

    pFad Proxy

    pFad v3 Proxy

    pFad v4 Proxy