Skip to content

Commit

Permalink
std::flag: use constraints for the Add and AddVar functions
Browse files Browse the repository at this point in the history
  • Loading branch information
mertcandav committed Aug 7, 2024
1 parent a3a1c6f commit 6edb3ed
Showing 1 changed file with 2 additions and 11 deletions.
13 changes: 2 additions & 11 deletions std/flag/flag.jule
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Use of this source code is governed by a BSD 3-Clause
// license that can be found in the LICENSE file.

use comptime for std::comptime
use conv for std::conv
use strings for std::strings

Expand Down Expand Up @@ -130,14 +129,6 @@ impl FlagSet {
}

fn addFlagCommon[T](mut self, name: str, short: rune, what: str): &Flag[T] {
match type comptime::Match(T) {
| i64 | u64 | f64 | bool | str:
// Ok
break
|:
panic("std::flag: FlagSet.Add[T]: unsupported typed for flag")
}

if self.FindFlag(name) != nil {
panic("std::flag: FlagSet.Add[T]: flag is already exist in this name: " + name)
}
Expand Down Expand Up @@ -166,14 +157,14 @@ impl FlagSet {
// - f64
// - str
// - bool
fn Add[T](mut self, name: str, short: rune, default: T, what: str): &T {
fn Add[T: i64 | u64 | f64 | bool | str](mut self, name: str, short: rune, default: T, what: str): &T {
mut flag := self.addFlagCommon[T](name, short, what)
flag.data = new(T, default)
ret flag.data
}

// Same with add method but do not allocates new reference, uses existing.
fn AddVar[T](mut self, mut var: &T, name: str, short: rune, what: str) {
fn AddVar[T: i64 | u64 | f64 | bool | str](mut self, mut var: &T, name: str, short: rune, what: str) {
mut flag := self.addFlagCommon[T](name, short, what)
flag.default = *var
flag.data = var
Expand Down

0 comments on commit 6edb3ed

Please sign in to comment.