none
Does Value.Type return the primitive type or the custom type? RRS feed

  • Question

  • The ascribed type of a value may be obtained using Value.Type. But what is the ascribed type? The primitive type or the custom type?
    Value.Type(Value.ReplaceType(   {1}, type {number}) equals type {number} though only type list is shown, but
    Type.ListItem(Value.Type(Value.ReplaceType({1}, type {number}))) equals type number, thus it seems to be the custom type.
    Value.Type([x = type number, y   = type text]) equals type record
    Type.RecordFields(Value.Type([x   = type number, y = type text])) equals [x = [Type = type   any, Optional = false], y = type any, Optional = false]], thus it seems to be only the primitive type 
    Saturday, January 31, 2015 2:12 PM

Answers

  • You're not actually ascribing a type there. You're constructing a record with two fields and taking its type. Here's an actual example of ascribing a type and then using Type.RecordFields to recover the ascribed information

    M> Type.RecordFields(Value.Type(Value.ReplaceType([x=1,y=""], type [x = number, y = text])))
    [
        x = [
            Type = type number,
            Optional = false
        ],
        y = [
            Type = type text,
            Optional = false
        ]
    ]

    • Marked as answer by frank-t Sunday, February 1, 2015 3:50 PM
    Saturday, January 31, 2015 5:32 PM

All replies

  • A value generally[*] starts out with a type that represents its actual type -- what you referred to as "primitive type" though I don't think we have a formal name for the concept. You can then ascribe some other type to it with Value.ReplaceType. Value.Type will then return the ascribed type (if there is one) or the actual type (if none has been ascribed).

    The actual type of a complex value (like a record) is just "record". The only way for it to have a more complex type is if that type was ascribed to it.

    Within certain limits, you can ascribe the wrong complex type to a value. For instance, you can ascribe the type {text} (that is, list of text) to a list value which contains numbers, but you cannot ascribe that type to a scalar number.

    * I use the weasel word "generally" because it really depends on who constructed the value. Certain data sources may ascribe types onto values for you as part of their construction.

    Saturday, January 31, 2015 3:09 PM
  • Thanks for your quick answer, Curt.

    Thus, the ascribed type is what the formula language specification calls the custom type.

    But then the functioning of Value.Type seems to be odd.

    The following is a record and IMO I have ascribed a complex type to it.

    [x = type number, y = type text]

    However,

    Type.RecordFields (Value.Type([x   = type number, y = type text]))

    returns

    [x = [Type = type   any, Optional = false], y = [Type =type any, Optional = false]] 

    So, why do number/text get lost here?

    BTW, how can I avoid these stupid line breaks? I'm just typing.

    Saturday, January 31, 2015 3:56 PM
  • You're not actually ascribing a type there. You're constructing a record with two fields and taking its type. Here's an actual example of ascribing a type and then using Type.RecordFields to recover the ascribed information

    M> Type.RecordFields(Value.Type(Value.ReplaceType([x=1,y=""], type [x = number, y = text])))
    [
        x = [
            Type = type number,
            Optional = false
        ],
        y = [
            Type = type text,
            Optional = false
        ]
    ]

    • Marked as answer by frank-t Sunday, February 1, 2015 3:50 PM
    Saturday, January 31, 2015 5:32 PM
  • @Curt

    Now it's clear. Thanks!

    Sunday, February 1, 2015 4:00 PM