Giter Club home page Giter Club logo

Comments (4)

 avatar commented on May 11, 2024 1

I've been messing around a bit trying to understand the code, so i tried messing around with the type conversion default.

This is basically a proof of concept, would something like this work for you? if so i'll make a pr for all built in conversion types including tests.

If needed i can also add an error type ErrWrongObjectType or something like that when the default isn't an Int (for this specific case)

func builtinInt(args ...Object) (Object, error) {
	argsLen := len(args)
	if !(argsLen == 1 || argsLen == 2) {
		return nil, ErrWrongNumArguments
	}

	switch arg := args[0].(type) {
	case *Int:
		return arg, nil
	case *Float:
		return &Int{Value: int64(arg.Value)}, nil
	case *Char:
		return &Int{Value: int64(arg.Value)}, nil
	case *Bool:
		if arg.Value {
			return &Int{Value: 1}, nil
		}
		return &Int{Value: 0}, nil
	case *String:
		n, err := strconv.ParseInt(arg.Value, 10, 64)
		if err == nil {
			return &Int{Value: n}, nil
		}
	}

	if argsLen == 2 {
		switch arg := args[1].(type) {
		case *Int:
			return arg, nil
		}
	}

	return UndefinedValue, nil
}

from tengo.

d5 avatar d5 commented on May 11, 2024

Yeah, I'd appreciate if you could create a PR for this! 😄 Btw, I just merged a big PR that has some code changes around these type conversions. But it shouldn't be too different.

from tengo.

d5 avatar d5 commented on May 11, 2024

I also added a documentation on types: https://github.com/d5/tengo/wiki/Variable-Types. Just FYI.

from tengo.

 avatar commented on May 11, 2024

I will create a PR shortly, should be able to adjust it to the changes you made in the latest PR in like an an hour or so

from tengo.

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.