Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

maybe type #7

Closed
8 tasks done
andrewrk opened this issue Dec 11, 2015 · 2 comments
Closed
8 tasks done

maybe type #7

andrewrk opened this issue Dec 11, 2015 · 2 comments
Labels
enhancement Solving this issue will likely involve adding new logic or components to the codebase.
Milestone

Comments

@andrewrk
Copy link
Member

  • generic ? syntax for declaring a maybe type
  • syntax for assigning the maybe state to a maybe type
  • if (const foo ?= expression) {} syntax
  • ?? unwrapping expression
  • test for many ? qualifiers on a type
  • optimization for bool - use 0 for false, 1 for true, 2 for maybe state
  • optimization for pointer - use null pointer for maybe state
  • error for if var expression initializer not maybe type
@andrewrk andrewrk added the enhancement Solving this issue will likely involve adding new logic or components to the codebase. label Dec 11, 2015
@andrewrk
Copy link
Member Author

Better idea, no null pointer constant.

Instead we have a "maybe" type, perhaps by putting a question mark before another type, like this:

?*mut u8

This represents a tagged union which can either be None or a *mut u8 which is guaranteed to not be null. You could .unwrap() on a maybe type to resolve to a non-null type, or panic.

Also, a ?? operator which the left operand is a maybe type and the right operand is an expression which is of the unwrapped type. So you could for example:

fn func(x : ?bool) {
    let want_the_thing = x ?? true;
}

Now you have an optional boolean that has a default value of true.

Or:

fn create_a_thing() -> error {
    let thing = malloc(...) ?? {
        return Error.OutOfMem;
    };
    things.append(thing);
    return Error.None;
}

The right hand side of the ?? returned unreachable which is also legal.

@andrewrk andrewrk changed the title add void pointer constant maybe type Dec 13, 2015
@thejoshwolfe
Copy link
Contributor

Alternate unpacking syntax ideas:

idea 1:

fn report_error(msg : string, maybe_additional_info : ?string) {
    report(str);
    ifunpack additional_info = maybe_additional_info {
        report(additional_info);
    }
}

idea 2:

fn report_error(msg : string, maybe_additional_info : ?string) {
    report(str);
    if let additional_info ?= maybe_additional_info {
        report(additional_info);
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Solving this issue will likely involve adding new logic or components to the codebase.
Projects
None yet
Development

No branches or pull requests

2 participants