Following calls to static methods with indexing when importing classes

前端 未结 1 677
迷失自我
迷失自我 2021-02-15 06:01

I have a class file myClass.m in a package folder +myPack that\'s on the path. A simple example of the class file is:

classdef myClass
         


        
1条回答
  •  广开言路
    2021-02-15 06:25

    Here is some more weird for you: the behavior is different if you are running in the command window, from a script, or from a function!

    1) command prompt (1st: ok, 2nd: error)

    This is what you've already shown

    >> x = myPack.myClass(2).prop
    x =
         2
    
    >> import myPack.myClass; y = myClass(2).prop
    Static method or constructor invocations cannot be indexed.
    Do not follow the call to the static method or constructor with
    any additional indexing or dot references. 
    

    2) Script (1st: error, 2nd: error)

    testMyClassScript.m

    x = myPack.myClass(2).prop
    import myPack.myClass; y = myClass(2).prop
    

    and

    >> testMyClassScript
    Static method or constructor invocations cannot be indexed.
    Do not follow the call to the static method or constructor with
    any additional indexing or dot references.
    Error in testMyClassScript (line 1)
    x = myPack.myClass(2).prop 
    

    (the second line would also throw the same error)

    3) Function (1st: ok, 2nd: ok)

    testMyClassFunction.m

    function testMyClassFunction()
        x = myPack.myClass(2).prop
        import myPack.myClass; y = myClass(2).prop
    end
    

    and

    >> testMyClassFunction
    x =
         2
    y =
         2
    

    I would definitely call that a bug :) The expected behavior is to give an error in all cases.

    0 讨论(0)
提交回复
热议问题