Skip to content

Generic array #22979

@khalyomede

Description

@khalyomede

Describe the feature

The compiler may enforce that a generic type is a subset of array.

Use Case

When we want to create generic functions that take an array of any types.

Proposed Solution

To be able to write it like this:

// definition
struct Expect[T] {
  actual T
}

fn expect[T](value T) Expect[T] {
  return Expect{ actual: value }
}

fn (expect Expect[[]T{}]) to_contain(value T) {
  assert expect.actual.contains(value)
}

// usage
expect([1, 2, 3]).to_contain(3) // assertion passes
expect([1, 2, 3]).to_contain(4) // assertion failure
expect([1, 2, 3]).to_contain(true) // compilation type mismatch error

Other Information

I sticked with the same syntax with accolads as for array initializations (numbers := []int{}).

Acknowledgements

  • I may be able to implement this feature request
  • This feature might incur a breaking change

Version used

V 0.4.7 e03bd3f

Environment details (OS name and version, etc.)

V full version: V 0.4.7 e03bd3f
OS: linux, Linux version 6.8.0-49-generic (buildd@lcy02-amd64-103) (x86_64-linux-gnu-gcc-12 (Ubuntu 12.3.0-1ubuntu122.04) 12.3.0, GNU ld (GNU Binutils for Ubuntu) 2.38) #4922.04.1-Ubuntu SMP PREEMPT_DYNAMIC Wed Nov 6 17:42:15 UTC 2
Processor: 8 cpus, 64bit, little endian, Intel(R) Core(TM) i7-10510U CPU @ 1.80GHz

getwd: /home/v
vexe: /opt/vlang/v
vexe mtime: 2024-09-26 07:47:51

vroot: OK, value: /opt/vlang
VMODULES: OK, value: /root/.vmodules
VTMP: OK, value: /tmp/v_0

env VFLAGS: "-cc gcc"

Git version: git version 2.40.3
Git vroot status: e03bd3f (500 commit(s) behind V master)
.git/config present: true

CC version: cc (Alpine 12.2.1_git20220924-r10) 12.2.1 20220924
thirdparty/tcc status: thirdparty-linuxmusl-amd64 a3e24da2

Huly®: V_0.6-21420

Note

You can use the 👍 reaction to increase the issue's priority for developers.

Please note that only the 👍 reaction to the issue itself counts as a vote.
Other reactions and those to comments will not be taken into account.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Feature/Enhancement RequestThis issue is made to request a feature or an enhancement to an existing one.complex
    No fields configured for Feature.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions