Nim icon indicating copy to clipboard operation
Nim copied to clipboard

std/options is very picky on the types

Open Alogani opened this issue 1 year ago • 0 comments

Summary

Hello,

Option type checking is more restrictive than it should be. It makes the usage of options verbose.

Description

When using Option, it is always mandatory to cast every object providing to it, even when types are compatible according to the type checker. It concerns inherited ref objects and proc() objects at least.

Here is an example :

import std/options

type
    BaseObject = ref object of RootRef
    ChildObject = ref object of BaseObject

    Cb = proc()

var myObject: BaseObject
myObject = ChildObject() ## Works
var myOptionalObject: Option[BaseObject]
myOptionalObject = some ChildObject() ## Fail
myOptionalObject = some ChildObject().BaseObject ## Works

var myCallback: Cb
myCallback = proc() = discard ## Works
var myOptionalCallback: Option[Cb]
myOptionalCallback = some proc() = discard ## Fail
myOptionalCallback = some proc() {.closure, noSideEffect, gcsafe.} = discard ## Works
myOptionalCallback = some Cb(proc() = discard) ## Works

Expected behaviour

Assigning an Option value to an Option typed variable should have the same "compatability" as assigning a value to a standard typed variable.

Alternatives

No response

Examples

No response

Backwards Compatibility

No response

Links

No response

Alogani avatar May 04 '24 10:05 Alogani